Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda