forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdissectionAnimation.js
More file actions
41 lines (35 loc) · 1.47 KB
/
dissectionAnimation.js
File metadata and controls
41 lines (35 loc) · 1.47 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
document.addEventListener('DOMContentLoaded', () => {
const section = document.querySelector('.NativeDevelopment');
const dissection = document.querySelector('.NativeDevelopment .dissection');
const images = dissection.children;
const numImages = images.length;
const fadeDistance = 40;
const navbarHeight = 60;
function clamp(val, min, max) {
return Math.min(max, Math.max(min, val));
}
// Scale the percent so that `min` goes to 0% and `max` goes to 100%
function scalePercent(percent, min, max) {
const scale = max - min;
return clamp((percent - min) / scale, 0, 1);
}
// Get the percentage that the image should be on the screen given
// how much the entire container is scrolled
// so we can fine-tune at what screen % the animation starts and stops
function getImagePercent(index, scrollPercent) {
const start = index / numImages;
return clamp((scrollPercent - start) * numImages, 0, 1);
}
window.addEventListener('scroll', () => {
const elPos = section.getBoundingClientRect().top - navbarHeight;
const height = window.innerHeight;
const screenPercent = 1 - clamp(elPos / height, 0, 1);
const scaledPercent = scalePercent(screenPercent, 0.2, 0.9);
for (let i = 0; i < numImages; i++) {
const imgPercent = getImagePercent(i, scaledPercent);
images[i].style.opacity = imgPercent;
const translation = fadeDistance * (1 - imgPercent);
images[i].style.left = `${translation}px`;
}
});
});