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.