溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue3圖片懶加載IntersectionObserver和useIntersectionObserver怎么用

發布時間:2023-03-16 10:30:03 來源:億速云 閱讀:215 作者:iii 欄目:開發技術

這篇文章主要介紹“vue3圖片懶加載IntersectionObserver和useIntersectionObserver怎么用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“vue3圖片懶加載IntersectionObserver和useIntersectionObserver怎么用”文章能幫助大家解決問題。

1、在src下創建helloworld.js 內容如下

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

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

// 自定義指令
const defineDirective = (app) => {
    // 1.圖片懶加載指令v-lazyLoad
// 原理:先存儲圖片地址不能在src上,當圖片進入可視區,將你存儲圖片地址設置給圖片元素(dom)的src即可。
    app.directive('lazyLoad', {
        // vue2.0監聽使用指令的DOM是否創建好,鉤子函數: inserted
        // vue3.0 的指令擁有的鉤子函數和組件的一樣,使用指令的DON是否創建好,鉤子函數: mounted,onMounted是在組合API時候使用,現在是選項
        mounted (el, binding) { // 綁定的元素,綁定的值
            // // 2.創建一個觀察對象,來觀察當前使用指令得元素

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

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

// const { stop } = useIntersectionObserver(
//     // 監聽目標元素
//     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([
    'https://yanxuan-item.nosdn.127.net/a6939f41c48fa9e9c8f7a7ed855351f1.jpg',
    'https://yanxuan-item.nosdn.127.net/0cdfd546f8675669b87716114ad5900a.jpg',
    'https://yanxuan-item.nosdn.127.net/240983ccc935146a4795e3990d30468d.jpg',
    'https://yanxuan-item.nosdn.127.net/d46e005025a5d3b73c4781d31b327558.jpg',
    'https://yanxuan-item.nosdn.127.net/330913911087b44b0d817dd78233165f.png',
    'https://yanxuan-item.nosdn.127.net/9d9590bdb4f7cdd874de6a4554abcff9.jpg',
    'https://yanxuan-item.nosdn.127.net/cd9060820a1a52f296fa2502e56a5872.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怎么用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女