微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
  • 以上所述就是本文的全部内容了,希望大家能够喜欢。

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

    相关推荐