<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")); } } });
本文介绍了一种使用JavaScript实现的图片懒加载技术,该技术能够提高网页加载速度,改善用户体验。通过监听滚动事件,只有当图片即将进入可视区域时才加载实际图片,避免了不必要的资源消耗。
2914

被折叠的 条评论
为什么被折叠?



