Skip to content

Instantly share code, notes, and snippets.

@dilverdev
Created April 27, 2020 04:44
Show Gist options
  • Save dilverdev/b4c49833c4ea9799b1b364543412c3b0 to your computer and use it in GitHub Desktop.
Save dilverdev/b4c49833c4ea9799b1b364543412c3b0 to your computer and use it in GitHub Desktop.
Multiple Modal Vanilla
<div class="modal" id="modal-1">
<div class="modal-box">
<div class="modal-close">
<div class="icon-close"></div>
</div>
<div>
<!-- content -->
</div>
</div>
</div>
const OpenModal = document.querySelectorAll('.open-modal')
if (OpenModal) {
const elemtModal = document.querySelectorAll('.modal')
const elemtModalBox = document.querySelectorAll('.modal-box')
const modalClose = document.querySelectorAll('.modal-close')
let modals = []
function seachModalActive (idDelete) {
for (let i = 0; i < modals.length; i++) {
if (modals[i] === idDelete) {
modals.splice(i, 1);
}
}
if (modals.length === 0) {
document.body.style.overflow = ''
}
}
// Open Modals
for (let i = 0; i < OpenModal.length; i++) {
OpenModal[i].addEventListener('click', (e) => {
e.preventDefault()
const idModalActive = e.target.dataset['idModal']
document.getElementById(idModalActive).classList.add('active')
document.body.style.overflow = 'hidden'
modals.push(idModalActive)
})
}
// Close Modals
for (let i = 0; i < elemtModal.length; i++) {
elemtModalBox[i].addEventListener('click', (e) => {
e.stopPropagation()
})
elemtModal[i].addEventListener('click', () => {
elemtModal[i].classList.remove('active')
const idModal = elemtModal[i].getAttribute('id')
seachModalActive(idModal)
})
modalClose[i].addEventListener('click', (e) => {
const fatherModal = e.target.parentElement.parentElement.parentElement
const idModalClose = e.target.parentElement.parentElement.parentElement.getAttribute('id')
fatherModal.classList.remove('active')
seachModalActive(idModalClose)
})
}
}
.modal {
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100%;
background-color: transparentize(#000, 0.2);
display: flex;
align-items: center;
justify-content: center;
padding: 20px 15px;
z-index: 1000;
overflow: auto;
margin: 0 auto;
opacity: 0;
visibility: hidden;
transition: all 0.3s;
@media (min-width: 768px) {
background-color: transparentize(#000, 0.4);
}
&.active {
opacity: 1;
visibility: visible;
.modal {
&-box {
transform: scale(1);
}
}
}
&-box {
position: relative;
border-radius: 4px;
overflow: hidden;
margin: auto;
transition: all 0.3s;
transform: scale(0.7);
}
&-close {
position: absolute;
top: 0;
right: 0;
z-index: 10;
.icon-close {
transform: scale(0.8);
&:before, &:after {
background-color: #ffffff;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment