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

205
Views
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
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!