jQuery实现判断滚动条到底部

判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop、clientHeight、scrollHeight。

scrollTop为滚动条在Y轴上的滚动距离。

clientHeight为内容可视区域的高度。

scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。

从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。

废话不多少说,赶紧上代码(兼容不同的浏览器)。

lazyload.js

0) ? bodyScrollTop : documentScrollTop;   return scrollTop; } //文档的总高度 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; } //浏览器视口的高度 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!");   } };

lazyload-jQuery.js

rush:js;"> $(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");   } });

lazyLoad.html

rush:js;"> <Meta charset="UTF-8"> <Meta name="Author" content="forever">
  • 12
  • 以上所述就是本文的全部内容了,希望大家能够喜欢。

    相关文章

    页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
    jQuery实时显示日期、时间 html: &lt;span id=&quot...
    jQuery 添加水印 &lt;script src=&quot;../../../.....
    中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
    1. 用Response.Write方法 代码如下: Response.Write(&q...
    Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...