forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderAnimation.js
More file actions
48 lines (42 loc) · 1.19 KB
/
headerAnimation.js
File metadata and controls
48 lines (42 loc) · 1.19 KB
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
document.addEventListener('DOMContentLoaded', () => {
const steps = ['full', 'mobile', 'desktop', 'laptop', 'mobile2', 'full2'];
const intervals = [1250, 1500, 1500, 1500, 1500, 1250];
let i = 0;
const timeouts = [];
const logo = document.querySelector('.LogoAnimation');
function animateStep() {
const prev = steps[i];
logo.classList.remove(prev);
i = (i + 1) % steps.length;
const current = steps[i];
const timeout = intervals[i];
logo.classList.add(current);
timeouts.push(setTimeout(animateStep, timeout));
}
// only start the animation if the document is visible on load
if (!document.hidden) {
timeouts.push(
setTimeout(() => {
logo.classList.remove('init');
animateStep();
}, 2000)
);
}
// https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
document.addEventListener(
'visibilitychange',
() => {
if (document.hidden) {
timeouts.forEach(timeout => {
clearTimeout(timeout);
});
// clear the timeouts array
timeouts.length = 0;
} else {
// restart the animation when visible
animateStep();
}
},
false
);
});