<script setup lang="ts">
//下载vueuse插件
import { useIntersectionObserver } from '@vueuse/core';
import { ref } from 'vue';
//此target要和被监视的目标模块想关联
const target = ref(null);
const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
console.log('是否进入可视区域', isIntersecting);
if (isIntersecting) {
stop();
}
});
</script>
<template>
<div>
<div class="box"></div>
<div class="box2" ref="target"></div>
</div>
</template>
<style lang="less" scoped>
.box {
width: 500px;
height: 1000px;
background-image: linear-gradient(to right, red, green, yellow);
}
.box2 {
width: 500px;
height: 500px;
margin: 0 auto;
background-color: pink;
}
</style>