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

207
Visualizações
Scroll function only adds a class when scrolling down but doesn't remove it when scrolling to the top

I have this problem. At the bottom of a page is a fixed (position: fixed) div. When I scroll down I want to add a class to the div and when I scroll to the top the class must be removed. The following code works in the sense that it adds the class but when scrolling up it does not remove the class. So I added an alert to the else block and now if I scroll down it fires and I see a popup, which obviously makes no sense.

  $(window).scroll(function () {
    if ($(this).scrollTop() > 50) {
      $("#legalNotice").addClass("show");
    } else {
      $("#legalNotice").removeClass("show");
    }
  });

How can this be right? I tried it in multiple browsers and the same result every time.

I have another part to do the same when I scroll to the bottom and that works fine.

  $(window).scroll(function () {
    if ($(this).scrollTop() > 3000) {
      $("#legalNotice").removeClass("show");
    } else {
      $("#legalNotice").addClass("show");
    }
  });

What could the problem be?

about 4 years ago · Juan Pablo Isaza
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