-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (47 loc) · 1.46 KB
/
index.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
class Navbar {
constructor(target, menu) {
// 1. Check parameters type and throw error if not an HTML element
if (!(target instanceof HTMLElement && menu instanceof HTMLElement)) {
throw new TypeError("The Target and Menu arguments must be a DOM element.");
}
this.isOpen = false;
this.btn = target;
this.menu = menu;
this.btn.addEventListener('click', () => {
this.open();
});
}
open() {
if (this.isopen) {
this.menu.classList.add('nav-hidden');
} else {
this.menu.classList.remove('nav-hidden');
}
this.isopen = !this.isopen;
}
}
class Dropdown {
constructor(elem) {
if (!(elem instanceof HTMLElement)) {
throw new TypeError("The element must be an HTML element.");
}
this.isOpen = false;
this.elem = elem;
this.elem.addEventListener('click', () => {
this.open();
});
}
open() {
if (!this.isOpen) {
this.elem.querySelector(".dropdown-list").classList.add("show-dropdown");
} else {
this.elem.querySelector(".dropdown-list").classList.remove("show-dropdown");
}
this.isOpen = !this.isOpen;
}
}
const dropdown = new Dropdown(document.querySelector(".dropdown"));
const navbar = new Navbar(
document.querySelector(".menu-btn"),
document.querySelector(".menu-list")
);