Skip to content
html
<!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>
    html, body {
      height: 500%;
    }

    div {
      width: 200px;
      height: 200px;
      background-color: #ddd;
      margin: 0 auto;
      margin-top: 1000px;
    }

    div img {
      width: 100%;
      height: 100%;
    }
  </style>
</head>
<body>
  <!-- 
    未来设想:loading='lazy', 目前兼容性不好
    lazy: 最后加载
    eager:立即加载,默认值
  -->

  <div>
    <img src="./demo.png" alt="" loading='lazy'>
  </div>

  <div>
    <img src="./demo2.webp" alt="" loading=''>
  </div>

  <div>
    <img src="./demo3.webp" alt="" loading='eager'>
  </div>

  <script>
    const imgs = document.querySelectorAll('img')
    for (let i = 0; i < imgs.length; i++) {
      imgs[i].onload = () => {
        console.log(`第` + (i + 1) + `张图片加载成功`)
      }
    }

    // 判断浏览器是否支持
    const isSupport = 'loading' in HTMLImageElement.prototype;
    console.log(isSupport)
  </script>
</body>
</html>