const intersectionObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(item => {
const { intersectionRatio, target } = item
// intersectionRatio
为目标元素的可见比例,大于0
代表可见
if (intersectionRatio) {
observer.unobserve(target);
const src = target.getAttribute('lazy');
target.src = src;
}
});
});
document.querySelectorAll('img[lazy]').forEach((el) => intersectionObserver.observe(el))
标签:src,const,target,检测,元素,可见,IntersectionObserver,intersectionRatio From: https://www.cnblogs.com/listen80/p/16750281.html