Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery : how to determine that a div is scrolled down

Tags:

jquery

scroll

I have a div with defined height, and overflow:scroll;. Its content is too long so scrollbars appear.

Now for the ichy part. Some of its inner HTML strangely appears all the time (to be precise, the footer of a table generated by the tableFilter plugin). I'd like to make this footer disappear when it is not needed (it actually appears out of the containing <div>'s border). I resolved to make it disappear but setting its z-index to -1000. But I want to make it appear when the containing <div> is totally scrolled down.

How can I know the user has scrolled at the bottom ?


Using the help from answers below, I used scrollTop attribute but the difference between scrollTop and innerHeight is the size of the scrollbar plus some unidentified delta. A scrollbar is 16 pixels high in most browsers under Windows, but I get a difference of 17 in Firefox and something like 20 in IE, where my <div> content's borders seems to be rendered bigger.

A way (actually two ways...) to compute the scrollbar size has been given there.

like image 522
glmxndr Avatar asked Jun 12 '09 13:06

glmxndr


1 Answers

You need to compare the div height with the scrollTop position and the element height.

$(div).scroll(function(){ 
  if(isScrollBottom()){ 
    //scroll is at the bottom 
    //do something... 
  } 
}); 

function isScrollBottom() { 
  var elementHeight = $(element).height(); 
  var scrollPosition = $(div).height() + $(div).scrollTop(); 
  return (elementHeight == scrollPosition); 
} 
like image 90
Shaun Humphries Avatar answered Nov 25 '22 05:11

Shaun Humphries