-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.js
84 lines (76 loc) · 2.69 KB
/
menu.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
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
customElements.define(
"navigation-menu",
class extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const nav = document.createElement("nav");
nav.classList.add("navigation");
nav.innerHTML = `
<a class="navigation__item" href="/">
<div class="navigation__item--logo">AMAGI</div>
</a>
<a class="navigation__hamburger">
<i class="fas fa-bars"></i>
</a>
<ul class="navigation__list mobile-hidden">
<li class="navigation__item">
<a class="navigation__item--link" href="/">Home</a>
</li>
<li class="navigation__item">
<a class="navigation__item--link" href="/services">Services</a>
</li>
<li class="navigation__item">
<a class="navigation__item--link" href="/industries">Industries</a>
</li>
<li class="navigation__item">
<a class="navigation__item--link" href="/careers">Careers</a>
</li>
<li class="navigation__item">
<a class="navigation__item--link" href="/contact">Contact Us</a>
</li>
</ul>
`;
this.append(nav);
}
}
);
window.addEventListener("load", () => {
/* HAMBURGER MENU */
// drop down menu on hamburger icon click
const hamburgerEl = document.getElementsByClassName(
"navigation__hamburger"
)[0];
const navListEl = document.getElementsByClassName("navigation__list")[0];
const navLinks = document.getElementsByClassName("navigation__item--link");
const toggleHamburger = () => {
const isVisible = navListEl.style.display === "flex";
if (isVisible) {
navListEl.style.display = "none";
document.body.style.height = "100%";
document.body.style.overflow = "auto";
} else {
navListEl.style.display = "flex";
document.body.style.height = "100vh";
document.body.style.overflow = "hidden";
}
};
// toggle hamburger visibility on click
hamburgerEl.addEventListener("click", () => {
toggleHamburger();
});
// hide hamburger when a navlink is pressed
for (const item of navLinks) {
item.addEventListener("click", () => {
const isVisible = navListEl.style.display === "flex";
if (isVisible) toggleHamburger();
});
}
// reset nav list and body styles on resize
window.addEventListener("resize", () => {
navListEl.style.display = "";
document.body.style.height = "";
document.body.style.overflow = "";
});
});