-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla.js
56 lines (41 loc) · 1.28 KB
/
vanilla.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
//Hamburger nav:
const nav = document.querySelector("nav");
const burger = document.querySelector(".burger");
burger.addEventListener("click", function() {
nav.classList.toggle("show-nav");
});
//Modal for sign-up - I saw lots of tutorials for vanilla JS modals but I wanted to try using a class/constructor.
// Source: engineertodeveloper.com/create-a-modal-using-vanilla-javascript/
//Modal Class
class Modal {
constructor(modal, target) {
this.isOpen = false;
this.modal = modal;
this.target = target;
this.closeModal = modal.querySelectorAll("[data-close]");
//Event listener to check for modal trigger (sign-up button):
this.target.addEventListener("click", (event) => {
if (this.isOpen) {
return this.close();
}
return this.open();
});
this.closeModal.forEach(item => {
item.addEventListener("click", (event) => {
this.close();
});
});
}
//open and close toggle modal methods:
open() {
this.modal.classList.add("show-modal");
}
close() {
this.modal.classList.remove("show-modal");
}
}
// Modal instantiation:
const modal = new Modal(
document.querySelector(".modal"),
document.querySelector('[data-toggle="modal"]')
);