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

114
Vistas
Execute an event when the user scrolls X px down/up

I would like to detect the scroll direction of an user.

Only I would like to execute the event when the user scrolls 30px down or up depending on his position on the web page.

Currently in my code, the event is triggered too quickly on a mobile device. I would like to add a threshold to execute the event.

let lastScrollPosition = 0;

window.addEventListener("scroll", () => {
  // Get current user scroll position.
  let currentScrollPosition = document.body.getBoundingClientRect().top;

  if (currentScrollPosition > lastScrollPosition) {
    console.log("User scroll UP ⬆");
  } else {
    console.log("User scroll Down ⬇");
  }

  // Assign current user scroll position to last postion.
  lastScrollPosition = currentScrollPosition;
});

Thanks for your support.

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