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

198
Vistas
Get deltaY from scroll event

I am trying to set up a project for use with mobile and desktop but the wheel event doesn't work on mobile, so I need to use scroll.

I know how to get the deltaY from the Wheel event:

window.addEventListener("wheel", event => console.info(event.deltaY));

How do I get the deltaY from the Scroll event?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should memorize the last scroll position in a variable, and when the scroll event fires, you can compute the difference.

Here is a modified example for your use case from the MDN docs on scroll event:

let lastKnownScrollPosition = 0;
let deltaY = 0;

document.addEventListener('scroll', function(e) {
  let ticking = false;
  if (!ticking) {
    // event throtteling
    window.requestAnimationFrame(function() {
      deltaY = window.scrollY - lastKnownScrollPosition;
      lastKnownScrollPosition = window.scrollY;
      console.log('deltaY', deltaY);
      ticking = false;
    });
    ticking = true;
  }


});
body {
  background: lightgrey;
  height: 8000px;
}
<body>
</body>

The code above uses throtteling in order to reduce the amount of fired events. Also see: Difference Between throttling and debouncing a function

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