-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (36 loc) · 1.2 KB
/
Copy pathscript.js
File metadata and controls
44 lines (36 loc) · 1.2 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
// Mobile navigation toggle
document.addEventListener('DOMContentLoaded', function () {
const nav = document.querySelector('header nav');
const toggle = nav ? nav.querySelector('.nav-toggle') : null;
const navList = nav ? nav.querySelector('ul') : null;
if (!nav || !toggle || !navList) {
return;
}
function closeNav() {
nav.classList.remove('is-open');
toggle.setAttribute('aria-expanded', 'false');
}
function toggleNav() {
const isOpen = nav.classList.toggle('is-open');
toggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
}
toggle.addEventListener('click', toggleNav);
navList.querySelectorAll('a').forEach(function (link) {
link.addEventListener('click', closeNav);
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') {
closeNav();
}
});
document.addEventListener('click', function (e) {
if (nav.classList.contains('is-open') && !nav.contains(e.target)) {
closeNav();
}
});
window.addEventListener('resize', function () {
if (window.innerWidth > 900) {
closeNav();
}
});
});