<p>
<button class="btn">
Animate
</button>
</p>
<div class="container">
<div class="box">
</div>
</div>
/* Copyright 2018 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
/* CSS files add styling rules to your content */
body {
font-family: "Benton Sans", "Helvetica Neue", helvetica, arial, sans-serif;
margin: 2em;
}
.container {
border: 4px solid #777;
height: 70vh;
display: flex;
align-items: center;
justify-content: center;
}
.box {
background-color: #165baa;
color: #fff;
width: 100px;
height: 100px;
border-radius: .5em;
}
.animate {
animation: slide-in 0.7s both;
}
@keyframes slide-in {
0% {
transform: translateY(-1000px);
}
100% {
transform: translateY(0);
}
}
let btn = document.querySelector(".btn");
let item = document.querySelector(".box");
btn.addEventListener('click', (e) => {
item.classList.toggle('animate');
item.addEventListener('animationend', animationEndCallback);
});
let animationEndCallback = (e) => {
item.removeEventListener('animationend', animationEndCallback);
item.classList.remove('animate');
}