<dialog id="modal">
<p>Hi! This is a modal.</p>
<button id="hide-modal">OK</button>
</dialog>
<button id="show-modal">
Show Modal
</button>
<script>
#modal {
display: block;
opacity: 0;
pointer-events: none;
border: none;
top: 50px;
}
#modal[open] {
opacity: 1;
pointer-events: auto;
animation: slidein 0.5s forwards;
}
#modal::backdrop {
background-color: rgba(0, 0, 0, 0.6);
animation: fadein 0.5s forwards;
}
#modal.close[open] {
animation: slideout 0.5s forwards;
}
#modal.close::backdrop {
animation: fadeout 0.5s forwards;
}
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeout {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
@keyframes slidein {
from {
transform: translateY(-100%);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
@keyframes slideout {
from {
transform: translateY(0);
opacity: 1;
}
to {
transform: translateY(-100%);
opacity: 0;
}
}
const modal = document.getElementById("modal");
const showModalBtn = document.getElementById("show-modal");
const hideModalBtn = document.getElementById("hide-modal");
function closeModal() {
modal.close();
modal.classList.remove('close');
modal.removeEventListener('animationend', closeModal);
}
showModalBtn.addEventListener('click', () => {
modal.showModal();
});
hideModalBtn.addEventListener('click', () => {
modal.addEventListener('animationend', closeModal);
modal.classList.add('close');
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.