forked from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
timeline.js
53 lines (43 loc) · 1.25 KB
/
timeline.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
"use strict";
function qs(selector, all = false) {
return all ? document.querySelectorAll(selector) : document.querySelector(selector);
}
const sections = qs('.section', true);
const timeline = qs('.tl');
const line = qs('.line');
line.style.bottom = `calc(100% - 20px)`;
let prevScrollY = window.scrollY;
let up, down;
let full = false;
let set = 0;
const targetY = window.innerHeight * .8;
function scrollHandler(e) {
const {
scrollY
} = window;
up = scrollY < prevScrollY;
down = !up;
const timelineRect = timeline.getBoundingClientRect();
const lineRect = line.getBoundingClientRect();
const dist = targetY - timelineRect.top;
console.log(dist);
if (down && !full) {
set = Math.max(set, dist);
line.style.bottom = `calc(100% - ${set}px)`;
}
if (dist > timeline.offsetHeight + 50 && !full) {
full = true;
line.style.bottom = `-50px`;
}
sections.forEach(item => {
// console.log(item);
const rect = item.getBoundingClientRect(); // console.log(rect);
if (rect.top + item.offsetHeight / 5 < targetY) {
item.classList.add('show-me');
}
}); // console.log(up, down);
prevScrollY = window.scrollY;
}
scrollHandler();
line.style.display = 'block';
window.addEventListener('scroll', scrollHandler);