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

163
Views
Window scroll function not working for mobile

I have a navigation bar that should be visible only if the user scrolls up. Below is the code snippet that works fine for desktop.

$(window).scroll(function() {
 
    var scrollMovement = $(window).scrollTop();
   
    if (topPosition < 200 ){
    }
    else{
    if(scrollMovement > topPosition) {
          $('#main-header').removeClass('show-header');
          $('#main-header').addClass('hide-header');
    } else {
          $('#main-header').removeClass('hide-header');
          $('#main-header').addClass('show-header');
    }
    }
    topPosition = scrollMovement;
});

The above doesn't work on mobiles. I did check multiple similar questions on the forum and updated my codes, but no success. Below is what I have tried so far.

$(document.body).on('touchmove', onScroll); // for mobile 
$(window).on('scroll', onScroll); 

// callback
function onScroll(){ 
 
    var scrollMovement = $(window).scrollTop();
   
    if (topPosition < 200 ){
    }
    else{
    if(scrollMovement > topPosition) {
          $('#main-header').removeClass('show-header');
          $('#main-header').addClass('hide-header');
    } else {
          $('#main-header').removeClass('hide-header');
          $('#main-header').addClass('show-header');
    }
    }
    topPosition = scrollMovement;
};   

May I know what part I am not doing correctly?

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!