Sync animations perfectly without hardcoding durations. Master computed timing to keep your keyframe animations flexible and error-free.
<html><head> <style> body { background-color: #222; } .container { position: relative; width: 500px; height: 500px; border: 1px solid #fff; margin: 0 auto; } .piece { position: absolute; background-color: white; border-radius: 100%; width: 15px; height: 15px; } </style></head><body> <div class="container"> <div class="piece"></div> </div>
<script> const pieceKeyframes = [ { left: "50%", top: 0 }, { left: "calc(100% - 12px)", top: "50%" }, { left: "50%", top: "calc(100% - 12px)" }, { left: 0, top: "50%" }, { left: "50%", top: 0 } ];
const pieceAnimate = document.querySelector(".piece").animate( pieceKeyframes, { duration: 3500, iterations: Infinity } );
const containerAnimate = document.querySelector(".container").animate( [ { borderColor: "orange" }, { borderColor: "white" } ], { duration: pieceAnimate.effect.getComputedTiming().duration / 4, iterations: Infinity } ); </script></body></html>