插件:图片懒加载 lazyload.js

 

插件:图片懒加载  jquery.lazyload.js  2016-1-12

插件说明:http://www.w3cways.com/1765.html  (小插件,好用)

下载地址:

https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.js

https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.min.js

<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script src="jquery.lazyload.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
    $("img").lazyload({ 
        placeholder : "images/loading.jpg",
        effect: "fadeIn"
    });
});
</script>
<style>
img{display:block;margin-bottom:10px;border:0px;}
.div1{height:1800px;}
</style>
<img src="images/loading.jpg" data-original="images/1.jpg">
<img src="images/loading.jpg" data-original="images/2.jpg">
<img src="images/loading.jpg" data-original="images/3.jpg">
<img src="images/loading.jpg" data-original="images/4.jpg">
<img src="images/loading.jpg" data-original="images/5.jpg">
<img src="images/loading.jpg" data-original="images/6.jpg">
<img src="images/loading.jpg" data-original="images/7.jpg">
<img src="images/loading.jpg" data-original="images/8.jpg">
<img src="images/loading.jpg" data-original="images/9.jpg">
<div class="div1">用这个div撑出高度,在这个div之下的图片,只有在屏幕滚屏到这里后,才会显示。</div>
<img src="images/loading.jpg" data-original="images/a.jpg">
<img src="images/loading.jpg" data-original="images/b.jpg">
<img src="images/loading.jpg" data-original="images/c.jpg">
<img src="images/loading.jpg" data-original="images/d.jpg">
<img src="images/loading.jpg" data-original="images/e.jpg">
<img src="images/loading.jpg" data-original="images/f.jpg">

 

 

 

 

 

 

 

 

 

 

图片懒加载

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
    //图片懒加载
    var waterfull = {
        init: function(col) { 
            window.onscroll=this.throttle(this.isScroll);
            this.isScroll();//初始化
        },
        isScroll:function(){
            //console.log("isScroll");
            var _height=parseInt($(window).height());
            var _srcoll=parseInt($(window).scrollTop());
            var _sTop=_height+_srcoll;
            //var _sTop=_height+_srcoll-360;//减一行的高度

            var imgs=$("img[data-src]");
            var e;
            var li_top;
            for(var i=0;i<imgs.length;i++){
                e=$(imgs[i]);
                li_top = e.parents("li").offset().top;
                if(li_top<_sTop){//判断li的top小于(屏幕+滚动高)的高度,即加载
                    if(e.attr("src").length===0){
                        e.attr("src",e.attr("data-src"));
                    }
                    //if(e.attr("src").indexOf("dd.jpg")>0){    e.attr("src",e.attr("data-src"));    }
                }
            }
        },
        throttle: function(fn){
            var timer = null;
            var _this=this;
            return function(){
                var context = _this, args = arguments;
                clearTimeout(timer);
                timer = setTimeout(function(){
                    fn.apply(context,args)}, 150);
            };
        }
    }

    waterfull.init( $("#ul_shoplist"));//图片懒加载

});
</script>
<style type="text/css">
#main{margin:0 auto;width:1100px;}
ul,li{list-style:none;margin:0;padding:0;}
li{float:left;width:360px;;border:1px solid #000;}
img{width:360px;height:360px;}
</style>
</head>
<body>
<div id="main">
<ul id="ul_shoplist">
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b229c200719.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201407/01/53b1f7a1e7a28.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad13d74493a.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad0d105c4a6.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
<li><img src="" data-src="http://cms.csdnimg.cn/article/201406/27/53ad2ef165946.jpg"></a></li>
</ul>
</div>
</body>
</html>

 

你可能感兴趣的:(插件:图片懒加载 lazyload.js)