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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *