<div class="top">上边</div>
<img class="lazyload" data-src="https://s2.ax1x.com/2019/11/18/MsHSZ8.png" />
<img class="lazyload" data-src="https://s2.ax1x.com/2019/11/18/MsHSZ8.png" />
...
<div class="bottom">下边</div>
class StartLazy {
constructor(defaultImg, timeout) {
this.defaultImg = defaultImg
this.timeout = timeout || 500
}
/**
* 初始化
* 或得到全部需要懒加载的图片资源
* 设置上默认要显示的图片
* 进行懒加载
*/
init() {
this.imgs = [...document.querySelectorAll('img[data-src]')]
// 设置默认图片
this.setDefaultImgs()
// 懒加载所有
this.loadAllImgs()
const self = this
let timer = null
document.body.onscroll = function (params) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
self.loadAllImgs()
}, self.timeout);
}
}
/**
* 设置上默认要显示图片
*/
setDefaultImgs() {
if (!this.defaultImg) {
// 如果没有设置默认图片
return
}
const imgs = this.imgs
for (let i = 0; i < imgs.len; i++) {
const img = imgs[i];
img.src = defaultImg
}
}
/**
* 加载所有的图片
*/
loadAllImgs() {
const imgs = this.imgs
for (let i = 0; i < imgs.length; i++) {
const img = imgs[i]
// 判断当前图片是否需要加载
if (this.loadImg(img)) {
// 去除掉已经加载的图片
imgs.splice(i, 1)
i--
}
}
}
/**
* 懒加载一张图片
* 判断是否应该加载
*/
loadImg(img) {
// 判断该图片是否能够加载,判断图片是否在可视区域内
const rect = img.getBoundingClientRect()
if (rect.bottom <= 0) {
return false
}
if (rect.top >= document.documentElement.clientHeight) {
return false
}
img.src = img.dataset.src
// 判断是否有原图(大图) 进行预加载
if (img.dataset.original) {
// 等待图片加载完成
img.onload = () => {
img.src = img.dataset.original
// 加载完成后清除事件
img.onload - null
}
}
return true
}
}
// 使用
const lazyLoad = new StartLazy('loading.gif', 300)
lazyLoad.init()