-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
37 lines (30 loc) · 995 Bytes
/
app.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
const navItems = document.querySelectorAll(".navigation ul li");
const sections = document.querySelectorAll("section");
// Variante A: per Klick wird die active class gesetzt
/* function activeLink() {
navItems.forEach((item) => {
item.classList.remove("active");
});
this.classList.add("active");
}
navItems.forEach((item) => {
item.addEventListener("click", activeLink);
}); */
// Varinate B: anhand der Scroll-Position wird die active class gesetzt
window.addEventListener("scroll", () => {
let current = "";
sections.forEach((section) => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
/* scrollY = aktuelle Scroll-Position des window-Objektes */
if (scrollY >= sectionTop - sectionHeight / 3) {
current = section.getAttribute("id");
}
});
navItems.forEach((item) => {
item.classList.remove("active");
if (item.classList.contains(current)) {
item.classList.add("active");
}
});
});