jQuery lazyload 的重復加載錯誤以及修復方法
更新時間:2010年11月19日 16:52:34 作者:
jQuery lazyload是一款延遲加載圖片的的插件,原意是按需加載,當圖片出現在可視區(qū)域時進行加載,但是官方的插件經過firebug的檢測可知,并不能節(jié)省流量開支,反而有重復加載的現象。
分析代碼也可以知道。
最主要的原因是 寫在頁面上的 <img src="the_big_img_toLoad.jpg" />一經加載,就去向服務器申請圖片地址,加載大圖片。
如果想實現原定的效果,則 寫在頁面上的待加載地址 必須不能是大圖片的地址,而我們可以將真正的圖片地址數據 存儲在alt屬性中。
正確的例子如下:
<a href="#nogo"><img alt="http://www.dbjr.com.cn/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.dbjr.com.cn/comstyles/img200-150-4.jpg" src="http://sc.jb51.net/style/img/spacer.gif" alt="200-150" /></a>
對原來的 jquery.lazyload.js我們也需要做一點改動:
<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>
完整實例如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</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,滾動到下面的時候,相應圖片才開始加載
</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>
測試可知 是真正的按需加載。而不是像之前,先加載了,滾動到相應位置 還又加載了一次。
Firebug的眼睛還是雪亮的。
最主要的原因是 寫在頁面上的 <img src="the_big_img_toLoad.jpg" />一經加載,就去向服務器申請圖片地址,加載大圖片。
如果想實現原定的效果,則 寫在頁面上的待加載地址 必須不能是大圖片的地址,而我們可以將真正的圖片地址數據 存儲在alt屬性中。
正確的例子如下:
復制代碼 代碼如下:
<a href="#nogo"><img alt="http://www.dbjr.com.cn/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.dbjr.com.cn/comstyles/img200-150-4.jpg" src="http://sc.jb51.net/style/img/spacer.gif" alt="200-150" /></a>
對原來的 jquery.lazyload.js我們也需要做一點改動:
復制代碼 代碼如下:
<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>
完整實例如下:
復制代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</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,滾動到下面的時候,相應圖片才開始加載
</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>
測試可知 是真正的按需加載。而不是像之前,先加載了,滾動到相應位置 還又加載了一次。
Firebug的眼睛還是雪亮的。
相關文章
jQuery Validate驗證表單時多個name相同的元素只驗證第一個的解決方法
這篇文章主要介紹了jQuery Validate驗證表單時多個name相同的元素只驗證第一個的問題及解決方法,需要的朋友可以參考下2016-12-12
jQuery動態(tài)添加li標簽并添加屬性和綁定事件方法
下面小編就為大家分享一篇jQuery動態(tài)添加li標簽并添加屬性和綁定事件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
解決html-jquery/js引用外部圖片時遇到看不了或出現403的問題
下面小編就為大家?guī)硪黄鉀Qhtml-jquery/js引用外部圖片時遇到看不了或出現403的問題。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

