Sometimes, we want to detect if user is scrolling with JavaScript.
In this article, we’ll look at how to detect if user is scrolling with JavaScript.
How to detect if user is scrolling with JavaScript?
To detect if user is scrolling with JavaScript, we can listen to the window’s scroll event.
For instance, we write
let userHasScrolled = false;
window.onscroll = (e) => {
userHasScrolled = true;
};
to set window.onscroll to a function that sets userHasScrolled to true when we scroll in the window.
Then if userHasScrolled is true, we know the user scrolled.
Conclusion
To detect if user is scrolling with JavaScript, we can listen to the window’s scroll event.