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

Source: Internet
Author: User

Reproduced from: http://www.diycms.com/edu/jquery/117.html


First understand three DOM elements, respectively: ClientHeight, Offsetheight, scrolltop.

clientheight: The height of this element takes up the height of the entire space, so if a DIV has a scroll bar, that height is not including the contents of the following section that the scroll bar does not show. And just the sheer height of the div.

offsetheight: Refers to the height of the element content. According to the above, that height is the height of the div inside, 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 contents are a very long list, the content is 1000px (that is, offsetheight is 1000). So, the visible part of the 400px,1000px content we see is still 600px invisible. And the invisible part of it is that we pull the scroll bar to show this part. If you do not pull the scroll bar, this time scrolltop is 0, if you pull the scroll bar to the end, showing the bottom of the list, at this point, ScrollTop 600. So 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. It's a good thing to decide whether to scroll to the bottom.

First, we pull the scroll bar from the top to the bottom and change the value of the scrolltop, which has an interval.

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

That is, the change in the entire process of the scrollbar pull is within 0 to (offsetheight–clientheight) range.

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

2, within the scroll bar distance from the bottom 50px: (offsetheight–clientheight) –scrolltop <= 50

3, in the scroll bar distance from the bottom 5%: scrolltop/(offsetheight–clientheight) >= 0.95

Above

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

Scrollbottomtest =function () {
     $ ("#contain"). Scroll (function () {
         var $this =$ (this),
         VIEWH =$ (this). Height (),//Visible Heights Contenth =$ (this). Get
         (0). scrollheight,//content Height
         scrolltop =$ (this). ScrollTop ()
        //Scroll height/ /if (contenth-viewh-scrolltop <= 100) {//When reaching bottom 100px, load new content
        if (scrolltop/(CONTENTH-VIEWH) >=0.95) {// When the bottom 100px arrives, load the new content
        //Load 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.