Static Header - Halts Animation on Downward Scroll, Resumes when Scrolling Ceases

I've implemented a fixed menu on my website.

Everything seems to be working fine, but here's the issue:

When I scroll down and the fixed menu starts animating, if I continue scrolling quickly, the animation pauses. It only continues when I stop scrolling, eventually fixing itself in place.

Here is the JavaScript code:

function init() {
        window.addEventListener("scroll", function() {
            var topbar = $("#topbar"),
                header = $("#header-main"),
                hdr_img = $("#header-main .inner img"),
                hdr_inner = $("#header-main .inner"),
                search = $("#search"),
                cart = $("#cart");
            $(window).width() > 1200 && ($(this).scrollTop() > 235 ? (header.addClass("sabit"), hdr_inner.addClass("sabit"), search.addClass("sabit"), cart.addClass("sabit"), hdr_img.addClass("sabit"), topbar.addClass("sabit"), header.stop().animate({
                top: "-44px",
                opacity: 1
            }, 800), topbar.stop().animate({
                top: "-44px",
                opacity: 0
            }, 900)) : $(this).scrollTop() < 1 && (header.stop().animate({
                top: "0px",
                opacity: 1
            }, 800), topbar.stop().animate({
                top: "0",
                opacity: 1
            }, 1300), header.removeClass("sabit"), hdr_inner.removeClass("sabit"), search.removeClass("sabit"), cart.removeClass("sabit"), hdr_img.removeClass("sabit"), topbar.removeClass("sabit")))
        })
    }
    window.onload = init();

Below are the CSS codes as well:

#topbar{
  color:#7BBD42;
  background:#2e3a47;
  background-image:url("//images....com./sprites/kullanicimenupat.png");
  padding:5px 0;
  min-height:30px;
  border-bottom:1px solid #7bbd42;
  border-width:3px;
}
#topbar.sabit{
position:fixed;
width:100%;
z-index:99;
top:0;
}

#header-main{
  background-color:#ffffff;
  min-height:107px;
  color:#8c8c8c;
}
#header-main.sabit{
position:fixed;
width:100%;
z-index:1010;
padding-top:35px;
border-bottom:1px solid #AFC999;
min-height:100px;
min-height:99px;
background:rgba(255,255,255,1);
/* Gradient background styles */
}

#header-main .inner{
  text-align:left;
  margin-top:25px;
  padding:0;

#header-main .logo img{
  /* Design for logo image */
  transition:all 0.4s ease-in 0s;
}
#header-main .inner.sabit{
margin-bottom:-15px;
margin-top:18px;
}
#header-main .inner.sabit img{
  max-width:75%;
  margin-top:-4px;
  transition:all 0.5s ease 0s;
}

Answer №1

Implement a setTimeout function within the scroll event:

var timerY = -1;
$(window).on('scroll', function () {
    clearTimeout(timerY);
    timeToChangeViewPort = setTimeout(function() {
        initialize();
    }, 200);
});

Answer №2

The reason for this issue is caused by utilizing the .stop() method.

The .stop() function halts the ongoing animation on the specified elements.

Each time you scroll, the animation stops and restarts. Try removing the .stop() to see a different effect.

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

Show side by side using javascript

My dilemma lies in having a set of cards that are meant to be displayed inline, but I have to initially hide them using "display: none". When a specific button is clicked, I aim to reveal these cards; however, upon doing so, each card seems to occupy its o ...

Leveraging either Websockets or Long Polling (utilizing AJAX) to monitor database modifications utilizing PHP

Exploring Websockets and Long Polling After extensive research on both Google and SO, I have delved into the world of Websockets and Long Polling, weighing their advantages and disadvantages. However, a definitive solution still eludes me. I have immerse ...

Encountered difficulties logging in with MongoDB and Node.js

I encountered an issue while attempting to authenticate a user using MongoDB and Node.js. Although no errors are thrown, the system fails to provide the expected data after the user logs in. Below is a breakdown of my code. server.js var port=8888; va ...

Switching a material-ui input control instead of a textfield for materials-ui-datepicker: the ultimate guide

Within my React application (v16.3), I am utilizing the DatePicker component from the material-ui-pickers library to render date-picker controls. This particular component renders a Material-UI TextField. However, I am interested in modifying it to only di ...

Able to retrieve individual elements from an array, but not able to loop through them

