Judge the JS Code at the bottom of the scroll bar _ javascript tips-js tutorial

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.