基于javascript实现浏览器滚动条快到底部时自动加载数据
作者:bea
废话不多说了,直接给大家贴js代码了。 <!DOCTYPE html> <html> <head> <script src="jquery-...js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { var range = ;
废话不多说了,直接给大家贴js代码了。
<!DOCTYPE html>
<html>
<head>
<script src="jquery-...js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var range = ; //距下边界长度/单位px
var elemt = ; //插入元素高度/单位px
var maxnum = ; //设置加载最多次数
var num = ;
var totalheight = ;
var main = $("#content"); //主体元素
$(window).scroll(function () {
var srollPos = $(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("<div style='border:px solid tomato;margin-top:px;color:#ac" + (num % ) + (num % ) + ";height:" + elemt + "' >hello world" + srollPos + "---" + num + "</div>");
main.append("<div style='border:px solid tomato;margin-top:px;color:#ac" + (num % ) + (num % ) + ";height:" + elemt + "' >hello world" + srollPos + "---" + num + "</div>");
num++;
}
});
});
</script>
</head>
<body>
<div id="content" style="height:px">
<div id="follow">this is a scroll test;<br /> 页面下拉自动加载内容</div>
<div style='border:px solid tomato;margin-top:px;color:#ac;height:'>hello world test DIV</div>
</div>
</body>
</html>
ps:原生JavaScript如何触发滚动条事件?
<script>
window.onscroll = function(){
var scrollT = document.documentElement.scrollTop||document.body.scrollTop;
var scrollH = document.documentElement.scrollHeight||document.body.scrollHeight;
var clientH = document.documentElement.clientHeight||document.body.clientHeight
//console.log(scrollT +"+"+scrollH+"+"+clientH);
if(scrollT == scrollH - clientH){
console.log("到底部了");
}else if(scrollT == 0){
console.log("到顶部了");
}
}
</script>
有用 | 无用
<!DOCTYPE html>
<html>
<head>
<script src="jquery-...js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var range = ; //距下边界长度/单位px
var elemt = ; //插入元素高度/单位px
var maxnum = ; //设置加载最多次数
var num = ;
var totalheight = ;
var main = $("#content"); //主体元素
$(window).scroll(function () {
var srollPos = $(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("<div style='border:px solid tomato;margin-top:px;color:#ac" + (num % ) + (num % ) + ";height:" + elemt + "' >hello world" + srollPos + "---" + num + "</div>");
main.append("<div style='border:px solid tomato;margin-top:px;color:#ac" + (num % ) + (num % ) + ";height:" + elemt + "' >hello world" + srollPos + "---" + num + "</div>");
num++;
}
});
});
</script>
</head>
<body>
<div id="content" style="height:px">
<div id="follow">this is a scroll test;<br /> 页面下拉自动加载内容</div>
<div style='border:px solid tomato;margin-top:px;color:#ac;height:'>hello world test DIV</div>
</div>
</body>
</html>
ps:原生JavaScript如何触发滚动条事件?
<script>
window.onscroll = function(){
var scrollT = document.documentElement.scrollTop||document.body.scrollTop;
var scrollH = document.documentElement.scrollHeight||document.body.scrollHeight;
var clientH = document.documentElement.clientHeight||document.body.clientHeight
//console.log(scrollT +"+"+scrollH+"+"+clientH);
if(scrollT == scrollH - clientH){
console.log("到底部了");
}else if(scrollT == 0){
console.log("到顶部了");
}
}
</script>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- jQuery学习笔记之Ajax用法实例详解
- 易操作的jQuery表单提示插件
- JavaScript保留关键字汇总
- 轻松学习jQuery插件EasyUI EasyUI表单验证
- 整理Javascript函数学习笔记
- 全面解析Bootstrap弹窗的实现方法
- js获取及判断键盘按键的方法
- Eclipse引入jquery报错如何解决
- JS验证邮件地址格式方法小结
- 基于JavaScript操作DOM常用的API小结
- 详解JavaScript函数
- javascript定义类和类的实现实例详解
- 深入JavaScript高级程序设计之对象、数组(栈方法,队列方法,重排序方法,迭代方法)
- js跨域请求数据的3种常用的方法
- jQuery插件实现多级联动菜单效果
- 基于Jquery实现焦点图淡出淡入效果
- 轻松学习jQuery插件EasyUI EasyUI创建RSS Feed阅读器
- 轻松学习jQuery插件EasyUI EasyUI创建树形菜单
- 轻松学习jQuery插件EasyUI EasyUI实现树形网络基本操作(2)