关闭

scrollLoading插件实现图片延迟懒加载实例包涵html和js代码

标签: html图片实例插件测试
884人阅读 评论(0) 收藏 举报
分类:
引用两个js文件:
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="js/jquery.scrollLoading-min.js" type="text/javascript"></script>

html代码部分:

<html>
<head>
 <title>测试页</title>
 <meta charset="UTF-8"/>
    <script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="js/jquery.scrollLoading-min.js" type="text/javascript"></script>
    <style type="text/css">
        #box{
            margin: 0 auto;
            text-align: center;
        }
        .scrollLoading{
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div id="box">
    <img data-url="img/0.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"/></br>
    <img data-url="img/1.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg" ></br>
    <img data-url="img/2.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg" ></br>
    <img data-url="img/3.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg" ></br>
    <img data-url="img/4.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg" ></br>
    <img data-url="img/5.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg" ></br>
    <img data-url="img/6.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/7.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/8.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg" ></br>
    <img data-url="img/9.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/10.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/11.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/12.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/13.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/14.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/15.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/16.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/17.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/18.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/19.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/20.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/21.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/22.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/23.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/24.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/25.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/26.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
    <img data-url="img/27.jpg"  class="scrollLoading" align="absmiddle" src="img/load.jpg"></br>
</div>
    <script type="text/javascript">
        $(function(){
            $(".scrollLoading").scrollLoading();
        })
    </script>
</body>
</html>

js代码部分:

if (/zhangxinxu|localhost/.test(location.href)==false) { alert('含外链JS资源,页面即将跳转...');location.href = "http://www.zhangxinxu.com/sp/welcome.php"; }
/*!
 * jquery.scrollLoading.js
 * by zhangxinxu  http://www.zhangxinxu.com
 * 2010-11-19 v1.0
 * 2012-01-13 v1.1 偏移值计算修改 position → offset
 * 2012-09-25 v1.2 增加滚动容器参数, 回调参数
 * 2015-11-17 v1.3 只对显示元素进行处理
*/
(function($) {
    $.fn.scrollLoading = function(options) {
        var defaults = {
            attr: "data-url",
            container: $(window),
            callback: $.noop
        };
        var params = $.extend({}, defaults, options || {});
        params.cache = [];
        $(this).each(function() {
            var node = this.nodeName.toLowerCase(), url = $(this).attr(params["attr"]);
            //重组
            var data = {
                obj: $(this),
                tag: node,
                url: url
            };
            params.cache.push(data);
        });

        var callback = function(call) {
            if ($.isFunction(params.callback)) {
                params.callback.call(call.get(0));
            }
        };
        //动态显示数据
        var loading = function() {

            var contHeight = params.container.height();
            if ($(window).get(0) === window) {
                contop = $(window).scrollTop();
            } else {
                contop = params.container.offset().top;
            }        

            $.each(params.cache, function(i, data) {
                var o = data.obj, tag = data.tag, url = data.url, post, posb;

                if (o) {
                    post = o.offset().top - contop, post + o.height();

                    if (o.is(':visible') && (post >= 0 && post < contHeight) || (posb > 0 && posb <= contHeight)) {
                        if (url) {
                            //在浏览器窗口内
                            if (tag === "img") {
                                //图片,改变src
                                callback(o.attr("src", url));        
                            } else {
                                o.load(url, {}, function() {
                                    callback(o);
                                });
                            }        
                        } else {
                            // 无地址,直接触发回调
                            callback(o);
                        }
                        data.obj = null;    
                    }
                }
            });    
        };

        //事件触发
        //加载完毕即执行
        loading();
        //滚动执行
        params.container.bind("scroll", loading);
    };
})(jQuery);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:189173次
    • 积分:2409
    • 等级:
    • 排名:第15445名
    • 原创:80篇
    • 转载:6篇
    • 译文:0篇
    • 评论:40条
    文章分类
    最新评论