-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.js
48 lines (36 loc) · 1.23 KB
/
main.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
// Mobile Navigation
const mobileNav = document.querySelector(".mobile-nav");
const menuIcon = document.querySelector(".menu-icon");
const mobileMenuItems = document.querySelectorAll(".mobile-nav .menu-items li");
menuIcon.addEventListener("click", () => {
mobileNav.classList.toggle("active");
});
mobileMenuItems.forEach((i) => {
i.addEventListener("click", () => {
mobileNav.classList.remove("active");
});
});
// Desktop Navigation
const options = {
threshold: 0.8,
};
const addActiveClass = (entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting && entry.intersectionRatio >= 0.2) {
// console.log(entry.target);
let currentActive = document.querySelector(".desktop-nav a.active");
if (currentActive) {
currentActive.classList.remove("active");
}
let newActive = document.querySelector(
`.desktop-nav a[href="#${entry.target.getAttribute("id")}"]`
);
newActive.classList.add("active");
}
});
};
const observer = new IntersectionObserver(addActiveClass, options);
const sections = document.querySelectorAll("section");
sections.forEach((section) => {
observer.observe(section);
});