-
Notifications
You must be signed in to change notification settings - Fork 26
/
enter-view.js
165 lines (141 loc) · 3.93 KB
/
enter-view.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
/*
* enter-view.js is library
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
window.enterView = factory.call(this);
}
})(() => {
const lib = ({
selector,
enter = () => {},
exit = () => {},
progress = () => {},
offset = 0,
once = false
}) => {
let raf = null;
let ticking = false;
let elements = [];
let height = 0;
function setupRaf() {
raf =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
return setTimeout(callback, 1000 / 60);
};
}
function getOffsetHeight() {
if (offset && typeof offset === 'number') {
const fraction = Math.min(Math.max(0, offset), 1);
return height - fraction * height;
}
return height;
}
function updateHeight() {
const cH = document.documentElement.clientHeight;
const wH = window.innerHeight || 0;
height = Math.max(cH, wH);
}
function updateScroll() {
ticking = false;
const targetFromTop = getOffsetHeight();
elements = elements.filter(el => {
const { top, bottom, height } = el.getBoundingClientRect();
const entered = top < targetFromTop;
const exited = bottom < targetFromTop;
// enter + exit
if (entered && !el.__ev_entered) {
enter(el);
el.__ev_progress = 0;
progress(el, el.__ev_progress);
if (once) return false;
} else if (!entered && el.__ev_entered) {
el.__ev_progress = 0;
progress(el, el.__ev_progress);
exit(el);
}
// progress
if (entered && !exited) {
const delta = (targetFromTop - top) / height;
el.__ev_progress = Math.min(1, Math.max(0, delta));
progress(el, el.__ev_progress);
}
if (entered && exited && el.__ev_progress !== 1) {
el.__ev_progress = 1;
progress(el, el.__ev_progress);
}
el.__ev_entered = entered;
return true;
});
if (!elements.length) {
window.removeEventListener('scroll', onScroll, true);
window.removeEventListener('resize', onResize, true);
window.removeEventListener('load', onLoad, true);
}
}
function onScroll() {
if (!ticking) {
ticking = true;
raf(updateScroll);
}
}
function onResize() {
updateHeight();
updateScroll();
}
function onLoad() {
updateHeight();
updateScroll();
}
function selectionToArray(selection) {
const len = selection.length;
const result = [];
for (let i = 0; i < len; i += 1) {
result.push(selection[i]);
}
return result;
}
function selectAll(selector, parent = document) {
if (typeof selector === 'string') {
return selectionToArray(parent.querySelectorAll(selector));
} else if (selector instanceof NodeList) {
return selectionToArray(selector);
} else if (selector instanceof Array) {
return selector;
}
}
function setupElements() {
elements = selectAll(selector);
}
function setupEvents() {
window.addEventListener('resize', onResize, true);
window.addEventListener('scroll', onScroll, true);
window.addEventListener('load', onLoad, true);
onResize();
}
function init() {
if (!selector) {
console.error('must pass a selector');
return false;
}
setupElements();
if (!elements || !elements.length) {
console.error('no selector elements found');
return false;
}
setupRaf();
setupEvents();
updateScroll();
}
init();
};
return lib;
});