詳解Vue3如何優(yōu)雅的加載大量圖片
原理
這個(gè)功能主要的底層邏輯是是使用IntersectionObserver
API
,IntersectionObserver
用于在瀏覽器中觀察元素的可見性和位置變化。它可以幫助開發(fā)者實(shí)現(xiàn)一些動(dòng)態(tài)行為,如圖片的懶加載、無限滾動(dòng)等。
簡單的示例如下:
// 創(chuàng)建IntersectionObserver實(shí)例 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); // 停止觀察該元素 observer.unobserve(img); } }); }); // 獲取所有需要懶加載的圖片元素 const lazyImages = document.querySelectorAll('.lazy-image'); // 觀察每個(gè)圖片元素 lazyImages.forEach(image => { observer.observe(image); });
實(shí)踐
接下來我們實(shí)現(xiàn)一個(gè)通用的 hook
,基本的功能如下:
- 給圖片提供默認(rèn)的占位圖片
src
,同時(shí)提供data-src
屬性 - 傳入圖片對應(yīng)的
ref
屬性。 - 當(dāng)圖片進(jìn)入可視區(qū)域時(shí),使用
data-src
屬性替換src
屬性
import { onMounted, Ref } from "vue"; const options = { // root: document.querySelector(".container"), // 根元素,默認(rèn)為視口 rootMargin: "0px", // 根元素的邊距 threshold: 0.5, // 可見性比例閾值 once: true, }; function callback( entries: IntersectionObserverEntry[], observer: IntersectionObserver ) { entries.forEach((entry) => { // 處理每個(gè)目標(biāo)元素的可見性變化 if (entry.intersectionRatio <= 0) return; const img: Element = entry.target; const src = img.getAttribute("data-src"); img.setAttribute("src", src ?? ""); // 將真實(shí)的圖片地址賦給 src 屬性 observer.unobserve(img); }); } export const useInView = (ref: Ref) => { const observer = new IntersectionObserver(callback, options); onMounted(() => { Object.keys(ref.value).forEach((e) => observer.observe(ref.value[e])); }); };
<script setup lang="ts"> import { ref } from "vue"; import { useInView } from "./hooks/useInView"; const imgRef = ref(null); useInView(imgRef); </script> <template> <h4>公眾號:萌萌噠草頭將軍</h4> <div v-for="(_, idx) in new Array(200).fill(11)" > <img ref="imgRef" src="https://via.placeholder.com/200" :data-src="`https://picsum.photos/200/${180 + idx}`" alt="b" /> </div> </template>
實(shí)際效果如下
雖然基本的功能要求已經(jīng)完成了,但是現(xiàn)在還不夠優(yōu)雅?。?!
優(yōu)化
接下來,我們增加個(gè)過渡動(dòng)畫。每次當(dāng)加載完圖片,就從占位圖過渡到正常圖片模式。
img.onload = () => { img.setAttribute('class', 'fade-in') }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } /* 應(yīng)用淡入動(dòng)畫到元素 */ .fade-in { animation: fadeIn 0.6s ease-in; }
完整代碼如下:
import { onMounted, Ref } from "vue"; const options = { // root: document.querySelector(".container"), // 根元素,默認(rèn)為視口 rootMargin: "0px", // 根元素的邊距 threshold: 0.5, // 可見性比例閾值 once: true, }; function callback( entries: IntersectionObserverEntry[], observer: IntersectionObserver ) { entries.forEach((entry) => { if (entry.intersectionRatio <= 0) return; const img = entry.target as HTMLImageElement; const src = img.getAttribute("data-src"); img.setAttribute("src", src ?? ""); // 將真實(shí)的圖片地址賦給 src 屬性 img.onload = () => { img.setAttribute("class", "fade-in"); }; observer.unobserve(img); }); } export const useInView = (ref: Ref) => { const observer = new IntersectionObserver( callback, options ); onMounted(() => { Object.keys(ref.value) .forEach((e) => observer.observe(ref.value[e])); }); };
<script setup lang="ts"> import { ref } from "vue"; import { useInView } from "./hooks/useInView"; const imgRef = ref(null); useInView(imgRef); </script> <template> <h4>公眾號:萌萌噠草頭將軍</h4> <div v-for="(_, idx) in new Array(200).fill(11)" style="width: 200px height: 200px;" > <img ref="imgRef" style="height: 100%" src="https://via.placeholder.com/200" :data-src="`https://picsum.photos/200/${180 + idx}`" alt="b" /> </div> </template> <style scoped> /* 定義淡入動(dòng)畫 */ @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } /* 應(yīng)用淡入動(dòng)畫到元素 */ .fade-in { animation: fadeIn 0.6s ease-in; } </style>
以上就是詳解Vue3如何優(yōu)雅的加載大量圖片的詳細(xì)內(nèi)容,更多關(guān)于Vue3加載大量圖片的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Composition API思想封裝NProgress示例詳解
這篇文章主要為大家介紹了Composition API思想封裝NProgress示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08使用Vue-Office實(shí)現(xiàn)Office文件預(yù)覽組件
在現(xiàn)代Web應(yīng)用中,Office 文件的預(yù)覽是一個(gè)常見的需求,本文將使用Vue-Office實(shí)現(xiàn)Office文件預(yù)覽組件,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03vue中ref和e.target的區(qū)別以及ref用法
這篇文章主要介紹了vue中ref和e.target的區(qū)別以及ref用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03關(guān)于@click.native中?.native?的含義與使用方式
這篇文章主要介紹了關(guān)于@click.native中?.native?的含義與使用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10一文詳解Vue3中簡單diff算法的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹Vue3中簡單diff算法的實(shí)現(xiàn)與使用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-09-09vue中點(diǎn)擊下載圖片的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue中點(diǎn)擊下載圖片的實(shí)現(xiàn)方法,在Vue的模板中,我們可以將下載屬性綁定至或元素上,用來實(shí)現(xiàn)點(diǎn)擊下載,需要的朋友可以參考下2023-08-08