当前位置: 首页 > 工具软件 > ng-lazy-image > 使用案例 >

img lazyload chrome

戴博
2023-12-01

chrome 75 之后,已经支持 img lazy-load 了,以后可以不用使用js 的库来实现了

<script>
  // 判断浏览器是否支持
  const hasSupport = 'loading' in HTMLImageElement.prototype;
  document.documentElement.className = hasSupport ? 'pass' : 'fail';
  
</script>

<!-- Lazy-load an offscreen image when the user scrolls near it -->
<img src="unicorn.jpg" loading="lazy" alt=".."/>

立刻加载图片
<!-- Load an image right away instead of lazy-loading -->
<img src="unicorn.jpg" loading="eager" alt=".."/>

由浏览器决定是否懒加载
<!-- Browser decides whether or not to lazy-load the image -->
<img src="unicorn.jpg" loading="auto" alt=".."/>

<!-- Lazy-load images in <picture>. <img> is the one driving image 
loading so <picture> and srcset fall off of that -->
<picture>
  <source media="(min-width: 40em)" srcset="big.jpg 1x, big-hd.jpg 2x">
  <source srcset="small.jpg 1x, small-hd.jpg 2x">
  <img src="fallback.jpg" loading="lazy">
</picture>

<!-- Lazy-load an image that has srcset specified -->
<img src="small.jpg"
     srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w"
     sizes="(min-width: 36em) 33.3vw, 100vw"
     alt="A rad wolf" loading="lazy">

<!-- Lazy-load an offscreen iframe when the user scrolls near it -->
<iframe src="video-player.html" loading="lazy"></iframe>

img lazy demo

原文

 类似资料:

相关阅读

相关文章

相关问答