图片懒加载|【避坑指“难”】Vue实现图片懒加载
v-imgLazy 这里我是用的IntersectionObserver API
去实现的。
IntersectionObserver
对象的observe()
方法向IntersectionObserver
对象监听的目标集合添加一个元素。一个监听者有一组阈值和一个根, 但是可以监视多个目标元素,以查看这些目标元素可见区域的变化。
简单来说可以监听dom
元素进出可视区域,并且可以控制具体的变化。
具体的使用请看 IntersectionObserver API
:https://developer.mozilla.org/zh-CN/docs/Web/API/IntersectionObserver/observe
directive/imgLazy.js:新建一个directive
用来存放自定义指令
import baseImg from '@/assets/logo.png'
// 创建一个监听器
let observer = new IntersectionObserver((entries)=>{
// entries是所有被监听对象的集合
entries.forEach(entry =>{
if(entry.isIntersecting){
// 当被监听元素到临界值且未加载图片时触发。
!entry.target.isLoaded&& showImage(entry.target,entry.target.data_src)
}
})
})
function showImage(el,imgSrc){
const img = new Image();
img.src = https://www.it610.com/article/imgSrc;
img.onload = ()=>{
el.src = https://www.it610.com/article/imgSrc;
el.isLoaded = true;
}
}
export default {
// 这里用inserted和bind都行,因为IntersectionObserver时异步的,以防意外还是用inserted好一点
// inserted和bind的区别在于inserted时元素已经插入页面,能够直接获取到dom元素的位置信息。
inserted(el,binding) {
// 初始化时展示默认图片
el.src = baseImg;
// 将需要加载的图片地址绑定在dom上
el.data_src = binding.value;
observer.observe(el)
},
unbind(){
// 停止监听
observer.disconnect();
}
}
main.js:在main.js中使用,注册全局指令
import imgLazy from '@/directive/imgLazy.js'
Vue.directive('imgLazy', imgLazy)
在组件中定义directives使用,给当前组件注册指令
import imgLazy from '@/directive/imgLazy.js'
export default {
// ...
directives: {
imgLazy: imgLazy,
},
}
【图片懒加载|【避坑指“难”】Vue实现图片懒加载】组件中使用
>
import imgLazy from '@/directive/imgLazy.js'
export default {
directives: {
imgLazy: imgLazy,
},
data(){
return {
imgSrc:[
"https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1091405991,859863778&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2396395246,715775841&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=224866248,765861809&fm=26&gp=0.jpg",
"https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2670715487,1547868437&fm=26&gp=0.jpg",
"https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2988957523,3295751190&fm=26&gp=0.jpg",
"https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2698110318,782174384&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1102788601,953675482&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1091405991,859863778&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2396395246,715775841&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=224866248,765861809&fm=26&gp=0.jpg",
"https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2670715487,1547868437&fm=26&gp=0.jpg",
"https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2988957523,3295751190&fm=26&gp=0.jpg",
"https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2698110318,782174384&fm=26&gp=0.jpg",
"https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1102788601,953675482&fm=26&gp=0.jpg",
"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2004055534,3969071219&fm=26&gp=0.jpg",
]
}
}
}
="scss" scoped>
img{
width: 200px;
height: 200px;
}