-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal.js
More file actions
53 lines (41 loc) · 1.5 KB
/
Copy pathmodal.js
File metadata and controls
53 lines (41 loc) · 1.5 KB
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
function closeModal(modalSelector) {
const modal = document.querySelector(modalSelector);
modal.classList.add('hide');
modal.classList.remove('show');
document.body.style.overflow = '';
}
function openModal(modalSelector, modalTimerId) {
const modal = document.querySelector(modalSelector);
modal.classList.add('show');
modal.classList.remove('hide');
document.body.style.overflow = 'hidden';
if(modalTimerId){
clearInterval(modalTimerId);
}
}
function modal(triggerSelector, modalSelector, modalTimerId){
const modalTrigger = document.querySelectorAll(triggerSelector),
modal = document.querySelector(modalSelector);
modalTrigger.forEach(btn => {
btn.addEventListener('click', () => openModal(modalSelector, modalTimerId));
});
modal.addEventListener('click', (e) => {
if (e.target === modal || e.target.getAttribute('data-close') == '') {
closeModal(modalSelector);
}
});
document.addEventListener('keydown', (e) => {
if (e.code === "Escape" && modal.classList.contains('show')) {
closeModal(modalSelector);
}
});
// Изменил значение, чтобы не отвлекало
function showModalByScroll() {
if (window.pageYOffset + document.documentElement.clientHeight >= document.documentElement.scrollHeight) {
openModal(modalSelector, modalTimerId);
window.removeEventListener('scroll', showModalByScroll);
}
}
window.addEventListener('scroll', showModalByScroll);
}
export default modal;