图片懒加载
check(document.querySelectorAll('img.J_lazy'))
function isIn(el) {
var bound = el.getBoundingClientRect();
var clientHeight = window.innerHeight;
return bound.top <= clientHeight;
}
//检查图片是否在可视区内,如果不在,则加载
function check(item) {
Array.from(item).forEach(function(el){
console.log(777)
if(isIn(el)){
loadImg(el);
}
})
}
function loadImg(el) {
if(el.getAttribute('class').indexOf('J_lazy') > -1){
var source = el.dataset.original;
el.src = source;
el.classList.remove("J_lazy");
}
}
function isIn(el) {
var bound = el.getBoundingClientRect();
var clientHeight = window.innerHeight;
return bound.top <= clientHeight;
}
本文标题:图片懒加载
本文链接:https://www.haomeiwen.com/subject/tkesjktx.html
网友评论