-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
54 lines (48 loc) · 1.65 KB
/
main.js
File metadata and controls
54 lines (48 loc) · 1.65 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
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const targetId = this.getAttribute('href').slice(1);
const target = document.getElementById(targetId);
if (target) {
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
});
// Section highlight in navbar
const navLinks = document.querySelectorAll('.nav-menu a');
const sectionIds = Array.from(navLinks)
.map(link => link.getAttribute('href'))
.filter(href => href.startsWith('#'))
.map(href => href.slice(1));
const sections = sectionIds
.map(id => document.getElementById(id))
.filter(Boolean);
function removeActive() {
navLinks.forEach(link => link.classList.remove('active'));
}
// IntersectionObserver for section highlighting
const observer = new IntersectionObserver(
entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const id = entry.target.id;
removeActive();
const activeLink = document.querySelector('.nav-menu a[href="#' + id + '"]');
if (activeLink) activeLink.classList.add('active');
}
});
},
{
root: null,
rootMargin: '-50% 0px -49% 0px', // triggers when section is in the middle of the viewport
threshold: 0
}
);
sections.forEach(section => {
observer.observe(section);
});
});
// Enable smooth scroll for the whole document (fallback)
document.documentElement.style.scrollBehavior = 'smooth';
console.log('Landing page script loaded.');