-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (77 loc) · 2.57 KB
/
index.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
import LocomotiveScroll from 'locomotive-scroll';
window.addEventListener('load', () => {
const scroll = new LocomotiveScroll({
el: document.querySelector('[data-scroll-container]'),
smooth: true,
});
let nav = document.querySelector('nav');
let lastScroll = 0;
scroll.on('scroll', (instance) => {
instance.scroll.y > 100 ? nav.classList.add('filled') : nav.classList.remove('filled');
if (instance.scroll.y < lastScroll) {
nav.style.top = '0';
} else {
nav.style.top = '-8rem';
}
lastScroll = instance.scroll.y;
});
scroll.on('call', (func) => {
if (func === 'countUp') {
let el = document.querySelector('#counter');
const animationDuration = 1500;
const frameDuration = 1000 / 60;
const totalFrames = Math.round(animationDuration / frameDuration);
const easeOutQuad = (t) => t * (2 - t);
let frame = 0;
const countTo = parseInt(el.innerHTML, 10);
const counter = setInterval(() => {
frame++;
const progress = easeOutQuad(frame / totalFrames);
const currentCount = Math.round(countTo * progress);
if (parseInt(el.innerHTML, 10) !== currentCount) {
el.innerHTML = currentCount;
}
if (frame === totalFrames) {
clearInterval(counter);
}
}, frameDuration);
}
if (func === 'update') {
scroll.update();
}
});
let ul = document.querySelector('#sidebar ul');
document.querySelectorAll('.project .header h1').forEach((el) => {
let item = document.createElement('li');
let itemLink = document.createElement('a');
itemLink.textContent = el.textContent;
itemLink.addEventListener('click', () => {
scroll.scrollTo(el, { offset: '-60px' });
});
item.appendChild(itemLink);
ul.appendChild(item);
});
});
let dropdowns = document.querySelectorAll('.dropdown');
let submenus = document.querySelectorAll('.submenu');
window.addEventListener('click', (e) => {
if (!e.target.classList.contains('submenu') && !e.target.classList.contains('dropdown')) {
dropdowns.forEach((item) => {
item.parentElement.querySelector('.submenu').classList.remove('open');
});
}
});
dropdowns.forEach((el) =>
el.addEventListener('click', () => {
let submenu = el.parentElement.querySelector('.submenu');
if (!submenu.classList.contains('open')) {
submenus.forEach((item) => {
item.classList.remove('open');
item.style.transition = 'all .5s ease';
});
submenu.classList.add('open');
} else {
submenu.classList.remove('open');
}
})
);