基于javascript实现浏览器滚动条快到底部时自动加载数据
废话不多说了,直接给大家贴js代码了。
<!DOCTYPEhtml>
<html>
<head>
<scriptsrc="jquery-...js"type="text/javascript"></script>
<scripttype="text/javascript">
$(document).ready(function(){
varrange=;//距下边界长度/单位px
varelemt=;//插入元素高度/单位px
varmaxnum=;//设置加载最多次数
varnum=;
vartotalheight=;
varmain=$("#content");//主体元素
$(window).scroll(function(){
varsrollPos=$(window).scrollTop();//滚动条距顶部距离(页面超出窗口的高度)
//console.log("滚动条到顶部的垂直高度:"+$(document).scrollTop());
//console.log("页面的文档高度:"+$(document).height());
//console.log('浏览器的高度:'+$(window).height());
totalheight=parseFloat($(window).height())+parseFloat(srollPos);//滚动条当前位置距顶部距离+浏览器的高度
if(($(document).height()-totalheight)<=range&&num!=maxnum){//页面底部与滚动条底部的距离<range
main.append("<divstyle='border:pxsolidtomato;margin-top:px;color:#ac"+(num%)+(num%)+";height:"+elemt+"'>helloworld"+srollPos+"---"+num+"</div>");
main.append("<divstyle='border:pxsolidtomato;margin-top:px;color:#ac"+(num%)+(num%)+";height:"+elemt+"'>helloworld"+srollPos+"---"+num+"</div>");
num++;
}
});
});
</script>
</head>
<body>
<divid="content"style="height:px">
<divid="follow">thisisascrolltest;<br/>页面下拉自动加载内容</div>
<divstyle='border:pxsolidtomato;margin-top:px;color:#ac;height:'>helloworldtestDIV</div>
</div>
</body>
</html>
ps:原生JavaScript如何触发滚动条事件?
<script>
window.onscroll=function(){
varscrollT=document.documentElement.scrollTop||document.body.scrollTop;
varscrollH=document.documentElement.scrollHeight||document.body.scrollHeight;
varclientH=document.documentElement.clientHeight||document.body.clientHeight
//console.log(scrollT+"+"+scrollH+"+"+clientH);
if(scrollT==scrollH-clientH){
console.log("到底部了");
}elseif(scrollT==0){
console.log("到顶部了");
}
}
</script>