我们有一个用户选择云环境下所有端口展示的方案,因为太多了(上万),一次性加载页面要等5s以上,考虑延迟加载,写了个小demo
代码长这个样子,我是用angular2.0
<div class="father" style="height:100px;overflow:auto;">
<div class="child" style='border:1px solid red;margin-top:20px;color:grey;height:200px' ></div>
</div>
ngAfterViewInit() {
let that = this;
$(".father").bind("scroll",function(e){
var sum =this.scrollHeight;
if(sum <= $(this).scrollTop()+$(this).height()){
$(".father").append($(".child").clone());
}
})
}
}
我也来解释下下面几个参数
100px的框包含了220px的内容,有滚动条
scrollHeight :就是220px,我理解就是文档的全部高度
scrollTop:滚动条的滚动距离
height:断点看是198(上下1px padding),就是father的高度
那么就是实际窗口高度+滚动条滚动距离 = 文档的全部高度,说明到底了,这时候加载(或者不到底加载,那么再给文档全部高度减去一个自定义常量)