JQuery - Enormous Delay in ScrollTop

Currently, I am developing a simple website with JQuery's ScrollTop functionality for the top navigation bar. However, I encountered a specific issue that I can't resolve.

When scrolling down past 50 pixels, the navbar shrinks as intended. But the problem arises when scrolling back up to the top. I implemented an else statement in my code to animate the navbar back to its original size if the scroll position is less than 50. While it does work, there is a noticeable delay of about 10 seconds after reaching the top. I'm baffled as to what could be causing this delay.

This is the JQuery code snippet I'm using:

$(window).scroll(function(){
          if ($(this).scrollTop() > 50) {
              $('.co_name').animate({ fontSize: '22pt', paddingTop: '18px', paddingBottom: '0px'}, 530);
          }  else {
            $('.co_name').animate({ fontSize: '40pt', paddingTop: '80px', paddingBottom: '75px'}, 530);
          }
      }); 

Although I don't see any issues with this code (given my limited experience with ScrollTop), you can view the entire functioning site at studionice.co/u/flatresponsive.

Does anyone have insights on what might be causing this delay?

Additionally, at times, the initial animation gets delayed when scrolling down. I've worked with JQuery before, and animations are usually swift. Could the delay be due to the additional scripts like Font Awesome, Bootstrap, and Flat UI integrated into my source code?

Answer №1

As you scroll, jQuery may trigger events multiple times leading to long animation durations. For instance, with each scroll triggering a 530-millisecond animation, scrolling down 10 times would require 5.3 seconds. Likewise, scrolling up another 10 times would add an additional 5.3 seconds. To optimize this process, consider implementing a lock mechanism that ensures animations occur only once when scrollTop() is greater than 50 or less than or equal to 50. Here's a sample implementation:

let animateLock = 0;
$(window).scroll(function(){
    if ($(this).scrollTop() > 50) {
        if (animateLock == 0) {
            animateLock = 1;
            $('.co_name').animate({ fontSize: '22pt', paddingTop: '18px', paddingBottom:'0px'}, 530);
        }
    } else {
        if (animateLock == 1) {
            animateLock = 0;
            $('.co_name').animate({ fontSize: '40pt', paddingTop: '80px', paddingBottom: '75px'}, 530);
        }
    }
});

Please note: This code snippet has not been tested.

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

Creating customizable Isotope objects using custom CSS margins that are influenced by the child-nth selector and JavaScript

My recent project involved creating a simple .isotope gallery, and when viewing the selected #portfolio-wrap container in Chrome Dev Tools, here is how it appears: Unfortunately, I am unable to upload three links here. Please visit this link for more info ...

Utilizing AJAX in ASP.net Web API 2 to send and receive JSON data efficiently

net Web API 2. I want to send a JSON string to one of my POST functions and receive JSON back. My AJAX: $("#btnPost").click(function (event) { var sqlQ = "SELECT TOP 50 LTRIM(RTRIM(REPLACE(OID, ' ', ''))) FROM vwPS_DAT WHERE OID != & ...

Leveraging LESS variables within media queries

After inputting the less code below into : @item-width: 120px; @num-cols: 3; @margins: 2 * 20px; @layout-max-width: @num-cols * @item-width + @margins; @media (min-width: @layout-max-width) { .list { width: @layout-max-width; } } The resul ...

What happens when the next button is clicked without any items selected?

I am working on a dynamic HTML page where a section changes based on user-selected options. Here is the part that displays the options: <div class="form-group-lg"> @foreach (var response in Model.ListResponses) { ...

Using the JSON stream object to loop through jQuery with the .each() method

CODE SNIPPET var IMController = { listIM: function () { var params = getUrlVars($("#getIM").attr("href")); $.ajax({ url: "listIM", type: "POST", dataType: "json", data: $.extend(params, { ...

Why is Chrome ignoring the flexbox CSS in the footer section of my website?

My footer flexbox CSS is working on various browsers and devices, but for some reason, Chrome on my iMac is not responding correctly. The website is www.batistaelectric.com. It works on my iPhone Chrome browser, Safari iPhone browser, iMac Safari browser, ...

Leveraging JavaScript event handlers within a progress wizard located within an update panel

I need assistance with implementing a password strength meter for a textbox within a wizard control. The issue I'm facing is that the password box does not become visible until step 4, preventing me from registering an event handler onload(). Placing ...

What is the reason behind Bootstrap 5's decision to have the getOrCreateInstance() method automatically toggle the collapsible element upon creation?

Today was my first encounter with the Collapse feature in Bootstrap 5, and I have to admit, I am not a fan! Imagine a scenario where I have a collapsible element that is initially hidden, and a button to toggle its visibility, like so: <button class=&q ...

"Encountering a form submission error - requested resource not located

I am currently trying to submit a form to an MVC controller using AJAX, where the controller is expected to take a form collection. I came across this helpful resource on Stack Overflow regarding passing formcollection using ajax call to an action. However ...

invoking a function with a callback within a loop

I need to execute window.resolveLocalFileSystemURI(file,success,fail) within a for loop where different file entries are passed. The goal is to only return the resolved entries in an array once all entries have been successfully retrieved. function resolv ...

Ways to retrieve the output parameter in the node mssql

` request.input('xyz',sql.Int,1); request.input('abc',sql.Numeric,2); request.output('pqr',sql.Int); request.output('def',sql.Char); request.execute('[StoredProcedure]',function(err,recor ...

Create a unique Bitcoin address using a derivation scheme

Starting with a derivation scheme that begins with tpub... for the testnet, I am looking to generate bitcoin addresses from this scheme. I also need a method that will work for the mainnet. Is there a library available that can assist me with this task? ...

Event fails to trigger when attached to different elements

Currently, I have an onchange event linked to the input text box and an onclick event attached to a text link. Interestingly, when I click on the link after editing the textbox, the click event does not trigger - instead, the change event is fired. If you ...

Utilize Axios in Vue.js to fetch and process data from a REST API endpoint in C#

After successfully creating and deploying an API on Azure, I am trying to display the response in an alert box using javascript (Vue.js). The test method of my API returns the string "working". You can test the API here. This is the code snippet from my A ...

Slideshow not displaying properly after an Ajax request due to CSS not being applied

My goal is to dynamically load data when reaching the bottom of a page using the onScroll event handler. The data to be loaded mainly consists of slideshows and, behind each one, within a separate div, there is an iframe with embedded vimeo content. Initi ...

Running into difficulties while attempting to sort through an object utilizing an array

My task is to filter a collection of objects based on an array of Fids. I have an object, $target = $('#tableA tr[data-id="' + elm.id + '"]'); // Collection of tr And I also have an array, var fids = $("#tableB tr .New.selected").pa ...

Using Object.freeze does not freeze the elements within an array

When I execute the following: var test = { 'test': 5 }; Object.freeze(test); // Throws an error test.test = 3; An error is thrown (as expected), but when I try this instead var nestedTest = [ {'test': 5}, {'test&ap ...

Creating a real-time text field update feature for a form using Google Script

One of my clients is dealing with a large number of contacts and to streamline the process, I created a form with a scrolling list for contact selection. However, the list has become too long to navigate easily. Is there a solution that would allow the c ...

Seeking some clarification on the autocomplete search queries

My goal is to enhance the search experience for my site's end users by implementing autofill functionality in the search box using AJAX calls. First, I will share my current code and then pose my questions. This is the HTML code: <input type="te ...

Tips for updating the class name of a specific div element during runtime

I'm trying to dynamically update the class name for a specific div at runtime, and this div also includes support for image preview using JQuery. ...