Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a div is scrolled to the bottom?

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)  
like image 975
Bjorn Avatar asked May 18 '09 03:05

Bjorn


People also ask

How do I know if a div is scrolled to the bottom?

To detect when a user scrolls to bottom of div with React, we can check if the sum of the scrollTop and clientHeight properties of a scrollable element is equal to the scrollHeight property of the same element. We call the useRef hook to create a ref and we assign the returned ref to the inner div, which is scrollable.

How can I determine if a div is scrolled to the top?

position(function(){ if($(this). position(). top == 100){ alert('checkThis'); } });

How do you detect if browser window is scrolled to bottom in react?

An even simpler way to do it is with scrollHeight, scrollTop, and clientHeight. Subtract the scrolled height from the total scrollable height. If this is equal to the visible area, you've reached the bottom! In react, just add an onScroll listener to the scrollable element, and use event.

How do you know which element is scrolled?

Show activity on this post. Then when you scroll down you should see one really tall box. Right click on it and select inspect element. That should give you the information you need.


1 Answers

You're pretty close using scrollTop == scrollHeight.

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

Your if statement should look like so (don't forget to use triple equals):

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

Edit: Corrected my answer, was completely wrong

like image 83
James Davies Avatar answered Sep 25 '22 16:09

James Davies