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

113
Views
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
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!