views:

506

answers:

1

How do I determine, without using jQuery or any other JavaScript library, if a div with a vertical scrollbar is scrolled all the way to the bottom?

My question is not how to scroll to the bottom. I know how to do that. I want to determine if the the div is scrolled to the bottom already.

This does not work:

if (objDiv.scrollTop == objDiv.scrollHeight) 
+3  A: 

You're pretty close using scrollTop == scrollHeight.

scrollTop refers to the top of the scroll position, which will be scrollHeight - offsetHeight

so

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

Edit: Corrected my answer, was completely wrong

Zoomzoom83
That almost works, scrollHeight-offsetHeight isn't exatly the same value as scrollTop, but after trying your code, if I require if that difference is fewer than 5 pixels for it to be the bottom I get the behavior I want.
apphacker