-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (101 loc) · 3.07 KB
/
app.js
File metadata and controls
120 lines (101 loc) · 3.07 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Enhanced Navbar Functionality
const navToggle = document.querySelector(".nav-toggle");
const links = document.querySelector(".links");
const mobileOverlay = document.querySelector(".mobile-overlay");
const navbar = document.querySelector(".navbar");
const navLinks = document.querySelectorAll(".nav-link");
const body = document.body;
// Toggle mobile menu
function toggleMenu() {
const isOpen = links.classList.contains("show-links");
links.classList.toggle("show-links");
mobileOverlay.classList.toggle("show");
navToggle.classList.toggle("active");
// Update aria attributes for accessibility
navToggle.setAttribute("aria-expanded", !isOpen);
// Prevent body scroll when menu is open (mobile)
if (!isOpen) {
body.style.overflow = "hidden";
} else {
body.style.overflow = "";
}
}
// Close mobile menu
function closeMenu() {
links.classList.remove("show-links");
mobileOverlay.classList.remove("show");
navToggle.classList.remove("active");
navToggle.setAttribute("aria-expanded", "false");
body.style.overflow = "";
}
// Event Listeners
navToggle.addEventListener("click", toggleMenu);
// Close menu when clicking overlay
mobileOverlay.addEventListener("click", closeMenu);
// Close menu when clicking a link (mobile)
navLinks.forEach((link) => {
link.addEventListener("click", () => {
if (window.innerWidth < 800) {
closeMenu();
}
});
});
// Handle window resize
let resizeTimer;
window.addEventListener("resize", () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
if (window.innerWidth >= 800) {
closeMenu();
}
}, 250);
});
// Add scroll effect to navbar
let lastScroll = 0;
window.addEventListener("scroll", () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 50) {
navbar.classList.add("scrolled");
} else {
navbar.classList.remove("scrolled");
}
lastScroll = currentScroll;
});
// Set active link based on current page
function setActiveLink() {
const currentPath = window.location.pathname;
const currentPage = currentPath.split("/").pop() || "index.html";
navLinks.forEach((link) => {
link.classList.remove("active");
const linkPath = link.getAttribute("href");
if (linkPath === currentPage ||
(currentPage === "" && linkPath === "index.html")) {
link.classList.add("active");
}
});
}
// Initialize active link on load
setActiveLink();
// Close menu on Escape key press
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && links.classList.contains("show-links")) {
closeMenu();
navToggle.focus(); // Return focus to toggle button
}
});
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
const href = this.getAttribute("href");
if (href !== "#" && href.startsWith("#")) {
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
}
});
});