fl-Lazyimg图片懒加载
基础使用
将img 绑定在fl-Lazyimg 标签中,设置img 的src-data 为图片路径即可使用。
当页面滚动到图片区域的时候则为自动加载。
<template>
<Lazyimg>
<img src-data = "../src/assets/image/4.jpg">
<img src-data = "../src/assets/image/5.jpg">
<img src-data = "../src/assets/image/6.jpg">
</Lazyimg>
</template>
源码:
<template>
<div>
<slot></slot>
</div>
</template>
<script>
export default {
name:'fl-Lazyimg',
mounted () {
this.imgDomList = this.$slots.default.filter(item=>{
return item.tag == 'img'
})
this.imgDomList = this.imgDomList.map(item=>{
return item.elm
})
this.obverseImg()
},
data() {
return {
imgDomList:[],
}
},
methods: {
obverseImg() {
this.imgDomList.forEach((img,index)=>{
var io = new IntersectionObserver(entries => {
if(entries[0].isIntersecting){
io.unobserve(this.imgDomList[index]);
setTimeout(() => {
this.imgDomList[index].setAttribute('src',this.imgDomList[index].getAttribute('src-data'))
}, 500);
}
});
io.observe(this.imgDomList[index]);
})
}
},
}
</script>
<style scoped>
</style>
|