jQuery lazyload 的重复加载错误以及修复方法
更新时间:2010年11月19日 16:52:34 作者:
jQuery lazyload是一款延迟加载图片的的插件,原意是按需加载,当图片出现在可视区域时进行加载,但是官方的插件经过firebug的检测可知,并不能节省流量开支,反而有重复加载的现象。
分析代码也可以知道。
最主要的原因是 写在页面上的 <img src="the_big_img_toLoad.jpg" />一经加载,就去向服务器申请图片地址,加载大图片。
如果想实现原定的效果,则 写在页面上的待加载地址 必须不能是大图片的地址,而我们可以将真正的图片地址数据 存储在alt属性中。
正确的例子如下:
<a href="#nogo"><img alt="https://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="https://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的眼睛还是雪亮的。
最主要的原因是 写在页面上的 <img src="the_big_img_toLoad.jpg" />一经加载,就去向服务器申请图片地址,加载大图片。
如果想实现原定的效果,则 写在页面上的待加载地址 必须不能是大图片的地址,而我们可以将真正的图片地址数据 存储在alt属性中。
正确的例子如下:
复制代码 代码如下:
<a href="#nogo"><img alt="https://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="https://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的眼睛还是雪亮的。
相关文章
jquery 获取 outerHtml 包含当前节点本身的代码
jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,后来实验发现有一个jQuery的一个方法可以解决2014-10-10
jquery checkbox 勾选的bug问题解决方案与分析
本文首先由一个在项目中遇到的jquery checkbox 勾选的bug的解决方案,引申出jQuery中attr()和prop()的差异分析,非常的实用,需要的小伙伴快来研究下吧2014-11-11
利用jQuery.Validate异步验证用户名是否存在(推荐)
这篇文章主要介绍了利用jQuery.Validate异步验证用户名是否存在的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下2016-12-12


最新评论