我有一些负责图像延迟加载的 js 代码。但如果我们也用 picture 标签包裹 img,并且针对 webp、avif 格式的图片添加 source 标签,那么是否也需要对 source 标签做延迟加载呢?或者如果您使用图像的延迟加载,您可以不用图片标签和 webp、avif 格式吗?
<picture>
<source srcset="human-back.avif" type="image/avif">
<img class="lazy first" data-src="human-back.avif" src="pixel.png" alt="">
</picture>
const Images = document.querySelectorAll('img[data-src]');
function loadImages(entries) {
if (entries[0].isIntersecting) {
entries[0].target.src = entries[0].target.dataset.src
observer.unobserve(entries[0].target)
}
}
const options = {
threshold: 0,
rootMargin: '50px'
}
const observer = new IntersectionObserver(loadImages, options)
Images.forEach(img => {
observer.observe(img)
})