• jQuery 简洁几句代码实现图片延迟加载


    /*!
     * jquery.scrollLoading.js*/
    (function($) {
        $.fn.scrollLoading = function(options) {
            var defaults = {
                attr: "data-original",
                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 (params.container.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, posb = post + o.height();
                        if ((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);
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>延时加载</title>
    <style type="text/css">
    body,div,{margin:0;padding:0;}
    #box{770px;margin:0 auto;padding:10px 0 0 10px;overflow:hidden;zoom:1;}
    #box img{float:left;375px;height:225px;color:#fff;padding:10px 0;display:inline;text-align:center;margin:0 10px 10px 0;background:#000 url(http://fgm.cc/learn/lesson10/img/lazy/loading.gif) 50% 50% no-repeat;}
    #box img.loaded{353px;height:225px;vertical-align:middle;}
    </style>
    </head>
    <body>
    <div id="box">
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/1.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/2.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/3.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/4.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/5.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/6.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/7.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/8.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/9.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/10.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/11.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/12.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/13.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/14.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/15.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/16.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/17.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/18.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/19.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/20.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/21.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/22.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/23.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/24.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/25.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/26.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/27.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/28.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/29.jpg" />
        <img src="http://fgm.cc/learn/lesson10/img/lazy/none.gif" data-img="http://fgm.cc/learn/lesson10/img/lazy/30.jpg" />
    </div>
    
    
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
    $(function(){
        showimg();//加载第一屏
        $(window).scroll(function(){
            showimg();//显示当前滚动位置一下两屏且没有被加载的图片
        });
    });
    function showimg(){
        var vtop=$(document).scrollTop();//滚动条滚动的距离
        var wheight=$(window).height();//窗口高度 
        $("#box img").each(function(){//遍历img
            var truesrc=$(this).attr("data-img");//获取真实图片地址
            if(truesrc&&$(this).offset().top<=(wheight+vtop)){ //如果truesrc存在且有值的时候加载。
                $(this).attr("src",truesrc).removeAttr("data-img");//赋值真实图片地址并移除truesrc属性防止重复加载。
            }
        });
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    将抓包工具证书从用户目录移动至系统目录,解决反爬对于本地证书认证(安卓7)
    《C++ concurrency in action》 读书笔记 -- Part 2 第三章 线程间的数据共享
    《C++ concurrency in action》 读书笔记 -- Part 3 第四章 线程的同步
    C++14 也快要来了
    《C++ concurrency in action》 读书笔记 -- Part 4 第五章 C++的多线程内存模型 (1)
    利用表达式树构建委托改善反射性能
    使用Task简化Silverlight调用Wcf(再续)
    逆变与协变详解
    Beginning Silverlight 4 in C#数据访问和网络
    使用Task简化Silverlight调用Wcf(续)
  • 原文地址:https://www.cnblogs.com/huanlei/p/2741850.html
Copyright © 2020-2023  润新知