<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>图片懒加载</title>
<style>
img {
display: block;
height: 450px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<img data-src="./images/1.png" alt="" />
<img data-src="./images/2.png" alt="" />
<img data-src="./images/3.png" alt="" />
<img data-src="./images/4.png" alt="" />
<img data-src="./images/5.png" alt="" />
<img data-src="./images/6.png" alt="" />
</body>
<script>
var imgs = document.querySelectorAll("img");
function throttle(func, wait) {
let timer = null;
return function (...args) {
if (!timer) {
func(...args);
timer = setTimeout(() => {
timer = null;
}, wait);
}
};
}
function lazyLoad1(imgs) {
function getTop(e) {
var T = e.offsetTop;
while ((e = e.offsetParent)) {
T += e.offsetTop;
}
return T;
}
var H = document.documentElement.clientHeight;
var S = document.documentElement.scrollTop || document.body.scrollTop;
Array.from(imgs).forEach(function (img) {
if (H + S + 100 > getTop(img) && !img.src) {
img.src = img.dataset.src;
}
});
}
const throttleLazyLoad1 = throttle(lazyLoad1, 200);
function lazyLoad2(imgs) {
function isIn(el) {
var bound = el.getBoundingClientRect();
var clientHeight = window.innerHeight;
return bound.top <= clientHeight + 100;
}
Array.from(imgs).forEach(function (img) {
if (isIn(img) && !img.src) {
img.src = img.dataset.src;
}
});
}
const throttleLazyLoad2 = throttle(lazyLoad2, 200);
function lazyLoad3(imgs) {
const io = new IntersectionObserver((ioes) => {
ioes.forEach((ioe) => {
const img = ioe.target;
const intersectionRatio = ioe.intersectionRatio;
if (intersectionRatio > 0 && intersectionRatio <= 1) {
if (!img.src) {
img.src = img.dataset.src;
}
}
img.onload = img.onerror = () => io.unobserve(img);
});
});
imgs.forEach((img) => io.observe(img));
}
lazyLoad3(imgs);
</script>
</html>