Categories
JavaScript Answers

How to detect if user is scrolling with JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.