笔记 - 图片的加载 & 懒加载

2021-08-13  本文已影响0人  青城墨阕
实现图片的加载
function createImg(url, delay) {
    return new Promise((resolve, reject) => {
        const img = new Image();
        img.src = url;
        setTimeout(() => {
            document.body.appendChild(img);
            resolve();
        }, delay);
    });
}

createImg('https://img2.baidu.com/it/u=575711754,3157460132&fm=26&fmt=auto&gp=0.jpg', 2000)
    .then(() => {
        createImg('https://img2.baidu.com/it/u=2597283190,2423487059&fm=26&fmt=auto&gp=0.jpg', 4000);
    })
    .then(() => {
        createImg('https://img2.baidu.com/it/u=602619791,1793937306&fm=26&fmt=auto&gp=0.jpg', 6000);
    });
image.png
图片的懒加载
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>图片懒加载</title>
  <style>
    img {
      display: block;
      height: 450px;
      margin-bottom: 20px;
    }
  </style>
</head>

<body>
  <img data-src="https://img2.baidu.com/it/u=575711754,3157460132&fm=26&fmt=auto&gp=0.jpg" alt="" />
  <img data-src="https://img2.baidu.com/it/u=2597283190,2423487059&fm=26&fmt=auto&gp=0.jpg" alt="" />
  <img data-src="https://img2.baidu.com/it/u=602619791,1793937306&fm=26&fmt=auto&gp=0.jpg" alt="" />
  <img data-src="https://img0.baidu.com/it/u=1897263140,1647399841&fm=26&fmt=auto&gp=0.jpg" alt="" />
  <img data-src="https://img2.baidu.com/it/u=2787125542,2636933450&fm=26&fmt=auto&gp=0.jpg" alt="" />
  <img data-src="https://img2.baidu.com/it/u=3200334446,1487314372&fm=26&fmt=auto&gp=0.jpg" alt="" />
</body>
<script>
  var imgs = document.querySelectorAll("img");

  // 节流函数,定时器版本
  function throttle(func, wait) {
    let timer = null;
    return function (...args) {
      if (!timer) {
        func(...args);
        timer = setTimeout(() => {
          timer = null;
        }, wait);
      }
    };
  }
</script>
<script>
  //方法1: H + S > offsetTop
  function lazyLoad1(imgs) {
    //offsetTop是元素与offsetParent的距离,循环获取直到页面顶部
    function getTop(e) {
      var T = e.offsetTop;
      while ((e = e.offsetParent)) {
        T += e.offsetTop;
      }
      return T;
    }
    var H = document.documentElement.clientHeight; //获取可视区域高度
    var S = document.documentElement.scrollTop || document.body.scrollTop;
    Array.from(imgs).forEach(function (img) {
      // +100 提前100个像素就开始加载
      // 并且只处理没有src即没有加载过的图片
      if (H + S + 100 > getTop(img) && !img.src) {
        img.src = img.dataset.src;
      }
    });
  }
  const throttleLazyLoad1 = throttle(lazyLoad1, 200);
  // 滚轮事件监听
  window.onload = window.onscroll = function () {
    throttleLazyLoad1(imgs);
  };
</script>
<script>
// 方法2:el.getBoundingClientRect().top <= window.innerHeight
  function lazyLoad2(imgs) {
    function isIn(el) {
      var bound = el.getBoundingClientRect();
      var clientHeight = window.innerHeight;
      return bound.top <= clientHeight + 100;
    }
    Array.from(imgs).forEach(function (img) {
      if (isIn(img) && !img.src) {
        img.src = img.dataset.src;
      }
    });
  }
  const throttleLazyLoad2 = throttle(lazyLoad2, 200);

  // 滚轮事件监听
  window.onload = window.onscroll = function () {
    throttleLazyLoad2(imgs);
  };
</script>
<script>
// 方法3:IntersectionObserver
  function lazyLoad3(imgs) {
    const io = new IntersectionObserver((ioes) => {
      ioes.forEach((ioe) => {
        const img = ioe.target;
        const intersectionRatio = ioe.intersectionRatio;
        if (intersectionRatio > 0 && intersectionRatio <= 1) {
          if (!img.src) {
            img.src = img.dataset.src;
          }
        }
        img.onload = img.onerror = () => io.unobserve(img);
      });
    });
    imgs.forEach((img) => io.observe(img));
  }
  lazyLoad3(imgs);
</script>
只加载两张图片
提前100像素加载图片
上一篇下一篇

猜你喜欢

热点阅读