小程序开发 - 图片懒加载
一、 获取屏幕高度
wx.getSystemInfo({
success: (res)=> {
this.data.height = res.windowHeight;
}
})
二、获取图片top
wx.createSelectorQuery().selectAll('.item_li').boundingClientRect((e) => {
e.forEach((dom) => {
this.data.images[dom.dataset.i][dom.dataset.j].top = dom.top;
});
}).exec();
三、监听scroll
scroll: function (e) {
let scroll = e.detail.scrollTop;
if (this.data.lazyloadInteger > 0) {
clearTimeout(this.data.lazyloadInteger)
}
this.data.lazyloadInteger = setTimeout(() => {
this.showImg(scroll);
}, 500)
}
,showImg: function(scrollTop) {
let images = this.data.images;
let height = this.data.height + scrollTop;
for (let i in images) {
for (let j in images[i]) {
if (!images[i][j].show && height >= images[i].images[j].top) {
this.setData({[`images[${i}].[${j}].show`]: true});
}
}
}
}