let img = document.getElementsByTagName("img");
const observer = new IntersectionObserver(entries => {
//entries 是被观察的元素集合
for(let i = 0, len = entries.length; i < len; i++) {
let entries= entries[i];
// 通过这个属性判断是否在视口中
if(entries.isIntersecting) {
const imgElement = entries.target;
imgElement.src = imgElement.getAttribute("data-src");
observer.unobserve(imgElement);
}
}
})
Array.from(img).forEach(item => observer.observe(item));
标签:img,observer,dom,imgElement,视口,let,entries,IntersectionObserver
From: https://blog.csdn.net/weixin_45580774/article/details/141664490