Skip to content

Web Animations API / lesson 3 of 7

Reverse

Learn how to create a smooth loading spinner with reverse playback functionality using waapi.

Play

Learn how to create a smooth loading spinner with reverse playback functionality using waapi. Build an engaging interface element with a rotating border animation that responds to user interaction.

index.html
<html>
<head>
<style>
body {
background-color: #222;
}
.container {
display: flex;
justify-content: center;
}
.circle {
width: 200px;
height: 200px;
border-radius: 100%;
border: 4px solid #fff;
border-top: 4px solid blue;
}
button {
font-size: 48px;
margin-bottom: 25px;
}
</style>
</head>
<body>
<button class="pause">Pause</button>
<button class="play">Play</button>
<button class="reverse">Reverse</button>
<div class="container">
<div class="circle"></div>
</div>
<script>
const myAnimation = document.querySelector(".circle").animate(
[
{
transform: "rotate(0)"
},
{
transform: "rotate(359deg)"
}
],
{
duration: 1000,
iterations: Infinity
}
);
document.querySelector(".pause")
.addEventListener("click", () => myAnimation.pause());
document.querySelector(".play")
.addEventListener("click", () => myAnimation.play());
document.querySelector(".reverse")
.addEventListener("click", () => myAnimation.reverse());
</script>
</body>
</html>

Share this post on:

Previous
Pause & Play
Next
playbackRate