×

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

作者:jiang2017.12.25来源:Web前端之家浏览:9066评论:0
关键词:JQueryJS

分析代码也可以知道。

最主要的原因是 写在页面上的 <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的眼睛还是雪亮的。

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/svg1488038400838.html

网友评论文明上网理性发言 已有0人参与

发表评论: