-
Notifications
You must be signed in to change notification settings - Fork 23
/
scroll-tracker.js
658 lines (490 loc) · 13.9 KB
/
scroll-tracker.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/**
* Emits events based on scrolling behavior in a given context. Shouldn't
* be called until after DOMReady.
*
* @example
* var scrollTracker = ScrollTracker({
* context: '#content'
* });
*
* scrollTracker.on({
* percentage: {
* every: [25]
* }
* }, function(evt) {
*
* // Will trigger when the user reaches 25, 50, 75, & 100% depth
* notifySomeService(evt.data.scrollDepth);
*
* });
*
* Copyright(c) 2017 LunaMetrics, LLC.
* Written by @notdanwilkerson
* Licensed under the MIT License
* For full license text, visit https://opensource.org/licenses/MIT
*/
(function(window) {
'use strict';
// Won't work on IE8, so we install a mock.
if (window.navigator.userAgent.match(/MSIE [678]/gi)) return installMock();
var document = window.document;
/**
* @constructor
*
* @param {object} [opts] options for the constructor
* @param {HTMLElement} [opts.context] defaults to <body>
* @param {number} [opts.minHeight] minimum height of context required to track
*
* @returns {ScrollTracker}
*/
function ScrollTracker(opts) {
if (!(this instanceof ScrollTracker)) return new ScrollTracker(opts);
opts = opts || {};
var context = opts.context || 'body';
if (typeof context === 'string') context = document.querySelector(context);
if (!context) throw new Error('Unable to find context ' + context);
this._context = context;
this.minHeight = opts.minHeight || 0;
this._marks = {};
this._tracked = {};
this._config = {
percentages: {
each: {},
every: {}
},
pixels: {
each: {},
every: {}
},
elements: {
each: {},
every: {}
}
};
var boundAndThrottledDepthCheck = throttle(this._checkDepth.bind(this), 500);
var boundUpdate = this._update.bind(this);
var throttledUpdate = throttle(boundUpdate, 500);
window.addEventListener('scroll', boundAndThrottledDepthCheck, true);
window.addEventListener('resize', throttledUpdate);
this._artifacts = {
timer: onDocHeightChange(boundUpdate),
resize: throttledUpdate,
scroll: boundAndThrottledDepthCheck
};
}
/**
* Cleans up timer and event bindings
*/
ScrollTracker.prototype.destroy = function() {
clearInterval(this._artifacts._timer);
window.removeEventListener('resize', this._artifacts.resize);
window.removeEventListener('scroll', this._artifacts.scroll, true);
};
/**
* Registers a handler for a given configuration
*
* @param {object} config
* @param {object} [config.percentages]
* @param {number[]} [config.percentages.each] tracks every 100 / n percentage
* @param {number[]} [config.percentages.every] tracks each percentage once
* @param {object} [config.pixels]
* @param {number[]} [config.pixels.each] tracks every context.height() / n pixel depth
* @param {number[]} [config.pixels.every] tracks each pixel depth once
* @param {object} [config.elements]
* @param {string[]} [config.elements.each] tracks every element that matches each selector
* @param {string[]} [config.elements.every] tracks the first element that matches each selector
* @param {function} handler
*/
ScrollTracker.prototype.on = function(config, handler) {
var _config = this._config;
['percentages', 'pixels', 'elements'].forEach(function(type) {
if (!config[type]) return;
['each', 'every'].forEach(function(freq) {
if (!config[type][freq]) return;
config[type][freq].forEach(function(key) {
_config[type][freq][key] = _config[type][freq][key] || [];
_config[type][freq][key].push(handler);
});
});
});
this._update();
};
/**
* Checks marks and depth
*/
ScrollTracker.prototype._update = function() {
this._calculateMarks();
this._checkDepth();
};
/**
* Calculates the pixels for all configs
*/
ScrollTracker.prototype._calculateMarks = function() {
delete this._marks;
this._fromTop = getNodeDistanceFromTop(this._context);
this._marks = {};
var _config = this._config;
var contextHeight = this._contextHeight();
var addMark = this._addMark.bind(this);
var self = this;
var elements,
element,
depth,
key;
if (contextHeight < this.minHeight) return;
for (key in _config.percentages.every) {
forEachIn({
n: Number(key),
numerator: 100,
callback: percentagesEveryCallback(_config.percentages.every[key])
});
}
for (key in _config.pixels.every) {
forEachIn({
n: Number(key),
numerator: contextHeight,
callback: pixelsEveryCallback(_config.pixels.every[key])
});
}
for (key in _config.percentages.each) {
depth = Math.floor(contextHeight * Number(key) / 100);
addMark({
label: key + '%',
depth: depth,
handlers: _config.percentages.each[key]
});
}
for (key in _config.pixels.each) {
depth = Number(key);
addMark({
label: key + 'px',
depth: depth,
handlers: _config.pixels.each[key]
});
}
for (key in _config.elements.every) {
elements = [].slice.call(this._context.querySelectorAll(key));
if (elements.length) {
elements.forEach(elementsEveryCallback(key, _config.elements.every[key]));
}
}
for (key in _config.elements.each) {
element = this._context.querySelector(key);
if (element) {
depth = element.getBoundingClientRect().top -
self._context.getBoundingClientRect().top;
addMark({
label: key,
depth: depth,
handlers: _config.elements.each[key]
});
}
}
/**
* Callback for our everyElements iterations
*
* @param {string} key
* @param {function[]} handlers
*
* @returns {everyElement~Callback}
*/
function elementsEveryCallback(key, handlers) {
/**
* @callback everyElement~Callback
*
* @param {HTMLElement} element
* @param {number} ind
*/
return function(element, ind) {
var depth = element.getBoundingClientRect().top -
self._context.getBoundingClientRect().top;
addMark({
label: key + '[' + ind + ']',
depth: depth,
handlers: _config.elements.every[key]
});
};
}
/**
* Builds a callback for our everyPercentages iterations
*
* @param {function[]} handlers
*
* @returns {everyPercentage~Callback}
*/
function percentagesEveryCallback(handlers) {
/**
* @callback everyPercentage~Callback
*
* @param {number} n
*/
return function(n) {
var depth = Math.floor(n * contextHeight / 100);
addMark({
label: String(n) + '%',
depth: depth,
handlers: _config.percentages.every[key]
});
};
}
/**
* Builds a callback for our everyPixels iterations
*
* @param {function[]} handlers
*
* @param {number} n
*/
function pixelsEveryCallback(handlers) {
/**
* @callback everyPixel~Callback
*
* @param {function[]} handlers
*/
return function(n) {
var depth = n;
addMark({
label: String(depth) + 'px',
depth: depth,
handlers: handlers
});
};
}
};
/**
* Checks all marks and triggers appropriate handlers
*/
ScrollTracker.prototype._checkDepth = function() {
var marks = this._marks;
var currentDepth = this._currentDepth();
var key;
for (key in marks) {
if (currentDepth >= key && !this._tracked[key]) {
marks[key].forEach(function(boundHandler) {
boundHandler();
});
this._tracked[key] = true;
}
}
};
/**
* Resets the internal cache of tracked marks
*/
ScrollTracker.prototype.reset = function() {
this._tracked = {};
this._marks = {};
this._update();
};
/**
* Returns the height of the scrolling context
*
* @returns {number}
*/
ScrollTracker.prototype._contextHeight = function() {
if (this._context !== document.body) return this._context.scrollHeight - 5;
return this._context.clientHeight - 5;
};
/**
* Returns the current depth we've scrolled into the context
*
* @returns {number}
*/
ScrollTracker.prototype._currentDepth = function() {
var isVisible = visibleInViewport(this._context);
var depth;
if (!this._context.scrollTop) {
this._context.scrollTop = 1;
if (!this._context.scrollTop) {
depth = (window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop || 0) - this._fromTop;
} else {
this._context.scrollTop = 0;
depth = this._context.scrollTop + isVisible;
}
} else {
depth = this._context.scrollTop + isVisible;
}
if (!isVisible) {
return depth >= this._fromTop ? depth : -1;
}
return depth + isVisible;
};
/**
* Adds a mark to be tracked
*
* @param {object} config
* @param {number} config.depth
* @param {string} config.label
* @param {function[]} config.handlers
*/
ScrollTracker.prototype._addMark = function(config) {
var depth = config.depth;
this._marks[depth] = (this._marks[depth] || []).concat(Mark(config));
};
/**
* @constructor
* @private
*
* @param {object} config
* @param {string} config.label
* @param {number} config.depth
* @param {function[]} config.handlers
*
* @returns {Mark}
*/
function Mark(config) {
/**
* A Mark is an array of callbacks bound with their data payloads
*
* @name Mark
*
* @type {function[]}
*/
return config.handlers.map(function(handler) {
return handler.bind(this, {
data: {
depth: config.depth,
label: config.label
}
});
});
}
/**
* Calls a callback function each time config.n goes into config.numerator
*
* @param {object} config
* @param {number} config.n
* @param {number} config.numerator
* @param {function} config.callback
*/
function forEachIn(config) {
var len = Math.floor(config.numerator / config.n);
var i;
for (i = 1; i <= len; i++) {
config.callback(i * config.n);
}
}
/**
* Helper that watches for changes in the height of the document
*/
function onDocHeightChange(handler) {
var documentHeight = docHeight();
return setInterval(function() {
if (docHeight() !== documentHeight) {
handler();
documentHeight = docHeight();
}
}, 500);
}
/**
* Returns the height of the document
*
* @returns {number}
*/
function docHeight() {
var body = document.body;
var html = document.documentElement;
return Math.max(body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight);
}
/**
* Returns the number of pixels of the element visible in the viewport
* @param {HTMLElement} element
*
* @returns {number}
* adapted from:
* @link https://stackoverflow.com/questions/24768795/get-the-visible-height-of-a-div-with-jquery#answer-26831113
*/
function visibleInViewport(element) {
var height = element.offsetHeight;
var windowHeight = viewportHeight();
var rect = element.getBoundingClientRect();
return Math.max(
0,
rect.top > 0 ? Math.min(height, windowHeight - rect.top) :
(rect.bottom < windowHeight ? rect.bottom : windowHeight)
);
}
/**
* Returns the height of the viewport
*
* @returns {number}
*/
function viewportHeight() {
var elem = (document.compatMode === "CSS1Compat") ?
document.documentElement :
document.body;
return elem.clientHeight;
}
/**
* Retrieves the distance of a node from the top of the document
*
* @param {HTMLElement} node
*
* @returns {number}
*/
function getNodeDistanceFromTop(node) {
var nodeTop = node.getBoundingClientRect().top - (node.scrollHeight - node.clientHeight) / 2;
// @link https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollX
var docTop = (window.pageYOffset !== undefined) ?
window.pageYOffset :
(document.documentElement || document.body.parentNode || document.body).scrollTop;
return nodeTop + docTop;
}
/**
* Does nothing
*/
function noop() {}
/**
* Throttle function borrowed from:
* Underscore.js 1.5.2
* http://underscorejs.org
* (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Underscore may be freely distributed under the MIT license.
*/
function throttle(func, wait) {
var context, args, result;
var timeout = null;
var previous = 0;
var later = function() {
previous = new Date;
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date;
if (!previous) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
}
/**
* Installs a noop'd version of ScrollTracker on the window
*/
function installMock() {
var fake = {};
var key;
for (key in ScrollTracker) {
fake[key] = noop;
}
window.ScrollTracker = fake;
}
window.ScrollTracker = ScrollTracker;
})(this);
/*
* v2.0.4
* Created by the Google Analytics consultants at http://www.lunametrics.com/
* Written by @notdanwilkerson
* Documentation: https://github.com/lunametrics/gascroll/
* Licensed under the MIT License
*/