forked from geomarts/mailchimp-popup-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
43 lines (37 loc) · 1.18 KB
/
main.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
const openEls = document.querySelectorAll("[data-open]");
const closeEls = document.querySelectorAll("[data-close]");
const isVisible = "is-visible";
const closeActions = () => {
document.querySelector(".modal.is-visible").classList.remove(isVisible);
};
for (const el of openEls) {
el.addEventListener("click", function () {
const modalId = this.dataset.open;
document.getElementById(modalId).classList.add(isVisible);
});
}
for (const el of closeEls) {
el.addEventListener("click", function () {
this.parentElement.parentElement.parentElement.classList.remove(
isVisible
);
});
}
document.addEventListener("click", (e) => {
if (e.target == document.querySelector(".modal.is-visible")) {
closeActions();
}
});
document.addEventListener("keyup", (e) => {
// if we press the ESC
if (e.key == "Escape" && document.querySelector(".modal.is-visible")) {
closeActions();
}
});
/*USE THIS CODE IF YOU WANT TO AUTOLOAD THE FORM INSTEAD OF TRIGGERING IT ON THE BUTTON CLICK*/
/*if ("false" !== sessionStorage.getItem("show-modal")) {
setTimeout(function () {
document.getElementById("modal1").classList.add(isVisible);
sessionStorage.setItem("show-modal", "false");
}, 300);
}*/