-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (59 loc) · 2.41 KB
/
Copy pathscript.js
File metadata and controls
72 lines (59 loc) · 2.41 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
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
document.addEventListener('DOMContentLoaded', () => {
const sections = document.querySelectorAll('.page');
const navLinks = document.querySelectorAll('header nav a');
// --- Set initial state ---
// On page load, explicitly set the "Home" link as active.
const homeLink = document.querySelector('header nav a[href="#home"]');
if (homeLink) {
homeLink.classList.add('active-link');
}
// --- Active Nav Link on Scroll ---
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.4
};
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
navLinks.forEach(link => link.classList.remove('active-link'));
const id = entry.target.getAttribute('id');
const activeLink = document.querySelector(`header nav a[href="#${id}"]`);
if (activeLink) {
activeLink.classList.add('active-link');
}
}
});
}, observerOptions);
sections.forEach(section => {
observer.observe(section);
});
// --- Keyboard Scroll Navigation (Simplified) ---
// With a "Home" link, we no longer need a special case.
const sectionOrder = Array.from(sections).map(section => section.id);
document.addEventListener('keydown', (e) => {
const activeLink = document.querySelector('header nav a.active-link');
if (!activeLink) return;
const currentId = activeLink.getAttribute('href').substring(1);
const currentIndex = sectionOrder.indexOf(currentId);
if (currentIndex === -1) return;
let nextIndex = -1;
if (e.key === 'ArrowDown' || e.key === 'ArrowRight') {
if (currentIndex < sectionOrder.length - 1) {
nextIndex = currentIndex + 1;
}
} else if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') {
if (currentIndex > 0) {
nextIndex = currentIndex - 1;
}
}
if (nextIndex !== -1) {
e.preventDefault();
const nextSectionId = sectionOrder[nextIndex];
const nextSection = document.getElementById(nextSectionId);
if (nextSection) {
nextSection.scrollIntoView({ behavior: 'smooth' });
}
}
});
});