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
.