温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue3图片懒加载IntersectionObserver和useIntersectionObserver如何用

发布时间:2023-05-16 16:48:19 来源:亿速云 阅读:96 作者: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://cache.yisu.com/upload/information/20230516/262/83726.jpg',
    'https://cache.yisu.com/upload/information/20230516/262/83727.jpg',
    'https://cache.yisu.com/upload/information/20230516/262/83728.jpg',
    'https://cache.yisu.com/upload/information/20230516/262/83729.jpg',
    'https://cache.yisu.com/upload/information/20230516/262/83730.jpg',
    'https://cache.yisu.com/upload/information/20230516/262/83731.jpg',
    'https://cache.yisu.com/upload/information/20230516/262/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如何用”的内容了,经过本文的学习后,相信大家对vue3图片懒加载IntersectionObserver和useIntersectionObserver如何用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI