1
<!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>Document</title>
<style>
#wrap {
margin: 100px auto;
width: 500px;
height: 500px;
overflow: auto;
padding: 20px;
display: flex;
flex-wrap: wrap;
border: 1px solid #000;
}
.img-box {
width: 200px;
height: 200px;
margin: 20px;
}
img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="wrap"></div>
<script>
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((item) => {
if (item.isIntersecting) {
const el = item.target
el.src = el.dataset.origin
observer.unobserve(el)
}
})
},
{
rootMargin: '0px 0px -100px 0px'
}
)
const wrap = document.getElementById('wrap')
for(let i = 0; i < 1000; i++) {
const el = document.createElement('div')
el.className = 'img-box'
const img = document.createElement('img')
img.dataset.origin = 'https://image.51cheyaoshi.com/carTypeImage/A6/a6L_2023_2.png'
el.appendChild(img)
wrap.appendChild(el)
observer.observe(img)
}
</script>
</body>
</html>
1