<!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> p { height: 10vh; } </style></head><body> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <img data-src="home-banner.png" alt=""> <img data-src="home-banner1.jpg" alt=""> <img data-src="home-banner2.jpg" alt=""> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <p>LOREM IPSUM DOLOR SIT AMET...</p> <script> const images = document.querySelectorAll('img') const callback = entry => { entry.forEach(entry => { if (entry.isIntersecting) { const image = entry.target; const data_src = image.getAttribute("data-src"); image.setAttribute("src", data_src); observer.unobserve(image); console.log("触发"); } }) } const observer = new IntersectionObserver(callback); images.forEach(image => { observer.observe(image) }) </script></body></html>步调1: