Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda