<button id="button-open">モーダルを開く</button>
<dialog id="my-modal">
<p>モーダルの中身です。</p>
<button id="button-close">モーダルを閉じる</button>
</dialog>
#my-modal:modal {
opacity: 0;
}
#my-modal:modal {
animation: fadeIn .5s forwards;
}
#my-modal:modal.is-hide {
animation: fadeOut .5s forwards;
}
#my-modal::backdrop {
opacity: 0;
background-color: rgba(0,0,0,.8);
}
#my-modal::backdrop {
animation: fadeIn .5s forwards;
}
#my-modal.is-hide::backdrop {
animation: fadeOut .5s forwards;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
const modal = document.querySelector('#my-modal')
const buttonOpen = document.querySelector('#button-open')
const buttonClose = document.querySelector('#button-close')
buttonOpen.addEventListener('click', () => {
modal.showModal()
})
buttonClose.addEventListener('click', () => {
modal.classList.add('is-hide')
modal.addEventListener('webkitAnimationEnd', function(){
modal.classList.remove('is-hide')
modal.close()
modal.removeEventListener('webkitAnimationEnd', arguments.callee, false);
}, false);
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.