(Hope it is not a duplicate because I didn't find it when searching and googling)
I am trying to find how to detect in some fixed-height div ('#div') when the scroll-bar is reaching the bottom of this latter div. Should I use $(document).height()
and $(window).height()
to detect this event ?
Edit : My div which is fixed-height and about which I set auto-scroll, so how to deal with that ? if I am suppose to use $('#div').height(), this height is fixed....
The document height is the entire height of the whole document, even what is outside the viewable area. This could be thousands of pixels if you have a long page. The window height is just the viewable area.
The height of the document is not necessarily the same as the height of the window. If you have a simple document with just a DIV and a little bit of text, the doc height will be miniscule compared to the height of the window.
Here's the code from jQuery source:
So for
$(window)
clientHeight
is used. Which, as @Drew correctly mentioned the height of visible screen area.For
$(document)
the whole scroll height of the current page will be used.Difference between $(window).height() and $(document).height() function.
$(window).height() function:
Ideally $(window).height() returns the pixel less height of browser window. This is always the height of current browser window. If you resize browser this value should change.
$(document).height() function: $(document).height() returns an unit-less pixel value of the height of the document being rendered.
In HTML if you dont declare DOCTYPE then all time HTML page returns $(window).height() and $(document).height() same value.
Output :
After declare DOCTYPE its returns perfect value.
Output :
In the
.height()
documentation:In your case it sounds like you may want the height of the
document
rather than thewindow
. Think of it this way: Thewindow
height is what you see, but thedocument
height includes everything below or above.EXAMPLE
EDIT:
Checking for top and bottom on scroll with help from the
scrollTop()
method: