【问题标题】:How to lazy load images?如何延迟加载图像?
【发布时间】:2020-08-12 08:32:55
【问题描述】:

我正在尝试找到一种在我的网站上延迟加载图片的方法,因为我将拥有大量图片,因此我不想遇到任何性能问题。我正在关注在线教程,他们将 src 替换为 data-src ,但这给我留下了一个断开的链接。如果我集成了 lightgallery,是否可以进行这样的延迟加载?

document.addEventListener("DOMContentLoaded", function() {
  var lazyloadImages = document.querySelectorAll("img.lazy");
  var lazyloadThrottleTimeout;

  function lazyload() {
    if (lazyloadThrottleTimeout) {
      clearTimeout(lazyloadThrottleTimeout);
    }

    lazyloadThrottleTimeout = setTimeout(function() {
      var scrollTop = window.pageYOffset;
      lazyloadImages.forEach(function(img) {
        if (img.offsetTop < (window.innerHeight + scrollTop)) {
          img.src = img.dataset.src;
          img.classList.remove('lazy');
        }
      });
      if (lazyloadImages.length == 0) {
        document.removeEventListener("scroll", lazyload);
        window.removeEventListener("resize", lazyload);
        window.removeEventListener("orientationChange", lazyload);
      }
    }, 20);
  }

  document.addEventListener("scroll", lazyload);
  window.addEventListener("resize", lazyload);
  window.addEventListener("orientationChange", lazyload);
});
<div class="row gallery-container" id="lightgallery">
  <div class="col-sm-6 col-md-4 col-lg-4 col-xl-3 item img-gallery center-images-tattoo image-box" data-aos="fade" data-src="images/tattoo/IMG_1086.jpg" data-sub-html="<h4>Fading Light</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor doloremque hic excepturi fugit, sunt impedit fuga tempora, ad amet aliquid?</p>">
    <a href="#"><img data-src="images/tattoo/IMG_1086.jpg" alt="Tattoo-image" class="img-fluid img-resize2 lazy"></a>
  </div>
  <div class="col-sm-6 col-md-4 col-lg-4 col-xl-3 item img-gallery center-images-tattoo image-box" data-aos="fade" data-src="images/tattoo/IMG_1089.jpg" data-sub-html="<h4>Fading Light</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam omnis quaerat molestiae, praesentium. Ipsam, reiciendis. Aut molestiae animi earum laudantium.</p>">
    <a href="#"><img data-src="images/tattoo/IMG_1089.jpg" alt="Tattoo-image" class="img-fluid img-resize2 lazy"></a>
  </div>
  <div class="col-sm-6 col-md-4 col-lg-4 col-xl-3 item img-gallery  center-images-tattoo image-box" data-aos="fade" data-src="images/tattoo/IMG_1090.jpg" data-sub-html="<h4>Fading Light</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem reiciendis, dolorum illo temporibus culpa eaque dolore rerum quod voluptate doloribus.</p>">
    <a href="#"><img data-src="images/tattoo/IMG_1090.jpg" alt="Tattoo-image" class="img-fluid img-resize2 lazy"></a>
  </div>

【问题讨论】:

  • 您需要支持哪种浏览器?您可以使用浏览器实现的lazy 属性吗? web.dev/native-lazy-loading
  • 我可以,但我发现它不支持 safari 和 firefox。
  • 是Firefox实现的,Safari不支持,没错。 caniuse.com/#feat=loading-lazy-attr
  • 我不希望 Safari 上的人体验更差,所以我正在寻找全方位的解决方案。

标签: javascript html css


【解决方案1】:

用 CSS 从 DOM 中隐藏未加载的图像怎么样?

img.lazy {
    display: none;
}

【讨论】:

  • 如果我隐藏它们,它们仍然会在后台加载,这并不能真正解决它。
  • 所以,基本上你想将它们动态添加到 DOM 中。
  • 是的,当用户接近图像时,它开始加载。我在页面末尾有一个画廊,因此在开头加载所有图像没有意义。我用 IntersectionObserver 进行了尝试,但是当我将 src 更改为 data-src 时,链接仍然断开
  • 为了隐藏损坏的图片,我请你使用 CSS。
  • 是的,但是它们不会显示在页面上,因此用户甚至不会知道他们在这里
猜你喜欢
  • 2023-03-07
  • 2016-12-08
  • 1970-01-01
  • 2013-10-24
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多