jquery-lazyload延遲加載圖片 及 加載順序 bug 修復


jquery-lazyload延遲加載圖片

 
代碼修改片段

function update() {
var counter = 0;

/**fix by weiyj start***/
elements.sort(function(a,b){
var value=$(a).offset().top-$(b).offset().top;
if(value==0){
value=$(a).offset().left-$(b).offset().left;
}
return value;
});
/**fix by weiyj end ***/

 
 
 

下載地址:https://github.com/tuupola/jquery_lazyload
用法:
頭部引用
<script src="jquery.js" type="text/javascript"></script> <script src="jquery.lazyload.js" type="text/javascript"></script>

body引用
修改 HTML 代碼中需要延遲加載的 IMG 標簽
<!-- 
將真實圖片地址寫在 data-original 屬性中,而 src 屬性中的圖片換成占位符的圖片(例如 1x1 像素的灰色圖片或者 loading 的 gif 圖片) 
添加 class="lazy" 用於區別哪些圖片需要延時加載,當然你也可以換成別的關鍵詞,修改的同時記得修改調用時的 jQuery 選擇器 
添加 width 和 height 屬性有助於在圖片未加載時占滿所需要的空間 
--> 
<img class="lazy" src="grey.gif" data-original="example.jpg" width="640" heigh="480"

調用
$("img.lazy").lazyload();
 

參數說明

名稱 默認值 說明
container window 父容器。延遲加載父容器中的圖片。 [Demo1] [Demo2]
event 'scroll' 觸發加載的事件 [Demo]
effect 'show' 加載使用的動畫效果,如 show, fadeIn, slideDown 等 jQuery 自帶的效果,或者自定義動畫。 [Demo]
effectspeed undefined 動畫時間。作為 effect 的參數使用:effect(effectspeed)
data_attribute 'original' 真實圖片地址的 data 屬性后綴
threshold 0 靈敏度。默認為 0 表示當圖片出現在顯示區域中的立即加載顯示;設為整數表示圖片距離 x 像素進入顯示區域時進行加載;設為負數表示圖片進入顯示區域 x 像素時進行加載。
failure_limit 0 容差范圍。頁面滾動時,Lazy Load 會遍歷延遲加載的圖片,檢查是否在顯示區域內,默認找到第 1 張不可見的圖片時,就終止遍歷。因為 Lazy Load 認為圖片的排序是與 HTML 中的代碼中的排序相同,但是也可能會出現例外,通過該值來擴大容差范圍。
skip_invisible true 跳過隱藏的圖片。圖片不可見時(如 display:none),不強制加載。
appear null 圖片加載時的事件 (Function),有 2 個參數:elements_left(未加載的圖片數量)、settings(lazyload 的參數)。[Demo](參考 DEMO 的源代碼)
load null 圖片加載后的事件 (Function),有 2 個參數,同 appear 。[Demo](參考 DEMO 的源代碼)


在容器中使用:
#container {
    height: 600px;
    overflow: scroll;
}
$("img.lazy").lazyload({
     container: $("#container")
});
實例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<title>Lazy Load Enabled With AJAX Content</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<style type="text/css">
.container img {
margin-bottom: 10px;
}

#container {
width: 800px;
overflow: scroll;
}

#inner_container {
width: 4750px;
}
</style>

</head>
<body>
<div id="container">
<div id="inner_container">
<img class="lazy" src="img/loading_16.gif" data-original="img/bmw_m1_hood.jpg" width="765" height="574" alt="BMW M1 Hood">
<img class="lazy" src="img/loading_16.gif" data-original="img/bmw_m1_side.jpg" width="765" height="574" alt="BMW M1 Side">
<img class="lazy" src="img/loading_16.gif" data-original="img/viper_1.jpg" width="765" height="574" alt="Viper 1">
<img class="lazy" src="img/loading_16.gif" data-original="img/viper_corner.jpg" width="765" height="574" alt="Viper Corner">
<img class="lazy" src="img/loading_16.gif" data-original="img/bmw_m3_gt.jpg" width="765" height="574" alt="BMW M3 GT">
<img class="lazy" src="img/loading_16.gif" data-original="img/corvette_pitstop.jpg" width="765" height="574" alt="Corvette Pitstop">
</div>
</div>


<script src="jquery-1.11.3.min.js"></script>
<script src="jquery.lazyload.js?v=1.9.7"></script>
<script>

$(function() {
$("img.lazy").lazyload({
effect : "fadeIn",
container: $("#container"),
threshold : -200
});
});

</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM