This article describes the JS Code used to judge the scroll bar to the bottom. If you need it, you can refer to it to determine the scroll bar to the bottom. You need to use the three DOM attribute values, namely, scrollTop, clientHeight, and scrollHeight.
ScrollTop indicates the scroll distance of the scroll bar on the Y axis.
ClientHeight is the height of the visible area of the content.
ScrollHeight is the height of the visible area of the content plus the overflow (scroll) distance.
From the introduction of these three attributes, we can see that the condition from the scroll bar to the bottom is scrollTop + clientHeight = scrollHeight.
Get started with code (compatible with different browsers ).
The Code is as follows:
// The scroll 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.doc umentElement ){
DocumentScrollTop = document.doc umentElement. scrollTop;
}
ScrollTop = (bodyScrollTop-documentScrollTop> 0 )? BodyScrollTop: documentScrollTop;
Return scrollTop;
}
// Document height
Function getScrollHeight (){
Var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
If (document. body ){
BodyScrollHeight = document. body. scrollHeight;
}
If(document.doc umentElement ){
DocumentScrollHeight = document.doc umentElement. scrollHeight;
}
ScrollHeight = (bodyScrollHeight-documentScrollHeight> 0 )? BodyScrollHeight: documentScrollHeight;
Return scrollHeight;
}
// The height of the browser.
Function getWindowHeight (){
Var required wheight = 0;
If (document. compatMode = "CSS1Compat "){
Required wheight = document.doc umentElement. clientHeight;
} Else {
Optional wheight = document. body. clientHeight;
}
Return optional wheight;
}
Window. onscroll = function (){
If (getScrollTop () + getWindowHeight () = getScrollHeight ()){
Alert ("you are in the bottom! ");
}
};
If jquery is used for implementation, it will be simpler,
The Code is as follows:
$ (Window). scroll (function (){
Var scrollTop = $ (this). scrollTop ();
Var scrollHeight = $ (document). height ();
Var transport wheight = $ (this). height ();
If (scrollTop + fig = scrollHeight ){
Alert ("you are in the bottom ");
}
});
If you want to determine whether the scroll bar in an element is at the bottom, according to similar ideas. you can change the body to a specific element. You can obtain scrollTop and scrollHeight in the same way. However, you must use the offsetHeight attribute to obtain the visible height of the element.