JS Judge scroll bar to the bottom

Source: Internet
Author: User

Form:http://www.uphtm.com/js/269.html judge 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.

The code is as follows (compatible with different browsers).

  1. scroll bar on the y-axis of the scrolling distance 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;}  Browser viewport height 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 ("already to the bottom!!"); }};

It's easier to do it with jquery,

    1. $ (window). Scroll (function () {
    2. var scrolltop = $ (this). ScrollTop ();
    3. var scrollheight = $ (document). Height ();
    4. var WindowHeight = $ (this). Height ();
    5. if (scrolltop + windowheight = = scrollheight) {
    6. Alert ("It's already up to the bottom!") ");
    7. }
    8. });

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. Form:http://www.uphtm.com

JS Judge scroll bar to the bottom

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.