IntersectionObserver API兼容性:
部分代码展示:
html
// 懒加载处理
const imgsElem = document.querySelectorAll('img');
const topElem = document.querySelector('#top');
// IntersectionObserver
const Observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
let img = entry.target;
img.src = img.dataset.src
observer.unobserve(img)
}
})
})
// 绑定监听API
imgsElem.forEach((img) => {
Observer.observe(img)
})
具体代码请点击InsCode去详细查看!!!