<div class="box"></div>
<button class="runButton">Run the animation</button>Web animation API tips and tricks / notebook
Web animation API tips and tricks
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
.box {
width: 100px;
height: 100px;
border: 1px solid black;
margin-bottom: 1rem;
}const box = document.querySelector(".box");
const button = document.querySelector(".runButton");
/*
equivalent to the following CSS @keyframes
@keyframes color-change {
0% {
background-color: grey;
}
100% {
background-color: lime;
}
}
*/
const colorChangeFrames = { backgroundColor: ["grey", "lime"] };
function playAnimation() {
box.animate(colorChangeFrames, 4000);
}button.addEventListener("click", playAnimation);