图片过多,让多余图片延迟显示,滚动到图片的位置再加载图片
<img class="lazy" src="" x-src="背景图地址" />
$(document).ready(function(){ $("img").each(function(){ if( ($(this).offset().top + $(this).height()/8) < $(window).height() ){ $(this).attr("src", $(this).attr("x-src")); } }); }); window.onscroll = function() { $("img").each(function(){ if($(this).attr("src") == ""){ if( ($(this).offset().top + $(this).height()/8) < ($(window).height() + $(window).scrollTop())){ $(this).attr("src", $(this).attr("x-src")); } } });