jQuery lazyload 的重复加载错误以及修复方法_Javascript教程-查字典教程网
jQuery lazyload 的重复加载错误以及修复方法
jQuery lazyload 的重复加载错误以及修复方法
发布时间:2016-12-30 来源:查字典编辑
摘要:分析代码也可以知道。最主要的原因是写在页面上的一经加载,就去向服务器申请图片地址,加载大图片。如果想实现原定的效果,则写在页面上的待加载地址...

分析代码也可以知道。

最主要的原因是 写在页面上的 <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>

<a href="#nogo"><img src="http://www.titan24.com/comstyles/img200-150-2.jpg" alt="200-150" /></a>

第一屏高度为900px,滚动到下面的时候,相应图片才开始加载

</div>

<div id="lazy1">

<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">

<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的眼睛还是雪亮的。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类