懒加载

var img = Array.from(document.getElementsByTagName('img'))
//获取window 的高度
var windowHeight = window.innerHeight
document.onscroll = function(){
img.forEach(element => {
var offset = element.getBoundingClientRect();
if (offset.top < windowHeight) {
var src = element.getAttribute('data-src'); // data-src
element.src = src;
}
});
}

你可能感兴趣的:(懒加载)