-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
55 lines (46 loc) · 1.71 KB
/
javascript.js
File metadata and controls
55 lines (46 loc) · 1.71 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
const menuToggle = document.querySelector(".menu-toggle");
const navLinks = document.querySelector(".nav-links");
const navItems = document.querySelectorAll(".nav-links a");
const revealItems = document.querySelectorAll(".reveal");
const sections = document.querySelectorAll("main section[id]");
if (menuToggle && navLinks) {
menuToggle.addEventListener("click", () => {
const isOpen = navLinks.classList.toggle("open");
menuToggle.setAttribute("aria-expanded", String(isOpen));
});
navItems.forEach((item) => {
item.addEventListener("click", () => {
navLinks.classList.remove("open");
menuToggle.setAttribute("aria-expanded", "false");
});
});
}
const revealObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
revealObserver.unobserve(entry.target);
}
});
}, {
threshold: 0.2
});
revealItems.forEach((item) => revealObserver.observe(item));
const setActiveLink = () => {
const scrollPosition = window.scrollY + 140;
sections.forEach((section) => {
const top = section.offsetTop;
const height = section.offsetHeight;
const id = section.getAttribute("id");
const link = document.querySelector(`.nav-links a[href="#${id}"]`);
if (!link) {
return;
}
if (scrollPosition >= top && scrollPosition < top + height) {
navItems.forEach((item) => item.classList.remove("active"));
link.classList.add("active");
}
});
};
window.addEventListener("scroll", setActiveLink);
window.addEventListener("load", setActiveLink);