I am facing an issue with my code where I have two arrays - one containing URLs and the other external data. These two arrays are perfectly aligned, but when I try to access them in a loop using AJAX requests, only the first element of the URL array prints ...

When scrolling back to the top of the page, the data-spy feature does not re-highlight the "Home" anchor

After experimenting with Data-spy to change the active anchor while scrolling, I encountered an issue. Upon scrolling back up to the top of the page from the about section, the "Home" anchor failed to re-activate. How can this be fixed? I attempted to rem ...

What is the method for triggering two actions within a single linked tag?

I have a link tag structured like this: <a href="<?php echo base_url().'dashboard' ?>" class="check_session">Home</a> Upon clicking the "Home" link, it should navigate to the dashboard. At the dashboard page, I want to check i ...

Leveraging a JavaScript variable declared outside the function to successfully transfer data to my AJAX function

Every time the enter key is pressed, my AJAX function gets executed. I used to pass a set of javascript variables equal to elements in the HTML (the contents of a text area) as data for the AJAX function. Now, I want these JS variables to hold values from ...

jQuery Drag Drop Sorting Feature Fails to Work when Moving Items from List to Table Cell

I need assistance with creating a sortable list that can have its items dragged into a table cell, where the items can then be sorted within that cell. The process involves: Dragging a list item into the table cell. Sorting the list item in the secon ...

Looking for a speedy solution with a [PHP function] that needs to be converted to a [JavaScript function

Looking for a quick favor - can anyone help me out with this: static function make_url_safe($z){ $z = strtolower($z); $z = preg_replace('/[^a-zA-Z0-9\s] /i', '', $z); $z = str_ireplace(' ', '-', $z) ...

JSON parsing throws an error due to encountering an 'unexpected end of input' issue

Here's some code I'm working with: var default_links = '{ "name" : "Google", "url": "https://encrypted.google.com/", "fav_url": "https://encrypted.google.com/favicon.ico" }\n'+ '{ "name" : "Yahoo", "url": "http://www. ...

Using media queries within specific CSS classes

I am attempting to create the following CSS code: .myClass { @media all (max-width: 1024px) { left: 33%; } @media all (min-width:1025px) { left: 25%; } } Unfortunately, it doesn't seem to be working as expected. Is t ...

How do I navigate to a different page in Vue.js HTML based on user selection?

Here is my first attempt at writing HTML code: <div class="col-md-4" > <div class="form-group label-floating"> <label class="control-label">Select No</label> <select class="form-control" v-model="or" required=""> ...

Understanding the intricacies of JavaScript function calls often results in unexpected null returns

I currently have a code that is able to run and collect data using an AJAX library. My goal is to allow users to add their own functions to the library and execute them, similar to $.get. It may be a bit difficult to fully explain what I am trying to achie ...

What is the best way to retrieve both the start and end date values from a React date range picker component

I have integrated a date range picker npm package called react-date-range into my code. On my screen, there is an "Apply Dates" button. When this button is clicked, I want to retrieve the selected date range value. How can I achieve this onclick event? ...

Utilizing AngularJS to selectively filter objects based on specific fields using the OR operator

My collection includes various items with different attributes. For instance, here is the information for one item: {"id":7,"name":"ItemName","status":"Active","statusFrom":"2016-01-04T00:00:00","development":"Started","devStartedFrom":"2016-01-04T00:00:0 ...

What steps can be taken to avoid my Bootstrap banner wrapping to a new line?

I am facing an issue while designing a top banner using Bootstrap. The condensed menu always wraps to a new line for the resolution of 1024x768. It works fine for resolutions greater than 1024x768. I need help with correcting my HTML so that the banner rem ...

Is there a way to incorporate the Indian rupee symbol into a Google chart?

I've been trying to incorporate the Indian Rupee symbol into a Google chart using the code below: var formatter = new google.visualization.NumberFormat({ prefix: '&#8377;' }); However, I'm encountering an issue where ...

Is there a way to create a mobile-exclusive slideshow using JavaScript?

I am trying to create a slideshow on my website, but whenever I add JavaScript it seems to break the desktop version. I want these three pictures to remain static and only start sliding when viewed on a mobile device. I have searched for resources on how t ...

Setting a default value in react-select

Recently, I developed a react-select component that is compatible with redux-form. The SelectInput component looks like this: const MySelect = props => ( <Select {...props} value={props.input.value} onChange={value => props.input.on ...