Categories
JavaScript Answers

How to determine if a div is scrolled to the bottom with JavaScript?

Spread the love

Sometimes, we want to determine if a div is scrolled to the bottom with JavaScript.

In this article, we’ll look at how to determine if a div is scrolled to the bottom with JavaScript.

How to determine if a div is scrolled to the bottom with JavaScript?

To determine if a div is scrolled to the bottom with JavaScript, we check if scrollTop is the same as scrollHeight - offsetHeight.

For instance, we write

if (obj.scrollTop === obj.scrollHeight - obj.offsetHeight) {
}

to check if obj.scrollTop is equal to obj.scrollHeight - obj.offsetHeight.

If it is, then we scrolled to the bottom of the obj element.

Conclusion

To determine if a div is scrolled to the bottom with JavaScript, we check if scrollTop is the same as scrollHeight - offsetHeight.

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 *