forked from vtimofeev/vue-check-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (180 loc) · 6.47 KB
/
index.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict'
function getPlugin() {
var ClassNames = {
Full: 'view-in--full',
In: 'view-in',
GtHalf: 'view-in--gt-half',
GtThird: 'view-in--gt-third',
Out: 'view-out',
Above: 'view-out--above',
Below: 'view-out--below'
},
EventTypes = {
Enter: 'enter',
Exit: 'exit',
Progress: 'progress'
}
function throttle(handler, timeout) {
timeout = typeof timeout !== 'undefined' ? timeout : 0
if (!handler || typeof handler !== 'function') throw new Error('Throttle handler argument is not incorrect. Must be a function.')
var timeoutTime = 0
return function (e) {
if (timeoutTime) return
timeoutTime = setTimeout(function () {
timeoutTime = 0
handler(e)
}, timeout)
}
}
function roundPercent(v) {
return (v * 1000 | 0) / 1000
}
function objectAssign(obj, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) obj[key] = src[key]
}
return obj
}
function createInstance(Vue, options) {
options = objectAssign({ throttleInterval: 16 }, options) // 60fps
var items = {},
scrollThrottledHandler = throttle(scrollHandler, options.throttleInterval)
var scrollValue = window.pageYOffset,
itemIndex = 0
window.addEventListener('scroll', scrollThrottledHandler)
window.addEventListener('resize', scrollThrottledHandler)
function scrollHandler(e) {
var viewportTop = window.pageYOffset,
viewportBottom = window.pageYOffset + window.document.documentElement.clientHeight,
viewportHeight = window.document.documentElement.clientHeight,
documentHeight = window.document.documentElement.scrollHeight,
scrollPercent = roundPercent(window.pageYOffset / (documentHeight - viewportHeight))
scrollValue = viewportTop - scrollValue
function getInType(i) {
var rect = i.element.getBoundingClientRect(),
elementTop = rect.top + viewportTop,
elementBottom = elementTop + rect.height,
topIn = elementTop > viewportTop && elementTop < viewportBottom,
bottomIn = elementBottom > viewportTop && elementBottom < viewportBottom,
fullyIn = rect.bottom >= viewportHeight && rect.top <= 0,
percentInView = fullyIn ? 1 : topIn || bottomIn ? ((bottomIn ? elementBottom : viewportBottom) - (topIn ? elementTop : viewportTop)) / rect.height : 0,
centerPercent = (elementTop - viewportTop + rect.height / 2) / viewportHeight,
zeroPoint = viewportTop - rect.height,
topPercent = (elementTop - zeroPoint) / (viewportBottom - zeroPoint),
isAbove = percentInView === 0 && elementTop < viewportTop,
isBelow = percentInView === 0 && elementTop > viewportTop
return [(topIn ? 1 : 0) | (bottomIn ? 2 : 0) | (isAbove ? 4 : 0) | (isBelow ? 8 : 0), roundPercent(percentInView), roundPercent(centerPercent), roundPercent(topPercent), rect]
}
for (var id in items) {
var i = items[id],
inType = getInType(i)
var type = inType[0],
percentInView = inType[1],
percentCenter = inType[2],
percentTop = inType[3],
rect = inType[4],
classes = i.classes,
classList = i.element.classList,
inViewChange = i.percent <= 0 && percentInView,
outViewChange = i.percent && percentInView === 0
if (percentInView === 0 && i.percent === 0) continue
i.rect = rect
var eventType = (inViewChange && EventTypes.Enter) || (outViewChange && EventTypes.Exit) || EventTypes.Progress
Object.keys(classes).forEach(function (v) {
classes[v] = false
})
if (percentInView >= 1) {
classes[ClassNames.Full] = true;
classes[ClassNames.In] = true;
}
if (percentInView >= 0.5) {
classes[ClassNames.GtHalf] = true
}
else if (percentInView >= 0.3) {
classes[ClassNames.GtThird] = true
}
if (type === 8) {
classes[ClassNames.Below] = true
classes[ClassNames.Out] = true
}
else if (type === 4) {
classes[ClassNames.Above] = true
classes[ClassNames.Out] = true
}
else if (type === 3) {
classes[ClassNames.Full] = true
classes[ClassNames.In] = true
}
else if (type === 1) {
classes[ClassNames.In] = true
}
else if (type === 2) {
classes[ClassNames.In] = true
}
Object.keys(classes).forEach(function (n) {
classList.toggle(n, classes[n])
if (!classes[n]) delete classes[n]
})
if (typeof i.handler === 'function') {
i.handler({
type: eventType,
percentInView: percentInView,
percentTop: percentTop,
percentCenter: percentCenter,
scrollPercent: scrollPercent,
scrollValue: scrollValue,
target: i
})
}
if (typeof i.onceenter === 'function' && eventType === EventTypes.Enter) {
i.onceenter({
type: eventType,
percentInView: percentInView,
percentTop: percentTop,
percentCenter: percentCenter,
scrollPercent: scrollPercent,
scrollValue: scrollValue,
target: i
})
delete i.onceenter
if (!i.persist) delete items[id]
}
i.percent = percentInView
}
scrollValue = viewportTop
}
Vue.directive('view', {
unbind: function (element, bind) {
delete items[element.$scrollId]
},
inserted: function (element, bind) {
var id = element.$scrollId || ('scrollId-' + itemIndex++),
item = items[id] || { element: element, classes: {}, percent: -1, rect: {} }
if (bind.modifiers && bind.modifiers.once) {
item.onceenter = bind.value || function () {
}
}
else {
item.persist = true
item.handler = bind.value
}
element.$scrollId = id
items[id] = item
scrollThrottledHandler()
}
})
}
return {
install: function (Vue, options) {
Vue.directive('view', Vue.prototype.$isServer ? {} : createInstance(Vue, options))
}
}
}
if (typeof exports === 'object' && typeof module !== 'undefined') {
module.exports = getPlugin()
}
else {
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(getPlugin(), { option: 'custom option of client' })
}
}