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

196
Views
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 answers
Answer question

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 Report
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!