uniapp兼容h5、app、微信小程序图片懒加载
【代码】uniapp兼容h5、app、微信小程序图片懒加载。
·
<view class="recommend_item_image">
<view class="img_item" v-for="(eImage,ei) in item.imageList" :key="ei">
<image @click.stop="clickImg(eImage)" :src="(item.show ? eImage.imageItem : '')" :class="[item.show ? 'active': '']"></image>
</view>
</view>
onPageScroll() { // 滚动事件 this.showImg()
uni.$u.throttle(this.showImg, 500)
},
onReady() {
let that = this
uni.getSystemInfo({
success(res) {
that.windowHeight = res.windowHeight;
}
});
},
showImg() {
let that = this
// #ifndef H5
uni.createSelectorQuery().selectAll('.recommend_commodity_item').boundingClientRect((ret) => {
ret.forEach((item, index) => {
if (item.top <= that.windowHeight) {
that.$set(that.recommendData[index], 'show', true)
}
})
}).exec()
// #endif
// #ifdef H5
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry, index) => {
if (entry.isIntersecting) {
that.$set(that.recommendData[index], 'show', true)
observer.unobserve(entry.target);
}
});
});
const lazyImages = document.querySelectorAll('.recommend_commodity_item');
lazyImages.forEach((img) => {
observer.observe(img);
});
// #endif
},
更多推荐
已为社区贡献1条内容
所有评论(0)