Judging the scroll bar to the bottom, you need to use the DOM's three attribute values, namely ScrollTop, ClientHeight, ScrollHeight.
ScrollTop is the scroll distance of the scrollbar on the y-axis.
ClientHeight is the height of the viewable area of the content.
ScrollHeight the height of the viewable area of the content plus the distance to overflow (scroll).
From the introduction of this three attributes can be seen, scroll bar to the bottom of the condition is scrolltop + clientheight = = ScrollHeight.
Nonsense not much to say, hurriedly on the code (compatible with different browsers).
The code is as follows:
Scrolling distance of the scroll bar on the y-axis
function Getscrolltop () {
var scrolltop = 0, bodyscrolltop = 0, documentscrolltop = 0;
if (document.body) {
Bodyscrolltop = Document.body.scrollTop;
}
if (document.documentelement) {
Documentscrolltop = Document.documentElement.scrollTop;
}
ScrollTop = (bodyscrolltop-documentscrolltop > 0)? Bodyscrolltop:documentscrolltop;
return scrolltop;
}
The total height of the document
function Getscrollheight () {
var scrollheight = 0, bodyscrollheight = 0, documentscrollheight = 0;
if (document.body) {
Bodyscrollheight = Document.body.scrollHeight;
}
if (document.documentelement) {
Documentscrollheight = Document.documentElement.scrollHeight;
}
ScrollHeight = (bodyscrollheight-documentscrollheight > 0)? Bodyscrollheight:documentscrollheight;
return scrollheight;
}
Height of browser viewport
function Getwindowheight () {
var windowheight = 0;
if (Document.compatmode = = "Css1compat") {
WindowHeight = Document.documentElement.clientHeight;
}else{
WindowHeight = Document.body.clientHeight;
}
return windowheight;
}
Window.onscroll = function () {
if (getscrolltop () + getwindowheight () = = Getscrollheight ()) {
Alert ("You were in the bottom!");
}
};
It's easier to do it with jquery,
The code is as follows:
$ (window). Scroll (function () {
var scrolltop = $ (this). ScrollTop ();
var scrollheight = $ (document). Height ();
var WindowHeight = $ (this). Height ();
if (scrolltop + windowheight = = scrollheight) {
Alert ("You were in the bottom");
}
});
If you want to determine whether the scroll bar in an element is in the bottom, according to a similar idea, Change the document.body to a specific element, get scrolltop and scrollheight the same way, but get the element visible height need to use the offsetheight attribute, directly according to gourd painting scoop.
JS Judge scroll bar to the bottom