背景图片lazyload

 

    // 背景lazyload
    var $willBgItems= $("[data-lazy-bg]");
    if ($willBgItems.length>0){
        var topArray=[];
        var $willDistance = $(window).height()*1.8;
        $willBgItems.each(function(i,element){
            topArray[i]=$(element).offset().top
        });
        function updateImage(){
            $willBgItems.each(function(i,element){
                if ($(element).data("lazy-bg").length>0 && $(element).data("lazy-bg")!=element.style.backgroundImage.replace(/"|'/g,"").replace(/url\(|\)$/ig, "")){
                    if (topArray[i]<$(window).scrollTop()+$willDistance){
                        $(element).css({
                            "background-image":'url('+$(element).data("lazy-bg")+')'
                        })
                    }
                }
            });
        }
        updateImage();
        $(window).bind("scroll", function (event) {
            updateImage();
        })
    }

 

posted @ 2015-10-15 17:15  安雅然  阅读(518)  评论(0编辑  收藏  举报