The alternative condition for jQuery Color takes 17 seconds to respond

After extensive testing on all the browsers available on my computer, I implemented error handling and encountered no issues.

The script is simple and works, but not as intended.

When scrolling down, the bar should change to red, and while scrolling up, it should turn green (I used random colors for demonstration).

The issue arises when the color changes to red instantly as expected, but when it has to switch back to green at the top, it takes nearly 20 seconds to respond.

Here is the complete code snippet:

(function(){
    
    "use strict";
    
   $(document).ready(function(){
       
            var advancedNav = $('.navigation').offset().top;

            $(window).scroll(function(){
                if( $(window).scrollTop() > advancedNav ) {
                    $(".navigation").animate({
                        "background-color": "red"
                    }, 1000)
                } 
                else {
                   $(".navigation").animate({
                        "background-color": "green"
                    }, 1000)
                }
            });
    });
    
})();
.navigation {
    
    width: 100%;
    position: fixed;
    overflow: hidden;
    background: rgba(15, 15, 15, 0);
    z-index: 1;
    
}


.navigation ul {
    
    list-style-type: none;
    margin: 0;
    padding: 0;
    
}

.navigation li {
    
    float: left;
    
}

.navigation  li a {
    
    display: inline-block;
    color: white;
    text-align: center;
    font-size: 20px;
    padding: 14px 16px;
    margin-left: 20px;
    width: 50px;
    text-decoration: none;
    font-family: barelFont;
    
}

.navigation  li a:hover {
    background-color: #fff;
    color: #ddd;
}
<header>
    <div class="navigation">
        <ul>
          <li><a href="#everest">Main</a></li>
          <li><a href="#projectsSection">Projects</a></li>
          <li><a href="#aboutSection">About</a></li>
          <li><a href="#contactSection">Contact</a></li>
        </ul> 
    </div>
</header>

Upon attempting to rectify the issue, using the original jQuery css() method proved successful.

However, in pursuit of a dynamic and visually appealing solution, implementing jQuery Color and jQueryUI resulted in the same delay.

What could be causing this problem?

Answer №1

It has come to light that the issue stems from an excessive number of animations being added to the animation queue, as noted by @Taplar.

To address this, it is recommended to toggle between CSS classes instead:

(function(){
    
    "use strict";
    
   $(document).ready(function(){
       
            var advancedNav = $('.navigation').offset().top;

            $(window).scroll(function(){
                if( $(window).scrollTop() > advancedNav ) {
                    $(".navigation").removeClass('green').addClass("red")
                } 
                else {
                   $(".navigation").removeClass('red').addClass("green")
                }
            });
    });
    
})();
body, html {height:100%}
header {height:200%}
.navigation {
    
    width: 100%;
    position: fixed;
    overflow: hidden;
    background-color: rgba(15, 15, 15, 0);
    z-index: 1;
    transition:background-color 1s
    
}
.green {background-color: green}
.red   {background-color: red}


.navigation ul {
    
    list-style-type: none;
    margin: 0;
    padding: 0;
    
}

.navigation li {
    
    float: left;
    
}

.navigation  li a {
    
    display: inline-block;
    color: white;
    text-align: center;
    font-size: 20px;
    padding: 14px 16px;
    margin-left: 20px;
    width: 50px;
    text-decoration: none;
    font-family: barelFont;
    
}

