-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupNav.js
37 lines (37 loc) · 1.09 KB
/
setupNav.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
export function setupNav(onNavChange) {
onNavChange = onNavChange || function () {};
const nav = document.getElementsByTagName("nav")[0];
const main = document.getElementsByTagName("main")[0];
const overlay = document.getElementById("overlay");
const burgerMenu = document.getElementById("burger-menu");
function openNav() {
onNavChange();
nav.style.width = "250px";
main.style.marginLeft = "250px";
main.style.marginRight = "-250px";
overlay.style.display = "block";
}
function closeNav() {
onNavChange();
nav.style.width = "0";
main.style.marginLeft = "0";
main.style.marginRight = "0";
overlay.style.display = "none";
}
let navOpen = false;
[burgerMenu, main].forEach((item) =>
item.addEventListener("click", function ({ target }) {
const isBurgerMenuClick = burgerMenu.contains(target);
if (!navOpen && !isBurgerMenuClick) {
return;
}
burgerMenu.classList.toggle("is-active");
if (!navOpen) {
openNav();
} else {
closeNav(!isBurgerMenuClick);
}
navOpen = !navOpen;
})
);
}