jQuery lazyload 的重复加载错误以及修复方法

  分析代码也可以知道。

  最主要的原因是 写在页面上的 <img src="the_big_img_toLoad.jpg" />一经加载,就去向服务器申请图片地址,加载大图片。

  如果想实现原定的效果,则 写在页面上的待加载地址 必须不能是大图片的地址,而我们可以将真正的图片地址数据 存储在alt属性中。

  正确的例子如下:

  

复制代码 代码如下:

  <a href="#nogo"><img alt="http://www.glzy8.com/comstyles/img200-150-3.jpg" src="http://sc.glzy8.com/style/img/spacer.gif" alt="200-150" /></a>

  <a href="#nogo"><img alt="http://www.glzy8.com/comstyles/img200-150-4.jpg" src="http://sc.glzy8.com/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.glzy8.com/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的眼睛还是雪亮的。