-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
38 lines (32 loc) · 1.33 KB
/
scripts.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
document.addEventListener("DOMContentLoaded", function() {
const links = document.querySelectorAll("nav a");
links.forEach(link => {
link.addEventListener("click", function(event) {
const targetId = event.currentTarget.getAttribute("href");
if (targetId !== "#home") {
smoothScroll(event, targetId);
}
});
});
function smoothScroll(event, targetId) {
event.preventDefault();
const targetElement = document.querySelector(targetId);
if (targetElement) {
const targetPosition = targetElement.offsetTop;
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
const duration = 1000;
let startTimestamp = null;
function animation(timestamp) {
if (!startTimestamp) startTimestamp = timestamp;
const progress = timestamp - startTimestamp;
const percentage = Math.min(progress / duration, 1);
window.scrollTo(0, startPosition + distance * percentage);
if (progress < duration) {
requestAnimationFrame(animation);
}
}
requestAnimationFrame(animation);
}
}
});