1.getBoundingClientRect 实现
1. 在img 元素上绑定一个data-original属性,存放src。
2. 获取视图高度,通过documentElement.clientHeight;
3. getBoundingClientRect().top<视图高度
4. item.src = item.dataset.original
5. item.removeAttribute('data-original')属性。
function lazyLoadFunc() {
console.log('lazyLoadFunc_start');
let elementArr = document.querySelectorAll("img[data-original]");
let vieHight = document.documentElement.clientHeight;
Array.prototype.forEach.call(elementArr, (item) => {
let rect = item.getBoundingClientRect();
if(rect.bottom>=0&&rect.top<vieHight){
let img = new Image();
img.src = item.dataset.original;
item.src = img.src;
item.removeAttribute('data-original');
}
});
}
2.offsetTop + scrollTop实现
1、2、4、5一样,仅2判断有区别
let scrollTop = document.body.scrollTop||document.documentElement.scrollTop;
item.offsetTop<=scrollTop+document.documentElement.clientHeight
IntersectionObserver
IntersectionObserver的作用是监听目标元素与祖先元素是否有相交(默认是浏览器的视口)
const config = {
root: null,
rootMargin: "0px 0px 0px 0px",
threshold: "0",
};
let observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.src = entry.target.dataset.original;
observer.unobserve(entry.target);
}
});
}, config);
useEffect(() => {
const imgs = document.querySelectorAll("img");
Array.prototype.forEach.call(imgs,(img)=>{
observer.observe(img);
})
}, []);