This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
in-viewport.js
281 lines (238 loc) · 6.74 KB
/
in-viewport.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
module.exports = inViewport;
var instances = [];
var supportsMutationObserver = typeof global.MutationObserver === 'function';
function inViewport(elt, params, cb) {
var opts = {
container: global.document.body,
offset: 0,
debounce: 15,
failsafe: 150
};
if (params === undefined || typeof params === 'function') {
cb = params;
params = {};
}
var container = opts.container = params.container || opts.container;
var offset = opts.offset = params.offset || opts.offset;
var debounceValue = opts.debounce = params.debounce || opts.debounce;
var failsafe = opts.failsafe = params.failsafe || opts.failsafe;
// ensure backward compatibility with failsafe as boolean
if (failsafe === true) {
failsafe = 150;
} else if(failsafe === false) {
failsafe = 0;
}
// failsafe check always needs to be higher than debounceValue
if (failsafe > 0 && failsafe < debounceValue) {
failsafe = debounceValue + 50;
}
for (var i = 0; i < instances.length; i++) {
if (
instances[i].container === container &&
instances[i]._debounce === debounceValue &&
instances[i]._failsafe === failsafe
) {
return instances[i].isInViewport(elt, offset, cb);
}
}
return instances[
instances.push(createInViewport(container, debounceValue, failsafe)) - 1
].isInViewport(elt, offset, cb);
}
function addEvent(el, type, fn) {
if (el.attachEvent) {
el.attachEvent('on' + type, fn);
} else {
el.addEventListener(type, fn, false);
}
}
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
function later() {
timeout = null;
if (!immediate) func.apply(context, args);
}
};
}
// https://github.com/jquery/sizzle/blob/3136f48b90e3edc84cbaaa6f6f7734ef03775a07/sizzle.js#L708
var contains = function() {
if (!global.document) {
return true;
}
return global.document.documentElement.compareDocumentPosition ?
function (a, b) {
return !!(a.compareDocumentPosition(b) & 16);
} :
global.document.documentElement.contains ?
function (a, b) {
return a !== b && ( a.contains ? a.contains(b) : false );
} :
function (a, b) {
while (b = b.parentNode) {
if (b === a) {
return true;
}
}
return false;
};
}
function createInViewport(container, debounceValue, failsafe) {
var watches = createWatches();
var scrollContainer = container === global.document.body ? global : container;
var debouncedCheck = debounce(watches.checkAll(watchInViewport), debounceValue);
addEvent(scrollContainer, 'scroll', debouncedCheck);
if (scrollContainer === global) {
addEvent(global, 'resize', debouncedCheck);
}
if (supportsMutationObserver) {
observeDOM(watches, container, debouncedCheck);
}
// failsafe check, every X we check for visible images
// usecase: a hidden parent containing eleements
// when the parent becomes visible, we have no event that the children
// became visible
if (failsafe > 0) {
setInterval(debouncedCheck, failsafe);
}
function isInViewport(elt, offset, cb) {
if (!cb) {
return isVisible(elt, offset);
}
var remote = createRemote(elt, offset, cb);
remote.watch();
return remote;
}
function createRemote(elt, offset, cb) {
function watch() {
watches.add(elt, offset, cb);
}
function dispose() {
watches.remove(elt);
}
return {
watch: watch,
dispose: dispose
};
}
function watchInViewport(elt, offset, cb) {
if (isVisible(elt, offset)) {
watches.remove(elt);
cb(elt);
}
}
function isVisible(elt, offset) {
if (!elt) {
return false;
}
if (!contains(global.document.documentElement, elt) || !contains(global.document.documentElement, container)) {
return false;
}
// Check if the element is visible
// https://github.com/jquery/jquery/blob/740e190223d19a114d5373758127285d14d6b71e/src/css/hiddenVisibleSelectors.js
if (!elt.offsetWidth || !elt.offsetHeight) {
return false;
}
var eltRect = elt.getBoundingClientRect();
var viewport = {};
if (container === global.document.body) {
viewport = {
top: -offset,
left: -offset,
right: global.document.documentElement.clientWidth + offset,
bottom: global.document.documentElement.clientHeight + offset
};
} else {
var containerRect = container.getBoundingClientRect();
viewport = {
top: containerRect.top - offset,
left: containerRect.left - offset,
right: containerRect.right + offset,
bottom: containerRect.bottom + offset
};
}
// The element must overlap with the visible part of the viewport
var visible =
(
eltRect.right >= viewport.left &&
eltRect.left <= viewport.right &&
eltRect.bottom >= viewport.top &&
eltRect.top <= viewport.bottom
);
return visible;
}
return {
container: container,
isInViewport: isInViewport,
_debounce: debounceValue,
_failsafe: failsafe
};
}
function createWatches() {
var watches = [];
function add(elt, offset, cb) {
if (!isWatched(elt)) {
watches.push([elt, offset, cb]);
}
}
function remove(elt) {
var pos = indexOf(elt);
if (pos !== -1) {
watches.splice(pos, 1);
}
}
function indexOf(elt) {
for (var i = watches.length - 1; i >= 0; i--) {
if (watches[i][0] === elt) {
return i;
}
}
return -1;
}
function isWatched(elt) {
return indexOf(elt) !== -1;
}
function checkAll(cb) {
return function () {
for (var i = watches.length - 1; i >= 0; i--) {
cb.apply(this, watches[i]);
}
};
}
return {
add: add,
remove: remove,
isWatched: isWatched,
checkAll: checkAll
};
}
function observeDOM(watches, container, cb) {
var observer = new MutationObserver(watch);
var filter = Array.prototype.filter;
var concat = Array.prototype.concat;
observer.observe(container, {
childList: true,
subtree: true,
// changes like style/width/height/display will be catched
attributes: true
});
function watch(mutations) {
// some new DOM nodes where previously watched
// we should check their positions
if (mutations.some(knownNodes) === true) {
setTimeout(cb, 0);
}
}
function knownNodes(mutation) {
var nodes = concat.call([],
Array.prototype.slice.call(mutation.addedNodes),
mutation.target
);
return filter.call(nodes, watches.isWatched).length > 0;
}
}