彻底弄懂IntersectionObserver

最近vue3在开发一个页面懒加载组件,使用到的核心API就是IntersectionObserver,虽然之前也了解用过,但是还是不够理解,本文就用于记录自己的学习理解过程

核心逻辑

  1. IntersectionObserver中的callback触发
    • 当IntersectionObserver指定的root窗口元素 与 observe入参所指向的监听dom元素之间的intersctionRadio大于等于IntersectionObserver中指定的threshold时,会触发callback
    • intersectionRadion = 被监听元素在视图中出现的高度 / 被监听元素的总高度intersectionRect 与 boundingClientRect 的比例值,返回的值并不总是恰好符合公式,会存在少许误差

 case 1 不触发callback

         

case 2 触发callback

2. 设置threshold需要注意,能触发callback的最大threshold并不总是为1

    • 当被监听元素的大小大于窗口元素,最大threshold = 窗口元素的高 / 被监听元素的总高

用法

  1. 创建IntersectionObserver实例intersectionObserverInstance
    1. 参数
      1. callback(entries, observer)
      2. options
        1. root
        2. rootMargin
        3. threshold
  2. 通过intersectionObserverInstance.observe(dom)对希望监听的dom对象与窗口元素交叉程度进行监听

// vue3 demo

<template>

<div ref="wrap">

<slot v-if="!isShowComp" name="skelton"></slot>

<slot v-if="!isShowComp" name="loading">加载中</slot>

<slot v-else></slot>

</div>

</template>

<script setup>

import {onMounted, ref, watch} from 'vue';

let isShowComp = ref(false);

const wrap = ref(null);

const props = defineProps({

root: {

type: Object,

default(rawProp) {

return rawProp || null

}

},

threshold: {

type: [Number, Array],

default: 0.58

}

})



const initObsever = () => {

    return new IntersectionObserver((entries, observer) => {

        console.log(entries[0])

        entries.forEach(el => {

            if (props.threshold instanceof Array) {

               setTimeout(() => {

                 isShowComp.value = true;

                 observer.disconnect();

               }, 5000);

            } else if (el.intersectionRatio >= props.threshold) {

              setTimeout(() => {

                isShowComp.value = true;

                observer.disconnect();

              }, 5000);

             }

        })

    }, {

        root: props.root,

        threshold: props.threshold

    });

}

let observer = initObsever();



onMounted(() => {

    observer.observe(wrap.value)

})

</script>

<style lang="scss" scoped></style>


 

参考资料

  1. 交叉观察器 API - Web API 接口参考 | MDN

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值