jQuery lazyload是一款延遲加載圖片的的插件,原意是按需加載,當圖片出現在可視區域時進行加載,但是官方的插件經過firebug的檢測可知,并不能節省流量開支,反而有重復加載的現象。
分析代碼也可以知道。
最主要的原因是 寫在頁面上的 <img src="the_big_img_toLoad.jpg" />一經加載,就去向服務器申請圖片地址,加載大圖片。
如果想實現原定的效果,則 寫在頁面上的待加載地址 必須不能是大圖片的地址,而我們可以將真正的圖片地址數據 存儲在alt屬性中。
正確的例子如下:
<a href="#nogo"><img alt="http://www.jb51.net/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.jb51.net/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的眼睛還是雪亮的。
新聞熱點
疑難解答
圖片精選