-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
54 lines (43 loc) · 1.56 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
44
45
46
47
48
49
50
51
52
53
54
window.addEventListener('DOMContentLoaded', () => {
// Pop-up script
let modalTrigger = document.querySelectorAll('.openModal'),
modal = document.querySelector('.modal-wrapper'),
modalCloseButton = document.querySelectorAll('.closeModal'),
scroll = calcScroll();
modalTrigger.forEach(item => {
item.addEventListener('click', () => {
modal.classList.add('show-modal');
// document.body.style.overflow = 'hidden';
// document.body.style.paddingRight = `${scroll}px`;
})});
function closeModal() {
modal.classList.remove('show-modal');
// document.body.style.overflow = '';
// document.body.style.paddingRight = '0px';
};
function calcScroll() {
let div = document.createElement('div');
div.style.width = '50px';
div.style.height = '50px';
div.style.overflowY = 'scroll';
div.style.visibility = 'hidden';
document.body.appendChild(div);
let scrollWidth = div.offsetWidth - div.clientWidth;
div.remove();
return scrollWidth;
};
modalCloseButton.forEach(item => {
item.addEventListener('click', closeModal);
});
modal.addEventListener('click', (e) => {
if (e.target === modal) {
closeModal();
}
});
document.addEventListener('keydown', (e) => {
if (e.code === "Escape" && modal.classList.contains('show-modal')) {
closeModal();
}
});
// END Pop-up script
});