這篇文章主要講解了“vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用”吧!

豐滿網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),豐滿網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為豐滿成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的豐滿做網(wǎng)站的公司定做!
// 隨便導(dǎo)入一張圖片作為加載出錯時的默認(rèn)圖片
import defaultImg from '@/assets/logo.svg'
import {useIntersectionObserver} from "@vueuse/core"; //圖片加載失敗時候用的圖片
export default {
install (app) {
defineDirective(app)//自定義指令
}
}
// 自定義指令
const defineDirective = (app) => {
// 1.圖片懶加載指令v-lazyLoad
// 原理:先存儲圖片地址不能在src上,當(dāng)圖片進(jìn)入可視區(qū),將你存儲圖片地址設(shè)置給圖片元素(dom)的src即可。
app.directive('lazyLoad', {
// vue2.0監(jiān)聽使用指令的DOM是否創(chuàng)建好,鉤子函數(shù): inserted
// vue3.0 的指令擁有的鉤子函數(shù)和組件的一樣,使用指令的DON是否創(chuàng)建好,鉤子函數(shù): mounted,onMounted是在組合API時候使用,現(xiàn)在是選項(xiàng)
mounted (el, binding) { // 綁定的元素,綁定的值
// // 2.創(chuàng)建一個觀察對象,來觀察當(dāng)前使用指令得元素
const observe = new IntersectionObserver(([{isIntersecting}]) => {
if (isIntersecting) { // 如果進(jìn)入了可視區(qū)
// 誰進(jìn)入了可視區(qū)?得用observe去觀察 是哪個元素使用了該指令,所以會傳入dom對象el
// 停止觀察,因?yàn)橛^察一次就夠了
observe.unobserve(el)
// console.log(binding.value, el) // binding.value就是綁定的地址
// 3.監(jiān)聽圖片加載失敗,用默認(rèn)圖
el.onerror = () => {
el.src = defaultImg
}
// 4.將指令v-lazyLoad上的地址設(shè)置給el的src屬性
el.src = binding.value
}
}, {
threshold: 0//進(jìn)入到可視區(qū)交界就開始觀察
})
observe.observe(el)// 使用observe上的observe方法觀察這個dom元素
}
// const { stop } = useIntersectionObserver(
// // 監(jiān)聽目標(biāo)元素
// el,
// ([{ isIntersecting }], observerElement) => {
// if (isIntersecting) {
// // 當(dāng)圖片url無效加載失敗的時候使用默認(rèn)圖片替代
// el.onerror = function () {
// el.src = defaultImg
// }
// console.log('加載')
// el.src = binding.value
// stop()
// }
// })
// }
})
}上邊的代碼中mounted下邊這個是一種方式,這種方式不借助插件即不用安裝任何東西
const observe = new IntersectionObserver(([{isIntersecting}]) => {
if (isIntersecting) { // 如果進(jìn)入了可視區(qū)
// 誰進(jìn)入了可視區(qū)?得用observe去觀察 是哪個元素使用了該指令,所以會傳入dom對象el
// 停止觀察,因?yàn)橛^察一次就夠了
observe.unobserve(el)
// console.log(binding.value, el) // binding.value就是綁定的地址
// 3.監(jiān)聽圖片加載失敗,用默認(rèn)圖
el.onerror = () => {
el.src = defaultImg
}
// 4.將指令v-lazyLoad上的地址設(shè)置給el的src屬性
el.src = binding.value
}
}, {
threshold: 0//進(jìn)入到可視區(qū)交界就開始觀察
})
observe.observe(el)// 使用observe上的observe方法觀察這個dom元素
}mounted 下邊帶注釋的是另一種方式,這種方式需要安裝插件安裝過程點(diǎn)擊進(jìn)入教程
// const { stop } = useIntersectionObserver(
// // 監(jiān)聽目標(biāo)元素
// el,
// ([{ isIntersecting }], observerElement) => {
// if (isIntersecting) {
// // 當(dāng)圖片url無效加載失敗的時候使用默認(rèn)圖片替代
// el.onerror = function () {
// el.src = defaultImg
// }
// console.log('加載')
// el.src = binding.value
// stop()
// }
// })
// }import { createApp } from 'vue'
import App from './App.vue'
import './assets/reset.css'
import Vant from 'vant'
import 'vant/lib/index.css';
// 導(dǎo)入自己UI組件庫
import UI from './helloworld'
// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(Vant).use(UI).mount('#app')
// 具體就是導(dǎo)入這個
// import UI from './helloworld'
// 再use(UI)<template>
<div class="cs"></div>
<ul >
<li v-for="item in image_list" :key="item">
<!-- 原本的寫法-->
<!-- <img :src="item.picture" alt=""> -->
<!-- // 使用圖片懶加載指令-->
<img v-lazyLoad="item" alt="">
</li>
</ul>
</template>
<script setup>
import {nextTick, onMounted, ref} from "vue";
const image_list = ref([
'/upload/otherpic46/83726.jpg',
'/upload/otherpic46/83727.jpg',
'/upload/otherpic46/83728.jpg',
'/upload/otherpic46/83729.jpg',
'/upload/otherpic46/83730.jpg',
'/upload/otherpic46/83731.jpg',
'/upload/otherpic46/83732.jpg'
])
</script>
<style lang="less" scoped>
.cs{
height: 1000px;
background-color: pink;
}
.outer{
img{
display: inline-block;
width: 500px;
}
}
.inner{
display: inline-block;
width: 500px;
height: 50px;
background-color: deeppink;
}
</style>感謝各位的閱讀,以上就是“vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
分享名稱:vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用
網(wǎng)頁鏈接:http://chinadenli.net/article28/ipgpjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站導(dǎo)航、軟件開發(fā)、電子商務(wù)、服務(wù)器托管、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)