Source Code: (back to article)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Animation Sequence</title>
</head>
<body>
<h1>Animation Sequence</h1>
<div
class="animated-element"
style="width: 100px; height: 100px; background-color: red"
></div>
<button style="margin-top: 15px" onclick="animateSequence()">
Start Animation
</button>
<p id="message"></p>
<script>
async function animateSequence() {
document.getElementById("message").textContent = ""; // Clear message
const element = document.querySelector(".animated-element");
const animation1 = element.animate(
{ opacity: [0, 1], transform: ["scale(0)", "scale(1)"] },
{ duration: 1000, easing: "ease-in-out" }
);

await animation1.finished;

const animation2 = element.animate(
{ opacity: [1, 0], transform: ["scale(1)", "scale(0)"] },
{ duration: 1000, easing: "ease-in-out" }
);

await animation2.finished;
document.getElementById("message").textContent = "Sequence complete!";
}
</script>