Skip to content

Commit

Permalink
modals add
Browse files Browse the repository at this point in the history
  • Loading branch information
GromovBoris committed Jan 16, 2024
1 parent e35fe00 commit 0c97df0
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion js/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
window.addEventListener("DOMContentLoaded", () => {
// TABS

// get listeners
const tabs = document.querySelectorAll(".tabheader__item");
const tabsContent = document.querySelectorAll(".tabcontent");
const tabsParent = document.querySelector(".tabheader__items");
Expand Down Expand Up @@ -104,4 +103,57 @@ window.addEventListener("DOMContentLoaded", () => {
}
}
setClock(".timer", deadline);

// MODAL

const modalTriggers = document.querySelectorAll("[data-modal]");
const modal = document.querySelector(".modal");
const modalCloseBtn = document.querySelector("[data-close]");

function openModal() {
modal.classList.add("show");
modal.classList.remove("hide");
// modal.classList.toggle("show");
document.body.style.overflow = "hidden";
clearInterval(modalTimerId);
}

function closeModal() {
modal.classList.add("hide");
modal.classList.remove("show");
// modal.classList.toggle("show");
document.body.style.overflow = "";
}

modalTriggers.forEach((btn) => {
btn.addEventListener("click", openModal);
});

modalCloseBtn.addEventListener("click", closeModal);

modal.addEventListener("click", (e) => {
if (e.target === modal) {
closeModal();
}
});

document.addEventListener("keydown", (e) => {
if (e.code === "Escape" && modal.classList.contains("show")) {
closeModal();
}
});

const modalTimerId = setTimeout(openModal, 5000);

function showModalByScroll() {
if (
window.pageYOffset + document.documentElement.clientHeight >=
document.documentElement.scrollHeight - 1
) {
openModal();
window.removeEventListener("scroll", showModalByScroll);
}
}

window.addEventListener("scroll", showModalByScroll);
});

0 comments on commit 0c97df0

Please sign in to comment.