jQuery lazyload 的重復(fù)加載錯(cuò)誤以及修復(fù)方法
更新時(shí)間:2010年11月19日 16:52:34 作者:
jQuery lazyload是一款延遲加載圖片的的插件,原意是按需加載,當(dāng)圖片出現(xiàn)在可視區(qū)域時(shí)進(jìn)行加載,但是官方的插件經(jīng)過firebug的檢測可知,并不能節(jié)省流量開支,反而有重復(fù)加載的現(xiàn)象。
分析代碼也可以知道。
最主要的原因是 寫在頁面上的 <img src="the_big_img_toLoad.jpg" />一經(jīng)加載,就去向服務(wù)器申請圖片地址,加載大圖片。
如果想實(shí)現(xiàn)原定的效果,則 寫在頁面上的待加載地址 必須不能是大圖片的地址,而我們可以將真正的圖片地址數(shù)據(jù) 存儲在alt屬性中。
正確的例子如下:
<a href="#nogo"><img alt="http://www.dhdzp.com/comstyles/img200-150-3.jpg" src="http://sc.jb51.net/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.dhdzp.com/comstyles/img200-150-4.jpg" src="http://sc.jb51.net/style/img/spacer.gif" alt="200-150" /></a>
對原來的 jquery.lazyload.js我們也需要做一點(diǎn)改動(dòng):
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="lazyload.js"></script>
<script type="text/javascript"> //初始化代碼
$(document).ready(function(){
$("#lazy1 img,#lazy2 img").lazyload({
placeholder : "http://sc.jb51.net/style/img/spacer.gif",
effect : "fadeIn"
});
});
</script>
完整實(shí)例如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</title>
<style type="text/css">
img{ display:block; border:2px solid #ccc; margin:0 0 10px;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="lazyload.js"></script>
<script type="text/javascript"> //初始化代碼
$(document).ready(function(){
$("#lazy1 img,#lazy2 img").lazyload({
placeholder : "http://qsl.cn/style/img/spacer.gif",
effect : "fadeIn"
});
});
</script>
</head>
<body>
<div style=" height:900px; width:400px; background:#eee;">
<a href="#nogo"><img src="http://www.titan24.com/comstyles/img200-150-2.jpg" alt="200-150" /></a>
第一屏高度為900px,滾動(dòng)到下面的時(shí)候,相應(yīng)圖片才開始加載
</div>
<div id="lazy1" style="width:350px; margin-bottom:340px;">
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/ad400-300.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="400-300" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-1.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-2.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
</div>
<div id="lazy2" style="width:350px;">
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-3.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-4.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-1.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-2.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-3.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-4.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
</div>
</body>
</html>
測試可知 是真正的按需加載。而不是像之前,先加載了,滾動(dòng)到相應(yīng)位置 還又加載了一次。
Firebug的眼睛還是雪亮的。
最主要的原因是 寫在頁面上的 <img src="the_big_img_toLoad.jpg" />一經(jīng)加載,就去向服務(wù)器申請圖片地址,加載大圖片。
如果想實(shí)現(xiàn)原定的效果,則 寫在頁面上的待加載地址 必須不能是大圖片的地址,而我們可以將真正的圖片地址數(shù)據(jù) 存儲在alt屬性中。
正確的例子如下:
復(fù)制代碼 代碼如下:
<a href="#nogo"><img alt="http://www.dhdzp.com/comstyles/img200-150-3.jpg" src="http://sc.jb51.net/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.dhdzp.com/comstyles/img200-150-4.jpg" src="http://sc.jb51.net/style/img/spacer.gif" alt="200-150" /></a>
對原來的 jquery.lazyload.js我們也需要做一點(diǎn)改動(dòng):
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="lazyload.js"></script>
<script type="text/javascript"> //初始化代碼
$(document).ready(function(){
$("#lazy1 img,#lazy2 img").lazyload({
placeholder : "http://sc.jb51.net/style/img/spacer.gif",
effect : "fadeIn"
});
});
</script>
完整實(shí)例如下:
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</title>
<style type="text/css">
img{ display:block; border:2px solid #ccc; margin:0 0 10px;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="lazyload.js"></script>
<script type="text/javascript"> //初始化代碼
$(document).ready(function(){
$("#lazy1 img,#lazy2 img").lazyload({
placeholder : "http://qsl.cn/style/img/spacer.gif",
effect : "fadeIn"
});
});
</script>
</head>
<body>
<div style=" height:900px; width:400px; background:#eee;">
<a href="#nogo"><img src="http://www.titan24.com/comstyles/img200-150-2.jpg" alt="200-150" /></a>
第一屏高度為900px,滾動(dòng)到下面的時(shí)候,相應(yīng)圖片才開始加載
</div>
<div id="lazy1" style="width:350px; margin-bottom:340px;">
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/ad400-300.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="400-300" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-1.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-2.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
</div>
<div id="lazy2" style="width:350px;">
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-3.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img200-150-4.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-1.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-2.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-3.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
<a href="#nogo"><img alt="http://www.titan24.com/comstyles/img560-300-4.jpg" src="http://qsl.cn/style/img/spacer.gif" alt="200-150" /></a>
</div>
</body>
</html>
測試可知 是真正的按需加載。而不是像之前,先加載了,滾動(dòng)到相應(yīng)位置 還又加載了一次。
Firebug的眼睛還是雪亮的。
您可能感興趣的文章:
- jQuery.lazyload+masonry改良圖片瀑布流代碼
- Jquery圖片延遲加載插件jquery.lazyload.js的使用方法
- jquery插件lazyload.js延遲加載圖片的使用方法
- jQuery lazyLoad圖片延遲加載插件的優(yōu)化改造方法分享
- Jquery.LazyLoad.js修正版下載,實(shí)現(xiàn)圖片延遲加載插件
- jquery lazyload延遲加載技術(shù)的實(shí)現(xiàn)原理分析
- 修改jquery.lazyload.js實(shí)現(xiàn)頁面延遲載入
- jquery.lazyload 實(shí)現(xiàn)圖片延遲加載jquery插件
- 詳解jQuery lazyload 懶加載
相關(guān)文章
jquery 獲取 outerHtml 包含當(dāng)前節(jié)點(diǎn)本身的代碼
jQuery.html() 是獲取當(dāng)前節(jié)點(diǎn)下的html代碼,并不包含當(dāng)前節(jié)點(diǎn)本身的代碼,后來實(shí)驗(yàn)發(fā)現(xiàn)有一個(gè)jQuery的一個(gè)方法可以解決2014-10-10
jquery checkbox 勾選的bug問題解決方案與分析
本文首先由一個(gè)在項(xiàng)目中遇到的jquery checkbox 勾選的bug的解決方案,引申出jQuery中attr()和prop()的差異分析,非常的實(shí)用,需要的小伙伴快來研究下吧2014-11-11
JQuery獲取元素尺寸、位置及頁面滾動(dòng)事件應(yīng)用示例
這篇文章主要介紹了JQuery獲取元素尺寸、位置及頁面滾動(dòng)事件應(yīng)用,結(jié)合實(shí)例形式分析了jQuery針對頁面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,并給出了購物車動(dòng)畫效果案例進(jìn)行總結(jié),需要的朋友可以參考下2019-05-05
利用jQuery.Validate異步驗(yàn)證用戶名是否存在(推薦)
這篇文章主要介紹了利用jQuery.Validate異步驗(yàn)證用戶名是否存在的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
jQuery 行背景顏色的交替顯示(隔行變色)實(shí)現(xiàn)代碼
主要是利用了jquery的attr為行添加樣式來實(shí)現(xiàn)的,具體的代碼如下。2009-12-12
jquery Ajax實(shí)現(xiàn)Select動(dòng)態(tài)添加數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了jquery Ajax實(shí)現(xiàn)Select動(dòng)態(tài)添加數(shù)據(jù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
JQuery中DOM加載與事件執(zhí)行實(shí)例分析
這篇文章主要介紹了JQuery中DOM加載與事件執(zhí)行,實(shí)例分析了jQuery中DOM加載及事件執(zhí)行的原理與實(shí)現(xiàn)方法,并補(bǔ)充說明了windows.onload方法和$(document).ready()方法的區(qū)別,需要的朋友可以參考下2015-06-06

