Created
July 13, 2018 07:33
-
-
Save tborychowski/c7106dd5e99088ed1e9a1121f769258b to your computer and use it in GitHub Desktop.
html dialog with animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset=UTF-8> | |
<title>Dialog demo</title> | |
<style> | |
.dialog { | |
border: none; | |
border-radius: 3px; | |
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3); | |
} | |
.dialog[open] { animation: show-dialog .3s ease; } | |
.dialog.hide { animation: hide-dialog .3s ease; } | |
@keyframes show-dialog { | |
from { transform: scale(0.7); opacity: 0; } | |
to { transform: scale(1); opacity: 1; } | |
} | |
@keyframes hide-dialog{ | |
from { transform: scale(1); opacity: 1; } | |
to { transform: scale(0.7); opacity: 0; } | |
} | |
</style> | |
</head> | |
<body> | |
<dialog class="dialog"> | |
<p>This is a dialog!</p> | |
<button id="close">Close Dialog!</button> | |
</dialog> | |
<button id="show">Open Dialog!</button> | |
<script> | |
const dialog = document.querySelector('dialog'); | |
function open () { | |
dialog.showModal(); | |
} | |
function close () { | |
dialog.classList.add('hide'); | |
dialog.addEventListener('animationend', animEnd); | |
} | |
function animEnd () { | |
dialog.close(); | |
dialog.classList.remove('hide'); | |
dialog.removeEventListener('animationend', animEnd, false); | |
} | |
document.querySelector('#show').addEventListener('click', open); | |
document.querySelector('#close').addEventListener('click', close); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment