forked from dejwid/tesla-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll.js
More file actions
32 lines (27 loc) · 973 Bytes
/
scroll.js
File metadata and controls
32 lines (27 loc) · 973 Bytes
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
document.lastScrollPosition = 0;
document.lastCentered = 0;
document.onWayTo = null;
document.addEventListener('scroll', () => {
const direction = window.pageYOffset - document.lastScrollPosition > 0 ? 'down' : 'up';
const sections = [...document.querySelectorAll('section')];
if (document.onWayTo === null) {
const destIndex = direction === 'up' ? document.lastCentered - 1 : document.lastCentered + 1;
if (destIndex >= 0 && destIndex < sections.length) {
console.log({destIndex,direction});
document.onWayTo = destIndex;
window.scroll(0, sections[destIndex].offsetTop);
}
}
sections.forEach((section,index) => {
if (window.pageYOffset === section.offsetTop) {
document.lastCentered = index;
section.className = 'active';
if (document.onWayTo === index) {
document.onWayTo = null;
}
} else {
section.className = '';
}
})
document.lastScrollPosition = window.pageYOffset;
})