To determine the scroll bar to the bottom of the JS code _javascript skills

Source: Internet
Author: User

To determine the scroll bar to the bottom, you need to use the DOM's three property values, namely ScrollTop, ClientHeight, ScrollHeight.

ScrollTop is the scroll distance of the scroll bar on the y-axis.

ClientHeight is the height of the content viewable area.

ScrollHeight adds an overflow (scrolling) distance to the height of the content viewable area.

From the introduction of this three attributes can be seen, the scroll bar to the bottom of the condition is scrolltop + clientheight = = ScrollHeight.

Nonsense not much said, hurriedly on the code (compatible with different browsers).


Copy Code code 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.documentelement) {
Documentscrolltop = Document.documentElement.scrollTop;
}
ScrollTop = (bodyscrolltop-documentscrolltop > 0)? Bodyscrolltop:documentscrolltop;
return scrolltop;
}

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 are in the bottom!");
}
};


It's easier if you do it in jquery,
Copy Code code as follows:

$ (window). Scroll (function () {
var scrolltop = $ (this). ScrollTop ();
var scrollheight = $ (document). Height ();
var WindowHeight = $ (this). Height ();
if (scrolltop + windowheight = = scrollheight) {
Alert ("You are in the bottom");
}
});

If you want to determine whether the scroll bar in one element is in the bottom, according to similar ideas, Change the document.body to a specific element, the way to get scrolltop and ScrollHeight is the same, but get the height of the element visible need to use the offsetheight attribute, directly according to gourd painting ladle can.

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.