html懒人加载实现

2023-12-18 10:06:47

在HTML中,懒加载(Lazy Load)是一种延迟加载图片或其他资源的技术,它可以提高页面的加载速度和性能。下面是一种实现懒加载的方法:

  1. 设置默认占位图片:在HTML中,为要延迟加载的图片设置一个默认的占位符图片,它将在图片加载之前显示。
    <img src="placeholder.jpg" data-src="image.jpg" alt="Lazy Load Image">

  2. 添加JavaScript代码:为了实现懒加载,我们需要使用JavaScript来检测图片是否在视口(Viewport)中可见,如果是则加载实际图片。
    document.addEventListener("DOMContentLoaded", function() {
      var lazyImages = [].slice.call(document.querySelectorAll("img[data-src]"));
      if ("IntersectionObserver" in window) {
        var lazyImageObserver = new IntersectionObserver(function(entries, observer) {
          entries.forEach(function(entry) {
            if (entry.isIntersecting) {
              var lazyImage = entry.target;
              lazyImage.src = lazyImage.dataset.src;
              lazyImage.classList.remove("lazy");
              lazyImageObserver.unobserve(lazyImage);
            }
          });
        });
        lazyImages.forEach(function(lazyImage) {
          lazyImageObserver.observe(lazyImage);
        });
      } else {
        // 如果浏览器不支持Intersection Observer,则简单地将所有的图片加载
        lazyImages.forEach(function(lazyImage) {
          lazyImage.src = lazyImage.dataset.src;
          lazyImage.classList.remove("lazy");
        });
      }
    });

    此JavaScript代码使用Intersection Observer API(如果浏览器支持)来观察图片是否在视口中可见。如果图片可见,则将实际图片的URL赋值给src属性,并从lazy类中移除。如果浏览器不支持Intersection Observer,那么它会简单地将所有图片加载出来。 3. 样式调整:为了更好地展示懒加载效果,你可以为占位符图片和实际图片添加一些样式。

    img {
      max-width: 100%;
      height: auto;
      display: block;
    }
    .lazy {
      filter: blur(5px); /* 可选:模糊效果 */
      opacity: 0.5; /* 可选:透明度降低 */
    }

    以上是一种实现懒加载的方法。当页面滚动或重新调整大小时,图片将根据它们在视口中的可见性自动加载。 请注意,懒加载并不局限于图片,你也可以将它应用于其他需要延迟加载的资源,比如视频或音频文件

? ?完整代码

<!DOCTYPE html>
<html>
<head>
    <title>Lazy Load</title>
    <style>
        img {
            max-width: 100%;
            height: auto;
            display: block;
        }
        .lazy {
            filter: blur(5px); /* 可选:模糊效果 */
            opacity: 0.5; /* 可选:透明度降低 */
        }
    </style>
</head>
<body>
<h1>Lazy Load Example</h1>
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img1.baidu.com/it/u=422078137,1307526884&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img2.baidu.com/it/u=2546424698,318222608&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img1.baidu.com/it/u=422078137,1307526884&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img1.baidu.com/it/u=422078137,1307526884&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img2.baidu.com/it/u=2546424698,318222608&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img1.baidu.com/it/u=3622442929,3246643478&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img1.baidu.com/it/u=422078137,1307526884&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img2.baidu.com/it/u=2546424698,318222608&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="Lazy Load Image" class="lazy">
<img src="https://preview.qiantucdn.com/58pic/36/28/63/18558PICu3b842hxJc6mW_PIC2018.gif!w1024_new_small" data-src="https://img2.baidu.com/it/u=2312383180,3750420672&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="Lazy Load Image" class="lazy">

<script>
    document.addEventListener("DOMContentLoaded", function() {
        var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
        if ("IntersectionObserver" in window) {
            var lazyImageObserver = new IntersectionObserver(function(entries, observer) {
                entries.forEach(function(entry) {
                    if (entry.isIntersecting) {
                        var lazyImage = entry.target;
                        lazyImage.src = lazyImage.dataset.src;
                        lazyImage.classList.remove("lazy");
                        lazyImageObserver.unobserve(lazyImage);
                    }
                });
            });
            lazyImages.forEach(function(lazyImage) {
                lazyImageObserver.observe(lazyImage);
            });
        } else {
            lazyImages.forEach(function(lazyImage) {
                lazyImage.src = lazyImage.dataset.src;
                lazyImage.classList.remove("lazy");
            });
        }
    });
</script>
</body>
</html>

实现效果

文章来源:https://blog.csdn.net/weixin_39934453/article/details/135054339
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。