页面首页会加载大量的图片,初次进入页面时,会导致页面性能下降,于是乎,改进这个功能,可以让所有图片自动懒加载
这个功能主要的底层逻辑是是使用IntersectionObserver API,IntersectionObserver用于在浏览器中观察元素的可见性和位置变化。它可以帮助开发者实现一些动态行为,如图片的懒加载、无限滚动等
// 创建IntersectionObserver实例
const observer = new IntersectionObserver((entries, observer) => {
// 遍历元素观察对象
entries.forEach(entry => {
if (entry.isIntersecting) {
// 加载图片
const img = entry.target;
const src = img.getAttribute('data-src');
img.setAttribute('src', src);
img.onload = () => {
img.setAttribute('class', `${props.imgClass} fade-in`);
}
// 停止观察元素
observer.unobserve(img)
}
})
});
// 获取所有需要懒加载的图片元素
const lazyImages = documents.querySelectorAll('.lazy-image');
// 观察每个图片元素
lazyImages.forEach(image => {
observer.observe(image);
});
接下来实现一个通用的 组件,基本的功能如下:
1.给图片提供默认的占位图片 src,同时提供data-src属性
2.传入图片对应的 ref 属性。
3.当图片进入可视区域时,使用data-src属性替换 src 属性
import { ref, onMounted } from 'vue';
const options = {
root: document.getElementById(`${props.rootId}`),
rootMargin: "0px", // 根元素的边距离
threshold: 0.5 // 可见性比例阈值
};
const callback = (entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const img = entry.target;
const src = img.getAttribute('data-src');
img.setAttribute('src', src); // 将真是的图片地址付给src属性
img.onload = () => {
img.setAttribute('class', `${props.imgClass} fade-in`);
}
observer.unobserve(img)
}
});
}
<div :class="props.wrapClass" v-viewer="{ toolbar: false }">
<template v-for="imgSrc of props.dataSource" :key="imgSrc">
<img ref="imgRef" :class="imgClass" :data-src="imgSrc"/>
</template>
</div>
接下来,我们增加个过渡动画。每次当加载完图片,就从占位图过渡到正常图片模式。
img.onload = () => {
img.setAttribute('class', `${props.imgClass} fade-in`);
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-in {
animation: fadeIn 0.6s ease-in;
}
完整示例代码
<template>
<div :class="props.wrapClass" v-viewer="{ toolbar: false }">
<template v-for="imgSrc of props.dataSource" :key="imgSrc">
<img ref="imgRef" :class="imgClass" :data-src="imgSrc"/>
</template>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
const props = defineProps({
dataSource: {
type: Array,
default: () => []
},
wrapClass: {
type: String,
default: ''
},
imgClass: {
type: String,
default: ''
},
rootId: {
type: String,
default: ''
}
});
const options = {
root: document.getElementById(`${props.rootId}`),
rootMargin: "0px",
threshold: 0.5
};
const callback = (entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const img = entry.target;
const src = img.getAttribute('data-src');
img.setAttribute('src', src);
img.onload = () => {
img.setAttribute('class', `${props.imgClass} fade-in`);
}
observer.unobserve(img)
}
});
}
const imgRef = ref(null);
onMounted(() => {
const observer = new IntersectionObserver(callback, options);
Object.keys(imgRef.value).forEach(item => {
observer.observe(imgRef.value[item]);
});
});
</script>
<style scoped lang="less">
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-in {
animation: fadeIn 0.6s ease-in;
}
</style>
参考资料
https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API