-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (37 loc) · 1.17 KB
/
Copy pathscript.js
File metadata and controls
41 lines (37 loc) · 1.17 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
// Mobile nav toggle
const navToggle = document.getElementById("navToggle");
const navMenu = document.getElementById("navMenu");
navToggle.addEventListener("click", () => {
navMenu.classList.toggle("open");
});
// Active link on scroll
const links = [...document.querySelectorAll(".nav__link")];
const sections = links.map((a) =>
document.querySelector(a.getAttribute("href"))
);
const onScroll = () => {
const pos = window.scrollY + 100;
sections.forEach((sec, i) => {
if (!sec) return;
const top = sec.offsetTop,
bottom = top + sec.offsetHeight;
const link = links[i];
if (pos >= top && pos < bottom) {
links.forEach((l) => l.classList.remove("active"));
link.classList.add("active");
}
});
};
document.addEventListener("scroll", onScroll);
onScroll();
// Back to top button
const toTop = document.getElementById("toTop");
window.addEventListener("scroll", () => {
if (window.scrollY > 500) toTop.classList.add("show");
else toTop.classList.remove("show");
});
toTop.addEventListener("click", () =>
window.scrollTo({ top: 0, behavior: "smooth" })
);
// Current year
document.getElementById("year").textContent = new Date().getFullYear();