forked from ManukMinasyan/vue3-observe-visibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
170 lines (145 loc) · 4.15 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<html>
<head>
<style>
body {
font-family: sans-serif;
}
#app {
padding: 100px;
}
.toolbar {
position: fixed;
bottom: 12px;
left: 0;
width: 100%;
}
.toolbar .wrapper {
background: white;
padding: 12px;
border-radius: 3px;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.07);
display: flex;
align-items: center;
border: #eee 1px solid;
}
.toolbar .wrapper > *:not(:last-child) {
margin-right: 6px;
}
.separator {
margin: 0 12px;
}
.info {
color: #eee;
}
.test {
background: #eee;
height: 100%;
border-radius: 3px;
flex-direction: column;
}
.info,
.test {
font-size: 42px;
}
.test-wrapper {
margin: 3000px 0;
height: 400px;
}
.toolbar,
.info,
.test {
display: flex;
align-items: center;
justify-content: center;
}
input[type="number"] {
width: 90px;
}
.number {
display: inline-block;
width: 40px;
font-family: monospace;
text-align: center;
background: #eee;
border-radius: 3px;
padding: 4px 0;
}
</style>
</head>
<body>
<div id="app">
<div class="toolbar">
<div class="wrapper">
<input id="is-visible" type="checkbox" v-model="isVisible" disabled>
<label for="is-visible">Is visible?</label>
<input id="disabled" type="checkbox" v-model="disabled">
<label for="disabled">Disable</label>
<span class="separator"></span>
<button @click="show = !show">Toggle</button>
<span class="separator"></span>
<label for="throttle">Throttle</label>
<input id="throttle" type="number" v-model="throttle" min="0" step="100">
<input id="throttle-leading-visible" type="checkbox" v-model="leadingVisible">
<label for="throttle-leading-visible">Leading (Visible)</label>
<input id="throttle-leading-hidden" type="checkbox" v-model="leadingHidden">
<label for="throttle-leading-hidden">Leading (Hidden)</label>
<span class="separator"></span>
<label for="threshold">Threshold</label>
<span class="number">{{ threshold }}</span>
<input type="range" v-model="threshold" min="0" max="1" step="0.01">
<input id="once" type="checkbox" v-model="once">
<label for="once">Once</label>
</div>
</div>
<div class="info">Scroll down</div>
<div class="test-wrapper">
<div
v-show="show"
ref="test"
class="test"
v-observe-visibility="disabled ? false : {
callback: visibilityChanged,
throttle,
throttleOptions: {
leading: leadingVisible && leadingHidden ? 'both' : leadingVisible ? 'visible' : leadingHidden ? 'hidden' : false,
},
intersection: {
threshold,
},
once,
}"
>
<div v-for="n in count">Hello world!</div>
</div>
</div>
</div>
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<script src="./dist/vue-observe-visibility.min.js"></script>
<script type="module">
// App
const app = Vue.createApp({
data() {
return {
show: true,
isVisible: true,
throttle: 0,
leadingVisible: false,
leadingHidden: false,
threshold: 0,
count: 1,
once: false,
disabled: false,
}
},
methods: {
visibilityChanged: function (isVisible, entry) {
this.isVisible = isVisible
this.count++
console.log('is visible:', isVisible, 'intersection:', Math.round(entry.intersectionRatio * 100) + '%')
}
}
})
app.mount('#app')
</script>
</body>
</html>