Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
jQuery toggle class gets called too often on scroll

I am trying to add a class to my sticky container once the user scrolls the page past 100px, but it is lagging, and toggling the class uncontrollable.

$(function () {

    $nav = $(".topmenu-container");
    $(document).scroll(function () {
        if ($(this).scrollTop() >= 100) {
            $nav.addClass('scrolled');
        } else {
            $nav.removeClass('scrolled');
        }

    });
});

This is what I have so far.. I had the same function without an if clause and the toggleClass function instead, but same effect there.

Here you see the container that sticks to the top on scroll, and that I want to add the class to once the user scrolls past 100px from top for example

Edit: I now output the scrollTop value and saw, that it seems to get stuck at the point where the toggle should happen. It keeps jumping between 156px and 87px which makes the toggle happen multiple times a second. Does anyone have an idea?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What you need is rate limiting for your scroll events. In your case, I would suggest throttling, which means that your scroll events are limited to a certain number per second or time unit.

Another option is to use debouncing, but this would cause the event to be triggered only once at the end of the scrolling, which might not be the best solution for your case.

Read more here: Difference Between throttling and debouncing a function

Nice visualization: http://demo.nimius.net/debounce_throttle/

about 4 years ago · Juan Pablo Isaza Report

0

Depending on your use case you may actually get away with the new sticky value for the position property in CSS: https://developer.mozilla.org/en-US/docs/Web/CSS/position

Additionally, instead of using debouncing and throttling, you could consider using the IntersectionObserver API. The events for InteresectionObserver fire only once (depending on settings) and surely less than scroll which is a hard to optimize event since it fires all of the time. See here: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

Now, if you have some elements inside the element that depend on the class, such as... the logo having a scaling animation or such, you will definitely have to use IntersectionObserver. It looks like your threshold is 100 pixels, you can definitely configure Interesction Observer to do that.

You can also take a look at this example from Wes Bos which seems to be in one of his free classes: https://wesbos.com/javascript/06-serious-practice-exercises/scroll-events-and-intersection-observer

I'm hoping you can try these solutions instead of the antiquated scroll thing.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!