-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (29 loc) · 1.14 KB
/
script.js
File metadata and controls
33 lines (29 loc) · 1.14 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
document.addEventListener('DOMContentLoaded', () => {
const menuBtn = document.querySelector('.mobile-menu-btn');
const navLinks = document.querySelector('.nav-links');
if (menuBtn && navLinks) {
menuBtn.addEventListener('click', (e) => {
e.stopPropagation();
const isActive = navLinks.classList.toggle('active');
document.body.style.overflow = isActive ? 'hidden' : '';
// Update ARIA attribute
menuBtn.setAttribute('aria-expanded', isActive);
});
// Close menu when clicking a link
navLinks.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
navLinks.classList.remove('active');
document.body.style.overflow = '';
menuBtn.setAttribute('aria-expanded', 'false');
});
});
// Close menu when clicking outside
document.addEventListener('click', (e) => {
if (navLinks.classList.contains('active') && !navLinks.contains(e.target) && !menuBtn.contains(e.target)) {
navLinks.classList.remove('active');
document.body.style.overflow = '';
menuBtn.setAttribute('aria-expanded', 'false');
}
});
}
});