jQuery實現(xiàn)判斷滾動條到底部
判斷滾動條到底部,需要用到DOM的三個屬性值,即scrollTop、clientHeight、scrollHeight。
scrollTop為滾動條在Y軸上的滾動距離。
clientHeight為內(nèi)容可視區(qū)域的高度。
scrollHeight為內(nèi)容可視區(qū)域的高度加上溢出(滾動)的距離。
從這個三個屬性的介紹就可以看出來,滾動條到底部的條件即為scrollTop + clientHeight == scrollHeight。
廢話不多少說,趕緊上代碼(兼容不同的瀏覽器)。
lazyload.js
//滾動條在Y軸上的滾動距離
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;
}
//文檔的總高度
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
$(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
<!doctype html>
<html lang="en" style="height:900px;">
<head>
<meta charset="UTF-8">
<meta name="Author" content="forever">
<link rel="stylesheet" href="css/lazyload.css" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<title>lazyLoad</title>
<script type="text/javascript">
$(function(){
var $ul=$("#lazyLoadWrap").find("ul");
$(window).scroll(function(){
var scrollTop = $(this).scrollTop();
var scrollHeight = $(document).height();
var windowHeight = $(this).height();
if(scrollTop + windowHeight == scrollHeight){
for(var i=0;i<6;i++){
$ul.append("<li>Hello</li>");
}
}
});
});
</script>
</head>
<body>
<div id="lazyLoadWrap">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li>12</li>
</ul>
</div>
</body>
</html>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
jQuery Tags Input Plugin(添加/刪除標(biāo)簽插件)詳解
本文主要介紹jQuery Tags Input Plugin添加/刪除標(biāo)簽插件的用法,非常實用,有需要的朋友可以參考一下。2016-06-06
jQuery插件HighCharts實現(xiàn)的2D面積圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts實現(xiàn)的2D面積圖效果,結(jié)合完整實例形式分析了jQuery插件HighCharts繪制2D面積圖的相關(guān)步驟與屬性設(shè)置技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
jquery單選框radio綁定click事件實現(xiàn)方法
這篇文章主要介紹了jquery單選框radio綁定click事件實現(xiàn)方法,可實現(xiàn)針對單選框radio值的改變作出響應(yīng),非常具有實用價值,需要的朋友可以參考下2015-01-01
jQuery中animate()的使用方法及解決$(”body“).animate({“scrollTop”:top})
這篇文章主要介紹了關(guān)于jQuery中animate()的使用方法及解決$("body").animate({"scrollTop":top})不被Firefox支持的問題,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04

