File tree Expand file tree Collapse file tree 1 file changed +6
-33
lines changed Expand file tree Collapse file tree 1 file changed +6
-33
lines changed Original file line number Diff line number Diff line change 11<script setup lang="ts">
2- import { onBeforeUnmount , onMounted , ref } from ' vue'
2+ import { ref } from ' vue'
3+ import { useElementVisibility } from ' @vueuse/core'
34
4- const slotWrapper = ref <HTMLElement >()
5- const isVisible = ref (false )
6- const observer = ref <IntersectionObserver >()
7-
8- function show() {
9- isVisible .value = true
10- }
11-
12- function hide() {
13- isVisible .value = false
14- }
15-
16- onMounted (() => {
17- observer .value = new IntersectionObserver ((entries ) => {
18- entries .forEach ((entry ) => {
19- if (entry .isIntersecting )
20- show ()
21- else
22- hide ()
23- })
24- }, { threshold: 0.1 })
25-
26- if (slotWrapper .value )
27- observer .value .observe (slotWrapper .value )
28- })
29-
30- onBeforeUnmount (() => {
31- if (observer .value )
32- observer .value .disconnect ()
33- })
5+ const target = ref (null )
6+ const targetIsVisible = useElementVisibility (target )
347 </script >
358
369<template >
37- <div ref =" slotWrapper " >
38- <slot v-if =" isVisible " />
10+ <div ref =" target " >
11+ <slot v-if =" targetIsVisible " />
3912 </div >
4013</template >
You can’t perform that action at this time.
0 commit comments