随笔1
JS getBoundingClientRect()
可以用来实现懒加载
返回一个对象DOMRect,包含以下属性:
bottom: 元素下边界距离浏览器窗口顶部的距离;
height: 元素高度;
top: 元素上边界距离浏览器窗口顶部的距离;
width: 元素宽度;
x: 元素左边界距离浏览器窗口左边的距离;
y: 元素上边界距离浏览器窗口顶部的距离;
大致代码:
var div = document.getElementById("dd");
window.onscroll = function(){
var bound = div.getBoundingClientRect(),
t = bound.top,
clientHeight = window.innerHeight;
if(t<clientHeight+100){
//加载需要的内容...
}
}
clientHeight+100为了提前加载
2999

被折叠的 条评论
为什么被折叠?



