-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (38 loc) · 1.57 KB
/
script.js
File metadata and controls
46 lines (38 loc) · 1.57 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
document.addEventListener('DOMContentLoaded', () => {
const scrollDownButton = document.querySelector('.glass-box');
const scrollToLinks = document.querySelectorAll('.scroll-to');
const mainContent = document.querySelector('.main-content');
function easeInOutExpo(t) {
return t < 0.5
? Math.pow(2, 10 * (t - 1)) * 0.5
: (2 - Math.pow(2, -10 * (t - 0.5))) * 0.5;
}
function smoothScroll(targetElement, duration = 1000) {
const startPosition = window.pageYOffset;
const targetPosition = targetElement.getBoundingClientRect().top;
const startTime = performance.now();
function animation(currentTime) {
const elapsed = currentTime - startTime;
const t = Math.min(elapsed / duration, 1);
const eased = easeInOutExpo(t);
const newPosition = startPosition + eased * targetPosition;
window.scrollTo(0, newPosition);
if (elapsed < duration) {
requestAnimationFrame(animation);
}
}
requestAnimationFrame(animation);
}
if (scrollDownButton) {
scrollDownButton.addEventListener('click', () => {
smoothScroll(mainContent);
});
}
scrollToLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetElement = document.querySelector(`#${link.getAttribute('data-target')}`);
if (targetElement) smoothScroll(targetElement);
});
});
});