How jquery determines that the scroll bar rolls to the bottom of the page and executes the event

Source: Internet
Author: User

Reprinted from the first code original article Http://diyidaima.com/article/detail/g2BtnD7P

First, we understand three DOM elements, namely:clientheight,offsetheight,scrolltop.

clientheight: The height of this element occupies the height of the entire space, so if a DIV has a scrollbar, that height does not include the contents of the following sections that are not shown in the scroll bar. And just the height of the pure div.

offsetheight: Refers to the height of the element's content. According to the above, that height is the height of the div, including the visible part and the invisible part under the scroll bar.

scrolltop: What is this? He can understand the length of the scroll bar that can be scrolled.

For example, if a div height is 400px (that is, ClientHeight is 400), and the content inside is a very long list, the height of the content is 1000px (that is, offsetheight is 1000).

So, the visible part of the 400px,1000px content we see there is 600px is not visible. And the invisible part of it is that we can show this part by pulling the scroll bar. If you do not pull the scrollbar, at this point scrolltop is 0, if you pull the scroll bar to the end, showing the bottom part of the list, at this time, ScrollTop 600. So the value of the ScrollTop range is [0, 600]. So this 600 can be understood as the length of the scroll bar that can be scrolled.

After understanding the above concept. To determine whether scrolling to the bottom is a good thing to do.

First, we pull the scrollbar, pull from the top to the bottom, change the value of the scrolltop, and this value has an interval.

This interval is: [0, (Offsetheight-clientheight)]

That is, the entire process of pulling the scroll bar changes within the range of 0 to (Offsetheight–clientheight).

1, judge the scroll bar scroll to the bottom: scrolltop = = (offsetheight–clientheight)

2. Within 50px of the bottom of the scroll bar: (offsetheight–clientheight) –scrolltop <= 50

3, within the scrollbar distance from the bottom 5%: scrolltop/(offsetheight–clientheight) >= 0.95

Above

If you want to implement pull-to-bottom auto-load content. Just register a scroll bar event:

Scrollbottomtest =function () {
    $ ("#contain"). Scroll (function () {
        var $this =$ (this),
                VIEWH =$ (this). Height (),//Visible height  
                contenth =$ (this). Get (0). scrollheight,//content Height  
                scrolltop =$ (this). ScrollTop ();//Scroll height  
        / /if (contenth-viewh-scrolltop <= 100) {//Reach bottom 100px, load new content  
        if (scrolltop/(CONTENTH-VIEWH) >=0.95) {// When you reach the bottom 100px, load the new content  
            //load the data here.}}
    );
}

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.