<img src="image.jpg" alt="描述" loading="lazy" width="800" height="600">
<iframe src="video.html" loading="lazy"></iframe>
支援:Chrome 76+、Firefox 75+、Edge 79+、Safari 15.4+。
特點:瀏覽器自動計算閾值、無 JS 依賴、無法自定義觸發邏輯。
const lazyImages = document.querySelectorAll('img[data-src]');
const observer = new IntersectionObserver((entries, obs) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.srcset = img.dataset.srcset || '';
img.removeAttribute('data-src');
img.removeAttribute('data-srcset');
obs.unobserve(img); // 載入後停止觀察
}
});
}, {
rootMargin: '200px 0px', // 提前 200px 開始載入
threshold: 0.01 // 1% 可見即觸發
});
lazyImages.forEach(img => observer.observe(img));
<img
data-src="image.jpg"
data-srcset="image-400w.jpg 400w, image-800w.jpg 800w"
alt="懶載入圖片"
width="800" height="600"
class="lazy-img">
<style>
.lazy-img {
opacity: 0;
transition: opacity 0.3s;
}
.lazy-img.loaded { opacity: 1; }
</style>
<script>
observer.observe(img);
img.onload = () => img.classList.add('loaded');
</script>
注意:原作者已停止維護,新專案請用方案一或二。
<script src="/js/jquery.min.js"></script>
<script src="/js/jquery.lazyload.min.js"></script>
<script>
$(function() {
$('.content-area img').lazyload({
effect: 'fadeIn', // 淡入效果:show | fadeIn | slideDown
threshold: 200, // 提前 200px 載入
placeholder: 'img/grey.gif', // 佔位圖(1px 灰圖或骨架屏)
event: 'scroll', // 觸發事件:scroll | click | mouseover
container: window, // 滾動容器
failure_limit: 10, // 容亂序:繼續檢查後 10 個元素
skip_invisible: true // 跳過隱藏圖片
});
});
</script>
<!-- 原來 -->
<img src="real-image.jpg" alt="...">
<!-- 改為 -->
<img class="lazy"
data-original="real-image.jpg"
src="img/grey.gif"
alt="..."
width="800" height="600">
| 參數 | 類型 | 預設 | 說明 |
|---|---|---|---|
| threshold | Number | 0 | 提前載入距離(px)。正值=提前,負值=延遲 |
| effect | String | 'show' | 顯示動畫:show/fadeIn/slideDown |
| placeholder | String | 無 | 佔位圖 URL,建議 1px Base64 透明圖 |
| event | String | 'scroll' | 觸發方式:scroll/click/mouseover/sporty |
| container | jQuery/Element | window | 指定滾動容器(如側邊欄滾動) |
| failure_limit | Number | 0 | HTML 亂序時,額外檢查後 N 個元素 |
| data_attribute | String | 'original' | 數據屬性名:data-original / data-src |
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
data-src="real.jpg" alt="...">
<img src="data:image/svg+xml,%3Csvg xmlns='https://www.w3.org/2000/svg' viewBox='0 0 800 600'%3E%3C/svg%3E"
data-src="real.jpg" alt="...">
<div class="skeleton-image"></div>
<style>
.skeleton-image {
aspect-ratio: 4/3;
background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }
</style>
alt 屬性:螢幕閱讀器、SEO、圖片加載失敗時顯示width/height 屬性:預留佈局空間,防止 CLS<noscript> 回退:禁用 JS 環境下仍能看到圖片/* 統一策略:原生優先 + 降級增強 */
img:not([loading]) {
/* 針對不支援 loading 的舊瀏覽器,可配合 IntersectionObserver Polyfill */
}
/* 關鍵圖片強制預載 */
img.hero, img.logo, img.lcp-candidate {
loading: eager; /* 覆蓋全域 lazy */
}
/* CMS 輸出時自動添加 loading="lazy" 並補全 width/height */