292 字
1 分钟
图片懒加载的三种实现

三种测试全是用于以下的 html 代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>图片懒加载</title>
</head>
<body>
<div class="container" id="container">
*[Image missing: u=3202947311,1179654885&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500]*
*[Image missing: u=3156137851,1307209439&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500]*
*[Image missing: u=1474625213,1040099858&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500]*
*[Image missing: u=3973060071,1632243021&fm=253&fmt=auto&app=138&f=JPEG?w=890&h=500]*
*[Image missing: u=1428075551,3971081578&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500]*
*[Image missing: u=3573056321,2239143646&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500]*
*[Image missing: u=1003272215,1878948666&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800]*
*[Image missing: u=345670089,3951600800&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500]*
*[Image missing: u=617579813,2960860841&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800]*
*[Image missing: u=45841977,3664621913&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500]*
*[Image missing: u=1273517628,1100314156&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281]*
*[Image missing: u=861863691,2776527252&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500]*
*[Image missing: u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500]*
*[Image missing: u=3151377466,4172354467&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800]*
</div>
<style>
.container {
height: 100%;
overflow: auto;
width: 500px;
}
.container img {
width: 100%;
display: block;
height: 300px;
}
</style>
<script type="module" src="./index.js"></script>
</body>
</html>

使用视口高度和 offsetHeight,scrollTop#

import throttle from "../throttle/throttle.js";
let imgs = document.querySelectorAll("img");
let length = imgs.length;
let count = 0;
function lazyLoadImg() {
let viewH = document.documentElement.clientHeight;
const scrollHeight = document.documentElement.scrollTop;
for (let i = count; i < length; i++) {
const dom = imgs[i];
const domOffsetTop = dom.offsetTop;
if (domOffsetTop <= viewH + scrollHeight) {
const imgSrc = dom.getAttribute("data-src");
dom.setAttribute("src", imgSrc);
count++;
}
}
}
lazyLoadImg();
document.addEventListener("scroll", throttle(lazyLoadImg, 200));

getBoundingClientRect#

import throttle from "../throttle/throttle.js";
let imgs = document.querySelectorAll("img");
let length = imgs.length;
let count = 0;
function lazyLoadImg() {
let viewH = document.documentElement.clientHeight;
for (let i = count; i < length; i++) {
const dom = imgs[i];
const { top } = dom.getBoundingClientRect();
if (top <= viewH) {
const imgSrc = dom.getAttribute("data-src");
dom.setAttribute("src", imgSrc);
count++;
}
}
}
lazyLoadImg();
document.addEventListener("scroll", throttle(lazyLoadImg, 200));

IntersectionObserver#

let imgs = document.querySelectorAll("img");
let length = imgs.length;
const observer = new IntersectionObserver((changes) => {
changes.forEach((change) => {
const { isIntersecting, target } = change;
if (isIntersecting) {
console.log("进来了");
const dataSrc = target.getAttribute("data-src");
target.setAttribute("src", dataSrc);
observer.unobserve(target);
}
});
});
Array.from(imgs).forEach((dom) => {
observer.observe(dom);
});
图片懒加载的三种实现
https://nollieleo.github.io/posts/图片懒加载的三种实现/
作者
翁先森
发布于
2023-02-24
许可协议
CC BY-NC-SA 4.0