@@ -32,18 +32,12 @@ useIntersection(
3232
3333## Usage
3434
35+ First we have to define the intersection component, named ` UseIntersectionElDemo.vue ` .
36+
3537``` html
3638<template >
37- <div >
38- <div class =" el-wrap" >
39- <div ref =" el1Ref" :class =" el1Class" class =" el" ></div >
40- </div >
41- <div class =" el-wrap" >
42- <div ref =" el2Ref" :class =" el2Class" class =" el" ></div >
43- </div >
44- <div class =" el-wrap" >
45- <div ref =" el3Ref" :class =" el3Class" class =" el" ></div >
46- </div >
39+ <div ref =" elRef" class =" el" :class =" elClass" >
40+ <slot ></slot >
4741 </div >
4842</template >
4943
@@ -53,50 +47,60 @@ useIntersection(
5347 import { useIntersection } from ' vue-use-kit'
5448
5549 export default Vue .extend ({
56- name: ' UseIntersectionDemo ' ,
50+ name: ' UseIntersectionElDemo ' ,
5751 setup () {
5852 const options = {
5953 root: null ,
6054 rootMargin: ' 0px 0px -30px' ,
6155 threshold: 1.0
6256 }
6357
64- const watchClass = (className , observedEntry , isEnabled ) => {
65- if (! isEnabled) return
58+ const elRef = ref (null )
59+ const elClass = ref (' ' )
60+ const { observedEntry } = useIntersection (elRef, options)
61+ watch (() => {
62+ if (! observedEntry .value ) return
6663 const isVisible = observedEntry .value .intersectionRatio === 1
67- className .value = isVisible ? ' -is-visible' : ' '
64+ elClass .value = isVisible ? ' -is-visible' : ' '
65+ })
66+
67+ return {
68+ elRef,
69+ elClass
6870 }
71+ }
72+ })
73+ </script >
74+ ```
6975
70- const el1Ref = ref (null )
71- const el1Class = ref (' ' )
72- const { observedEntry: el1Ob } = useIntersection (el1Ref, options)
73- watch (() => watchClass (el1Class, el1Ob, el1Ob .value ))
76+ Then we can call it in a loop in the parent component ` UseIntersectionDemo.vue ` .
7477
75- const el2Ref = ref (null )
76- const el2Class = ref (' ' )
77- const { observedEntry: el2Ob } = useIntersection (el2Ref, options)
78- watch (() => watchClass (el2Class, el2Ob, el2Ob .value ))
78+ ``` html
79+ <template >
80+ <div >
81+ <div class =" el-wrap" v-for =" n in tot" >
82+ <use-intersection-el-demo />
83+ </div >
84+ </div >
85+ </template >
7986
80- const el3Ref = ref (null )
81- const el3Class = ref (' ' )
82- const { observedEntry: el3Ob } = useIntersection (el3Ref, options)
83- watch (() => watchClass (el3Class, el3Ob, el3Ob .value ))
87+ <script lang =" ts" >
88+ import Vue from ' vue'
89+ import UseIntersectionElDemo from ' ./UseIntersectionElDemo.vue'
8490
85- return {
86- el1Ref,
87- el1Class,
88- el2Ref,
89- el2Class,
90- el3Ref,
91- el3Class
92- }
91+ export default Vue .extend ({
92+ name: ' UseIntersectionDemo' ,
93+ components: { UseIntersectionElDemo },
94+ setup () {
95+ const tot = new Array (100 )
96+ return { tot }
9397 }
9498 })
9599 </script >
96100
97101<style >
98102 .el-wrap {
99- margin : 500 px 0 ;
103+ margin : 200 px 0 ;
100104 }
101105
102106 .el {
0 commit comments