.navigation  li a:hover {
    background-color: #fff;
    color: #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
    <div class="navigation">
        <ul>
          <li><a href="#everest">Main</a></li>
          <li><a href="#projectsSection">Projects</a></li>
          <li><a href="#aboutSection">About</a></li>
          <li><a href="#contactSection">Contact</a></li>
        </ul> 
    </div>
</header>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Understanding the Intrinsic Dimensions of Replaced Elements in CSS

I'm currently facing some challenges with the CSS Intrinsic & Extrinsic Sizing Module Level 3, specifically chapter 4.1 on Intrinsic Sizes. This section is proving to be quite difficult for me: When a block-level or inline-level replaced element h ...

How to use jQuery to hide radio buttons that are not checked when clicking a submit

Looking to dynamically hide all unchecked radio buttons and their labels until a submit button is clicked, displaying only the checked radio button. <form method="post"> <input type="radio" name="radiobtn"> <label for="first">Fir ...

Submitting a form with AJAX using JQuery in ASP.Net is made even easier when utilizing Master Pages for

After developing a search program with lucene.net, I encountered an issue where reloading the entire page was necessary to submit a new query. To resolve this, I explored implementing AJAX for seamless search submission, but have been facing difficulties i ...

What is the most effective method for declaring a constant within a WebComponent?

After receiving questions, I included more details on how I'm utilizing the constants. The main issue I am encountering is that the constants are being added to the global object, which raises concerns about potential name collisions. I created a Web ...

Swap out original source files in HTML with minified versions

I have successfully utilized a maven plugin to create a minified and compressed version of my CSS and JavaScript files. Now, I am looking to update the section in my main HTML page that currently loads all those individual files. Here is what it looks lik ...

Ensure that the two background images are identical in size and content

Can someone assist me with achieving vertical equality for two background images like in this example image? Currently, I have the following styling applied: background-repeat: no-repeat, repeat-x, repeat-y; background-position: 925px center; However, t ...

Ways to stylize bullet points in lists with CSS in HTML document

I am currently working on a Q&A page using simple HTML. This is the current appearance of my page. Check it out here on JSFIDDLE. My task involves adding an 'A' to the answer for each question. Some answers have multiple paragraphs, so the ...

What can I do to prevent my website's font from getting distorted due to the recommended display settings?

I've noticed that newer laptops typically have a display font-size recommendation of 150%, which is causing issues with viewing my webpage correctly. The items appear larger and stretched out on their screens. Is there a way to ensure that users can ...

I am encountering a continuous css ParserError while trying to compile my react project

I'm having trouble with the compilation of my React project. While everything runs smoothly with npm start, I encounter an error during compilation: npm run build <a href="/cdn-cgi/l/email-protection" class="__cf_ema ...

"Interaction with jQuery causes button element to shift position upon resizing of browser

I am encountering an issue with a div element that has jQuery appended to it for a bounce effect. The element is absolutely positioned within its relative parent. However, when the browser is resized, the child element does not remain centrally aligned dur ...

What is the best way to divide this string with jQuery?

Is there a way to use jQuery to split this specific string? "[10.072721346470422,76.32974624633789][[10.075854059674523,76.32043361663818],[10.073650930297095,76.32888793945312],[10.074918540288232,76.33090496063231],[10.073862198974942,76.33137702941895] ...

Is it possible to achieve this shaded gradient effect using CSS3?

Check out this image for reference. I've managed to style a horizontal rule using SVG as the background image, creating a specific effect. However, I am struggling to achieve the same result using CSS3 alone. If there are any CSS experts who can adv ...

Tips on how to submit the ID of a span element with ajax

Can anyone help me retrieve the post ID of a span using Ajax? I have tried using alert($(this).attr("id")) and it works, but when I try to use Ajax it doesn't work. Any ideas why? $(".nav-item").click(function() { $.ajax({ ...

jQuery's show/hide functionality allows for the dynamic resizing of images,

I am experiencing an issue with a Joomla template that has a custom jQuery menu. When I hover over the map with my mouse, the overlay appears slightly larger than expected. This problem seems to be occurring in Firefox and IE 11, leading me to believe it ...

Accessing the value of a field using JavaScript/jQuery

Welcome to my page! I am working on obtaining the current Start and Finish date values. I plan to add a button for users to click, which will then retrieve all dates and display them somewhere on the page. But for now, I just need a way to access the date ...

What are the steps to ensure HTML5 video fills the entire height and width of the browser window?

Is there a way to make a video fill the entire width and height of the browser on first page load using bootstrap? I am currently facing some issues with achieving this. The code that I have been using seems to work, but the height ends up being more than ...

What is the best way to display a child div without impacting the position of other elements within the same parent container?

As I work with a div html tag within a login form, encountering an error inside this form has presented a challenging issue. The error div sits at the top of its parent div, and ideally, upon activation, should remain within the form div without disrupting ...

Want to develop a web application and incorporate Ajax into it?

I'm sure many of you have created an online application that streamlines processes and saves time and money for your company. So, how did it go when you added some Ajax to enhance the user experience after developing the application? Any recommendat ...

Achieving responsive design using text font percentage instead of text images can be done by implementing the following code snippet provided below

When working with text in HTML, I encountered an issue where using percentages for width and height was not giving me the desired results. To work around this, I used images of text instead, but each image ended up either stretched or looking too small. H ...

Tips for adjusting the text color of input fields while scrolling down

I am currently working on my website which features a search box at the top of every page in white color. I am interested in changing the color of the search box to match the background color of each individual page. Each page has its own unique background ...