-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpeekaboo.js
407 lines (358 loc) · 15.4 KB
/
peekaboo.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*! peekaboo v1.0.0 MIT/GPL2 @freqdec */
(function(window, document) {
"use strict";
/** @global */
var peekaboo = (function() {
/**
* The number of pixels outside of the viewport to take into account
* while calculating if an element is visible
* @var {Number} _rootMargin
*/
var _rootMargin = 150,
/**
* An Object holding the patterns & callback functions declared
* using the "observe" method of the peekaboo Object
* @var {Object} observers
*/
observers = {},
/**
* Have we added old-school scroll & resize listeners
* @var {Boolean} listenersAdded
*/
listenersAdded,
/**
* A reference to the scroll/resize event target
* @var {Object} eventTarget
*/
eventTarget,
/**
* Flag for determining if we can queue a requestAnimationFrame
* @var {Boolean} tick
*/
tick,
/**
* Used to cache the window width once per run
* @var {Number} windowWidth
*/
windowWidth,
/**
* Used to cache the window height once per run
* @var {Number} windowHeight
*/
windowHeight,
/**
* Does the browser support the IntersectionObserver Interface
* @var {Boolean} supportsIO
*/
supportsIO = ("IntersectionObserver" in window),
/**
* An key/value store for the IntersectionObserver
* @var {Object} iO
*/
iO = {};
/**
* Used as nodeList doesn't have a reliable cross browser forEach
* method.
*
* @param {Array} array The Array to iterate over
* @param {Function} callback The function to apply to each Array member
* @param {Object} scope The scope in which to run the callback within
*/
var forEach = function(array, callback, scope) {
for (var i = 0; i < array.length; i++) {
callback.call(scope, array[i]);
}
}
/**
* Checks an element is not hidden in the overflow of a parent element
* or the viewport.
*
* @param {HTMLElement} el The DOM element to check
* @returns {Boolean}
*/
var meanIsVisible = function(el, rootMargin) {
var rect = el.getBoundingClientRect(),
top = rect.top,
left = rect.left,
height = rect.height,
width = rect.width,
el = el.parentNode;
// Check we are not hidden in the overflow of a parent container.
// This is an expensive calculation and the much faster
// leanIsVisible function should be used (by using the
// fastVisibilityCheck option) whenever possible.
while (el != document.body) {
rect = el.getBoundingClientRect();
if (!checkElementInBounds(top, left, width, height, rect.top,
rect.left, rect.width, rect.height, rootMargin)) {
return false;
}
el = el.parentNode;
}
// Finally do a viewport check
return checkElementInBounds(top, left, width, height, 0, 0,
windowWidth, windowHeight, rootMargin);
}
/**
* Checks an element is not hidden in the overflow of the viewport. If
* using this function then elements hidden in the overflow of a parent
* container will not have their overflow calculated correctly.
*
* @param {HTMLElement} el The DOM element to check
* @returns {Boolean}
*/
var leanIsVisible = function(el, rootMargin) {
var rect = el.getBoundingClientRect();
return checkElementInBounds(rect.top, rect.left, rect.width,
rect.height, 0, 0, windowWidth, windowHeight, rootMargin);
}
/**
* Checks if a box is within the viewable bounds of a second box. The
* value of rootMargin is taken into account during the calculation.
*
* @param {Number} t The top position of the first box
* @param {Number} l The left position of the first box
* @param {Number} w The width of the first box
* @param {Number} h The height of the first box
* @param {Number} eT The top position of the second box
* @param {Number} eL The left position of the second box
* @param {Number} eH The height of the second box
* @param {Number} eW The width of the second box
* @param {Number} rM The root margin to take into account
* @returns {Boolean}
*/
var checkElementInBounds = function(t, l, w, h, eT, eL, eW, eH, rM) {
return !(t < eT - (h + rM)
|| t > eT + eH + rM
|| l < eL - (w + rM)
|| l > eL + eW + rM);
}
/**
* requestAnimation frame will call this function which basically cycles
* through the saved CSS Selectors, grabs the set of matching
* elements and tests which are within the container viewport.
*
* @param {DOMHighResTimeStamp|Boolean} recalculateElementList A
* Number representing a DOMHighResTimeStamp when called by
* requestAnimationFrame and a Boolean true value when called by the
* peekaboo.observe method. When the true value is passed, the element
* list for all patterns is recalculated and the internal cache
* updated
*/
var updateElements = function(recalculateElementList) {
var evtTarget = document,
elemRevealTotal = 0,
elemTotal = 0,
elemReveal,
rootMargin,
inViewport,
pattern;
windowWidth = window.innerWidth;
windowHeight = window.innerHeight;
// Are we in a scroll container or the document
if (typeof eventTarget === "undefined"
|| typeof eventTarget.tagName === "undefined") {
evtTarget = document;
} else if (eventTarget){
evtTarget = eventTarget;
}
for (pattern in observers) {
if (observers.hasOwnProperty(pattern)) {
// Can also be a DOMHighResTimeStamp so we need the ===
if (recalculateElementList === true) {
// IE9+ due to use of Array.prototype.slice.call
observers[pattern].elemList = Array.prototype.slice.call(evtTarget.querySelectorAll(pattern + ":not([data-peekaboo])"));
}
elemReveal = [];
elemTotal += observers[pattern].elemList.length;
rootMargin = observers[pattern].rootMargin;
inViewport = observers[pattern].fastVisibilityCheck ? leanIsVisible : meanIsVisible;
// IE9+ due to use of filter
observers[pattern].elemList = observers[pattern].elemList.filter(function(elem) {
if (inViewport(elem, rootMargin)) {
elem.setAttribute("data-peekaboo", pattern);
elemReveal.push(elem);
return false;
}
return true;
});
if (elemReveal.length) {
elemRevealTotal += elemReveal.length;
observers[pattern].callback(elemReveal);
}
}
}
if (elemTotal - elemRevealTotal > 0 && !listenersAdded) {
addOldSchoolListeners();
} else if (!elemTotal && listenersAdded) {
removeOldSchoolListeners();
}
tick = false;
}
/**
* Adds the scroll & resize events for browsers not using the
* IntersectionObserver.
*/
var addOldSchoolListeners = function() {
document.addEventListener("scroll", scrollResizeHandler, true);
window.addEventListener("resize", scrollResizeHandler);
listenersAdded = true;
}
/**
* Removes the scroll & resize events for browsers not using the
* IntersectionObserver.
*/
var removeOldSchoolListeners = function() {
document.removeEventListener("scroll", scrollResizeHandler);
window.removeEventListener("resize", scrollResizeHandler);
listenersAdded = false;
}
/**
* Callback for the scroll and resize events for browsers that have no
* IntersectionObserver support.
*
* @param {UIEvent} e scroll/resize event Object
*/
var scrollResizeHandler = function(e) {
if (!tick) {
eventTarget = e && e.target ? e.target : undefined;
requestAnimationFrame(updateElements);
}
tick = true;
}
/**
* Passes any required elements to the IntersectionObserver.
*/
var observeElements = function() {
var pattern,
rootMargin;
for(pattern in observers) {
if (observers.hasOwnProperty(pattern)) {
rootMargin = observers[pattern].rootMargin;
if (!iO.hasOwnProperty(rootMargin)) {
iO[rootMargin] = new IntersectionObserver(
observerCallback,
{
"rootMargin": rootMargin + "px"
}
);
}
// Only grab elements we are not already observing
forEach(document.querySelectorAll(pattern + ":not([data-peekaboo])"), function(elem) {
elem.setAttribute("data-peekaboo", pattern);
iO[rootMargin].observe(elem);
});
}
}
}
/**
* Stops the elements that match the CSS Selector pattern from being
* observed and cleans up any IntersectionObservers now not being used.
*
* @param {String} pattern The CSS Selector pattern to stop observing
*/
var unobserveElements = function(pattern) {
var elemList,
rootMargin;
if (observers.hasOwnProperty(pattern)) {
rootMargin = observers[pattern].rootMargin;
forEach(document.querySelectorAll(pattern), function(elem) {
elem.removeAttribute("data-peekaboo");
if (supportsIO) {
iO[rootMargin].unobserve(elem);
}
});
delete observers[pattern];
if (iO.hasOwnProperty(rootMargin)) {
for(pattern in observers) {
if (observers.hasOwnProperty(pattern) && observers[pattern].rootMargin == rootMargin) {
return;
}
}
delete iO[rootMargin];
}
}
}
/**
* Callback for the IntersectionObserver.
*
* @param {Array} elemList An Array of IntersectionObserverEntry Objects
*/
var observerCallback = function(elemList) {
var cbList = {},
pattern;
elemList.forEach(function(elem) {
var pattern = elem.target.getAttribute("data-peekaboo");
if (!observers.hasOwnProperty(pattern)
|| elem.intersectionRatio <= 0) {
return;
}
if (!(pattern in cbList)) {
cbList[pattern] = [];
}
cbList[pattern].push(elem.target);
iO[observers[pattern].rootMargin].unobserve(elem.target);
elem.target.removeAttribute("data-peekaboo");
});
for(pattern in cbList) {
if (cbList.hasOwnProperty(pattern)) {
observers[pattern].callback(cbList[pattern]);
}
}
}
/**
* Creates and passes back the public API Object.
*
* @param {Function|Boolean} observeFunction A function to be used as
* the peekaboo.observe public API method call or false if we are on
* older non-compliant browsers and wish to return a public API stub
*/
var init = function(observeFunction) {
return observeFunction === false ? {
"observe": function() {},
"unobserve": function() {},
"supported": false
} : {
"observe": function(argObj) {
if (typeof argObj === "object") {
if (!typeof argObj.callback === "function") {
throw new Error("No callback function passed within initialisation Object [callback]");
} else if (!(argObj.hasOwnProperty("pattern") && argObj.pattern)) {
throw new Error("No CSS Selector passed within initialisation Object [pattern]");
}
// I can't find a better way to test the validity of a CSS Selector
try {
document.querySelectorAll(argObj.pattern + ":not([data-peekaboo])");
} catch (e) {
throw new Error("Could not instantiate the CSS Selector [pattern]: " + argObj.pattern);
}
if (argObj.pattern in observers) {
throw new Error("The CSS Selector [pattern] is already being observed: " + argObj.pattern);
}
observers[argObj.pattern] = {
"callback": argObj.callback,
"fastVisibilityCheck": !!argObj.fastVisibilityCheck,
"rootMargin": argObj.hasOwnProperty("rootMargin") && parseInt(argObj.rootMargin, 10) == argObj.rootMargin ? argObj.rootMargin : _rootMargin
}
}
observeFunction(true);
},
"unobserve": unobserveElements,
"supported": true
};
}
// Chrome only as of time of writing...
if ("IntersectionObserver" in window) {
return init(observeElements);
// Other newish browsers (IE10+)
} else if ("requestAnimationFrame" in window
&& "querySelectorAll" in document
&& "addEventListener" in document
&& "getBoundingClientRect" in document.createElement('div')) {
return init(updateElements);
}
// Return a polite non-breaking API stub for non compliant browsers
return init(false);
})();
window.peekaboo = peekaboo;
})(window, document);