JavaScript 延迟加载图片
🏷️ JavaScript
js
/*图片加载方法*/
; window.onscroll = function () {
var imgs = $("");
imgs.each(function () {
//检查 oLi 是否在可视区域
var t = document.documentElement.clientHeight + (document.documentElement.scrollTop || document.body.scrollTop);
var h = getH(this);
if (h < t) {
showImgDataSrc(this);
}
});
};
$("body").trigger("scroll");
; function showImgDataSrc(obj) {
if ($(obj).attr("src") != $(obj).attr("data-src")) {
$(obj).hide();
$(obj).attr("src", $(obj).attr("data-src"));
$(obj).fadeIn("slow");
}
}
/*获得对象距离页面顶端的距离*/
; function getH(obj) {
var h = 0;
while (obj) {
h += obj.offsetTop;
obj = obj.offsetParent;
}
return h;
}