forked from vvo/in-viewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin-viewport.js
267 lines (224 loc) · 6.35 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
module.exports = inViewport;
var instances = [];
var supportsMutationObserver = typeof global.MutationObserver === 'function';
function inViewport(elt, params, cb) {
var opts = {
container: global.document.body,
offset: 0
};
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;
for (var i = 0; i < instances.length; i++) {
if (instances[i].container === container) {
return instances[i].isInViewport(elt, offset, cb);
}
}
return instances[
instances.push(createInViewport(container)) - 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 = 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) {
var watches = createWatches();
var scrollContainer = container === global.document.body ? global : container;
var debouncedCheck = debounce(watches.checkAll(watchInViewport), 15);
addEvent(scrollContainer, 'scroll', debouncedCheck);
if (scrollContainer === global) {
addEvent(global, 'resize', debouncedCheck);
}
if (supportsMutationObserver) {
observeDOM(watches, container, debouncedCheck);
}
// failsafe check, every 200ms 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
setInterval(debouncedCheck, 150);
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 (!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 containerRect = container.getBoundingClientRect();
var pos = {
left: eltRect.left,
top: eltRect.top
};
var viewport = {
width: offset,
height: offset
};
if (container === global.document.body) {
viewport.width += global.document.documentElement.clientWidth;
viewport.height += global.document.documentElement.clientHeight;
// We update body rect computing because
// when you have relative/absolute childs, you get bad compute
// we need to create a new Object, because it's read only
containerRect = {
bottom: container.scrollHeight,
top: 0,
left: 0,
right: container.scrollWidth
};
} else {
pos.left -= containerRect.left;
pos.top -= containerRect.top;
viewport.width += container.clientWidth;
viewport.height += container.clientHeight;
}
var visible =
// 1. They must overlap
!(
eltRect.right < containerRect.left ||
eltRect.left > containerRect.right ||
eltRect.bottom < containerRect.top ||
eltRect.top > containerRect.bottom
) && (
// 2. They must be visible in the viewport
pos.top <= viewport.height &&
pos.left <= viewport.width
);
return visible;
}
return {
container: container,
isInViewport: isInViewport
};
}
function createWatches() {
var watches = [];
function add(elt, offset, cb) {
setTimeout(function () {
if (!isWatched(elt)) {
watches.push([elt, offset, cb]);
}
}, 0);
}
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;
}
}