forked from robflaherty/scroll-depth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrolldepth.js
149 lines (126 loc) · 4.19 KB
/
scrolldepth.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
;(function () {
"use strict";
var options = {
minHeight: 0,
elements: document.querySelectorAll('[data-track-scroll]'),
percentage: true,
userTiming: true,
pixelDepth: true,
nonInteraction: true
};
var cache = [];
/*
* Plugin
*/
// Return early if document height is too small
if ( document.body.clientHeight < options.minHeight ) {
return;
}
/*
* Functions
*/
function sendEvent(label) {
analytics.track(document.location.pathname, {
exposed: label
});
}
function calculateMarks(docHeight) {
return {
'25%' : parseInt(docHeight * 0.25, 10),
'50%' : parseInt(docHeight * 0.50, 10),
'75%' : parseInt(docHeight * 0.75, 10),
// 1px cushion to trigger 100% event in iOS
'100%': docHeight - 5
};
}
function checkMarks(marks, scrollDistance) {
// Check each active mark
for (var key in marks) {
if (cache.indexOf(key) === -1 && scrollDistance >= marks[key]) {
sendEvent(key);
cache.push(key);
}
}
}
function checkElements(elements, scrollDistance) {
[].forEach.call(elements, function(elem){
if ( cache.indexOf(elem) === -1 && cache.length ) {
if ( scrollDistance >= offSet(elem).top ) {
var label = elem.id || elem.classList.toString() || elem.nodeName;
console.log(label);
sendEvent(label);
cache.push(elem);
}
}
});
}
function offSet(elem){
var rect = elem.getBoundingClientRect();
return {
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft
}
}
/*
* 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;
};
}
/*
* Scroll Event
*/
var scrollHandler = throttle(function() {
/*
* We calculate document and window height on each scroll event to
* account for dynamic DOM changes.
*/
var docHeight = Math.max( document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight),
winHeight = window.innerHeight ? window.innerHeight : window.screen.height,
scrollDistance = document.querySelector('body').scrollTop + winHeight,
// Recalculate percentage marks
marks = calculateMarks(docHeight);
// If all marks already hit, unbind scroll event
if (cache.length >= 4 + options.elements.length) {
window.removeEventListener('scroll', scrollHandler);
return;
}
// Check specified DOM elements
if (options.elements) {
checkElements(options.elements, scrollDistance);
}
// Check standard marks
if (options.percentage) {
checkMarks(marks, scrollDistance);
}
}, 500);
window.addEventListener('scroll', scrollHandler);
})();