图片懒加载js实现

43次阅读

共计 6023 个字符,预计需要花费 16 分钟才能阅读完成。

1. 懒加载原理
懒加载实现原理很简单, 通过在 img 标签中设置一个 H5 自定义属性, 在元素出现在可视区域时, 替换默认的加载图片来达到效果
2. 实现代码
<div id=”root”>
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220763007&di=45a5220dafdb197a4ab5316c2a42f734&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fac4bd11373f08202237cd08a49fbfbedaa641b9c.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911449&di=ca58cde281401ca95b098f6047f698db&imgtype=0&src=http%3A%2F%2Fimg5.xiazaizhijia.com%2Fwalls%2F20150417%2Fmid_84422024ff063d3.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911448&di=63a249145a90ae1681ce0b9c61354f2c&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F9e3df8dcd100baa1f437f36f4d10b912c9fc2ece.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911448&di=565668b21f797bef07c944c4f638c14a&imgtype=0&src=http%3A%2F%2Fpic.qiantucdn.com%2F58pic%2F13%2F71%2F35%2F24k58PICSiB_1024.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911447&di=bf7ab0359c7c5f998d73671e1e62c5bb&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D8a953b1817950a7b6138468762b808ac%2F03087bf40ad162d9344e02321bdfa9ec8a13cd78.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911446&di=8d04c9b363f1a924a96fb109276e890a&imgtype=0&src=http%3A%2F%2Fpic20.photophoto.cn%2F20110902%2F0034034471873095_b.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911446&di=5dce0c273b95e9b80afe5573c730aa54&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F0%2F5789d87be269b.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911446&di=1f8feec9ccc9f136f658ca39e5889545&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F13%2F16%2F42%2F18F58PIChTy_1024.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220911447&di=b6e2f4553b12b4822788bc637a239f94&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F83025aafa40f4bfbfbba4380094f78f0f63618ff.jpg” />
<img src=”https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png”
data-is-loaded=”false” data-src=”https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545220991377&di=742d7957b05c57bd3c19c43ab736056f&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F38dbb6fd5266d016c2a2ac069c2bd40735fa3560.jpg” />
</div>
// 定时器,300ms 延迟后加载图片
let timer = null;
let loadingImgUrl =
‘https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1545210653&di=173f4d639097ddf9e69ed23cd0da47f9&src=http://img.mp.sohu.com/upload/20170720/cd59c28a4c6d49bc8b4ecfcdffbc04f1_th.png’;
let loadingHeight = 0;
let n = 0;

function lazyload() {
let imgs = document.getElementById(‘root’).getElementsByTagName(‘img’);
// 获取可视窗口高度
let viewHeight = document.documentElement.clientHeight;
for (let i = 0; i < imgs.length; i++) {
let img = imgs[i];
// 获取图片离可视区域顶部的距离
let top = img.getBoundingClientRect().top;
if(‘false’ == img.dataset.isLoaded && ((0 <= top && top <= viewHeight) || (top <= 0 && Math.abs(top) <= loadingHeight))) {
// 打印 1,2,…,9,10
console.log(++n);
// 替换 src
img.src = img.dataset.src;
img.dataset.isLoaded = true;
}
}
}

let image = new Image();
image.src = loadingImgUrl;

// 先加载 loading 图片, 避免获取的 img 标签 top 属性不正确
image.onload = function () {
loadingHeight = image.height;
lazyload();
}

window.addEventListener(‘scroll’, function () {
clearTimeout(timer);
timer = setTimeout(function () {
lazyload();
}, 300);
})

正文完
 0