vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用

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

豐滿網(wǎng)站建設公司成都創(chuàng)新互聯(lián),豐滿網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為豐滿成百上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的豐滿做網(wǎng)站的公司定做!

1、在src下創(chuàng)建helloworld.js 內(nèi)容如下

// 隨便導入一張圖片作為加載出錯時的默認圖片
import defaultImg from '@/assets/logo.svg'

import {useIntersectionObserver} from "@vueuse/core"; //圖片加載失敗時候用的圖片
export default {
    install (app) {
        defineDirective(app)//自定義指令
    }
}

// 自定義指令
const defineDirective = (app) => {
    // 1.圖片懶加載指令v-lazyLoad
// 原理:先存儲圖片地址不能在src上,當圖片進入可視區(qū),將你存儲圖片地址設置給圖片元素(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)在是選項
        mounted (el, binding) { // 綁定的元素,綁定的值
            // // 2.創(chuàng)建一個觀察對象,來觀察當前使用指令得元素

            const observe = new IntersectionObserver(([{isIntersecting}]) => {
                if (isIntersecting) { // 如果進入了可視區(qū)
                    // 誰進入了可視區(qū)?得用observe去觀察 是哪個元素使用了該指令,所以會傳入dom對象el
                    // 停止觀察,因為觀察一次就夠了
                    observe.unobserve(el)
                    // console.log(binding.value, el) // binding.value就是綁定的地址
                    // 3.監(jiān)聽圖片加載失敗,用默認圖
                    el.onerror = () => {
                        el.src = defaultImg
                    }
                    // 4.將指令v-lazyLoad上的地址設置給el的src屬性
                    el.src = binding.value
                }
            }, {
                threshold: 0//進入到可視區(qū)交界就開始觀察
            })
            observe.observe(el)// 使用observe上的observe方法觀察這個dom元素
        }
            // const { stop } = useIntersectionObserver(
            //     // 監(jiān)聽目標元素
            //     el,
            //     ([{ isIntersecting }], observerElement) => {
            //         if (isIntersecting) {
            //             // 當圖片url無效加載失敗的時候使用默認圖片替代
            //             el.onerror = function () {
            //                 el.src = defaultImg
            //             }
            //             console.log('加載')
            //             el.src = binding.value
            //             stop()
            //         }
            //     })
            // }
    })
}

上邊的代碼中mounted下邊這個是一種方式,這種方式不借助插件即不用安裝任何東西

const observe = new IntersectionObserver(([{isIntersecting}]) => {
        if (isIntersecting) { // 如果進入了可視區(qū)
            // 誰進入了可視區(qū)?得用observe去觀察 是哪個元素使用了該指令,所以會傳入dom對象el
            // 停止觀察,因為觀察一次就夠了
            observe.unobserve(el)
            // console.log(binding.value, el) // binding.value就是綁定的地址
            // 3.監(jiān)聽圖片加載失敗,用默認圖
            el.onerror = () => {
                el.src = defaultImg
            }
            // 4.將指令v-lazyLoad上的地址設置給el的src屬性
            el.src = binding.value
        }
    }, {
        threshold: 0//進入到可視區(qū)交界就開始觀察
    })
    observe.observe(el)// 使用observe上的observe方法觀察這個dom元素
}

mounted 下邊帶注釋的是另一種方式,這種方式需要安裝插件安裝過程點擊進入教程

// const { stop } = useIntersectionObserver(
//     // 監(jiān)聽目標元素
//     el,
//     ([{ isIntersecting }], observerElement) => {
//         if (isIntersecting) {
//             // 當圖片url無效加載失敗的時候使用默認圖片替代
//             el.onerror = function () {
//                 el.src = defaultImg
//             }
//             console.log('加載')
//             el.src = binding.value
//             stop()
//         }
//     })
// }

2、在main.js中導入并使用

import { createApp } from 'vue'
import App from './App.vue'
import './assets/reset.css'
import Vant from 'vant'
import 'vant/lib/index.css';
// 導入自己UI組件庫
import UI from './helloworld'

// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(Vant).use(UI).mount('#app')
// 具體就是導入這個
// import UI from './helloworld'
// 再use(UI)

3、再app.vue中使用如下:

<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)過本文的學習后,相信大家對vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!

分享名稱:vue3圖片懶加載IntersectionObserver和useIntersectionObserver如何用
網(wǎng)頁鏈接:http://muchs.cn/article28/ipgpjp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃網(wǎng)站導航、軟件開發(fā)電子商務、服務器托管、網(wǎng)站設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設