-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
48 lines (37 loc) · 1.33 KB
/
script.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
document.addEventListener('DOMContentLoaded', function () {
const links = document.querySelectorAll('.navbar a');
links.forEach(link => {
link.addEventListener('click', scrollToSection);
});
function scrollToSection(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
let targetElement;
if (targetId === 'bottom') {
targetElement = document.documentElement;
} else {
targetElement = document.getElementById(targetId);
}
if (targetElement) {
smoothScrollTo(targetElement.offsetTop, 1000);
}
}
function smoothScrollTo(targetPosition, duration) {
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
const startTime = performance.now();
function scrollAnimation(currentTime) {
const elapsedTime = currentTime - startTime;
const scrollProgress = Math.min(elapsedTime / duration, 1);
const scrollValue = startPosition + distance * easeInOutQuad(scrollProgress);
window.scrollTo(0, scrollValue);
if (scrollProgress < 1) {
requestAnimationFrame(scrollAnimation);
}
}
function easeInOutQuad(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}
requestAnimationFrame(scrollAnimation);
}
});