圖片延遲加載,又稱 Lazy Load,常用於圖片較多的長頁面。它的概念是:只有當圖片即將進入視窗時,才真正載入,藉此降低首屏負擔。
基本用法
引入 jQuery 與 jquery.lazyload.js 後,對 img 元素啟用即可:
$("img").lazyload();
常見參數
- threshold:設定提前載入的距離,例如 200 像素。
- placeholder:指定佔位圖,例如 https:///keleyi/phtml/image/img/grey.gif。
- event:自訂觸發事件,例如 click。
- effect:設定顯示效果,例如 fadeIn。
範例圖片素材
原始範例使用以下圖片網址:
- https:///keleyi/phtml/image/img/bmw_m1_hood.jpg
- https:///keleyi/phtml/image/img/bmw_m1_side.jpg
- https:///keleyi/phtml/image/img/viper_1.jpg
- https:///keleyi/phtml/image/img/viper_corner.jpg
- https:///keleyi/phtml/image/img/bmw_m3_gt.jpg
- https:///keleyi/phtml/image/img/corvette_pitstop.jpg
注意事項
延遲加載能改善頁面載入速度,但也可能與部分 Ajax 流程不相容。若圖片高度未事先定義,可能造成版面跳動,因此建議保留寬高或預留空間。