背景图片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(); }) }