Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda