页面懒加载
(1)获取body
高度scrollHeight
(2)获取滚动距离scrollY
+ 获取视口高度clientHeight
(3)监听scroll
根据滚动条件发起新的数据请求渲染页面
window.addEventListener("scroll" , function() {
var _this = foo;
var height = document.body.scrollHeight;
var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
if((height - (window.scrollY + clientHeight))<=200 && _this.bool) {
_this.page++;
_this.bool = false;
_this.getMovInfo({page: _this.page});
}
});
var foo = {
bool: true,
page: "1",
getMovInfo: function (params) {
var _this = this;
if(params.page == 1) $("main").html("");
// 数据请求获取新数据
$.get('//xxx.php',params,function(res) {
if(res.data.length==0) return false;
var str = res.data; // 渲染html
$("main").append(str );
_this.bool = true;
});
},
}
foo.getMovInfo({ page: 1 });