下拉时图片载入

 
var lazyLoad = {
    /// <summary>
    /// img标签中的存放图片路径的自定义属性名称
    /// </summary>
    AttributeName: "path",
    /// <summary>
    /// 初始化。自动加载第一张图片。
    /// </summary>
    Init: function (v) {
        if (v != undefined && v != null && typeof (v) == "string") {
            this.AttributeName = v;
        }

        //        if ($("img[" + this.AttributeName + "]").size() > 0) {
        //            var src = $("img[" + this.AttributeName + "]").eq(0).attr(this.AttributeName);
        //            $("img[" + this.AttributeName + "]").eq(0).attr("src", src);
        //            $("img[" + this.AttributeName + "]").eq(0).removeAttr(this.AttributeName);
        //        }
    },
    addLoading: function (img) {
        var loadimg = $("<img src='http://static.sozhekou.com/img/common/loading.gif'/>");
        var offset = img.offset();
        var t = Body.scrollTop();
        var l = $.browser.mozilla ? 0 : img.width() / 2;
        loadimg.css({
            position: "absolute",
            top: offset.top + t + img.height() / 2 - 8,
            left: offset.left + l - 8
        });
        img.data("loadImg", loadimg);
        img.data("title", img.attr("title"));
        Body.append(loadimg);
        img.attr("title", "图片加载中...");
    },
    removeLoading: function (img) {
        img.data("loadImg").remove();
        img.attr("title", img.data("title"));
        img.removeData("loadImg");
        img.removeData("title");
    },
    /// <summary>
    /// 当scroll事件被触发时,进行加载图片的操作
    /// </summary>
    LoadImage: function (scrolltop) {
        //获取目前第一张需延迟加载的图片,无图片的话就终止
        var that = this;
        if ($("img[" + this.AttributeName + "]").size() > 0) {
            //获取窗体的高度
            var windowHeight = $(window).height();
            $("img[" + this.AttributeName + "]").each(function () {
                var currentObj = $(this);
                //获取当前图片相对于页面顶部的偏移量
                var _scrollTop = currentObj.offset().top - windowHeight - 300; // +currentObj.height();
                //根据scrollTop判断是否显示图片

                if (parseInt(scrolltop) >= parseInt(_scrollTop)) {
                    var src = currentObj.attr(that.AttributeName);
                    if (src.indexOf("http://") == -1) {
                        src = "http://" + src;
                    }
                    //that.addLoading(currentObj);
                    //alert(currentObj.offset().top);
                    currentObj.load(function () {
                        //that.removeLoading(currentObj);
                        currentObj.animate({ opacity: 1 }, 500);
                    }).attr("src", src);
                    currentObj.removeAttr(that.AttributeName);
                }
            });
        }
        else {
            return false;
        }
    },
    /// <summary>
    /// 启动延时加载
    /// <params key="v">img标签中的存放图片路径的自定义属性名称</params>
    /// </summary>
    Run: function (v) {
        this.Init(v);
        var _this = this;
        var time;
        var imgs = $("img[" + this.AttributeName + "]");
        var box = $("div.bd_wraper:first");
        if (imgs.size() > 0) {
            imgs.css("opacity", 0);
            box.bind("scroll", function () {
                time && clearTimeout(time);
                var that = this;
                time = setTimeout(function () {
                    _this.LoadImage($(this).scrollTop());
                }, 20);
            });
        }
        _this.LoadImage(box.scrollTop());
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值