Home >Web Front-end >CSS Tutorial >Hacking CSS Animation State and Playback Time
This article delves into manipulating CSS animations, specifically addressing the challenges of restarting and slowing down animations without using JavaScript. The author recounts their experience building a CSS-only Wolfenstein 3D-inspired game, highlighting two particularly tricky animation problems: triggering a weapon firing animation on enemy clicks and implementing dramatic slow motion for a boss's final hit.
The core issue revolves around the browser's handling of animation states. Simply adding or modifying animation properties while an animation is already running doesn't always restart or slow it down as expected.
Problem 1: Replaying Animations
The author explores several approaches to replaying animations:
spin1
, spin2
) for each trigger works, but requires careful CSS rule ordering.animation-name
property also functions, but can lead to unexpected behavior with animation-fill-mode: forwards
.The key takeaway is that you can't directly restart a CSS animation; you must add a new one.
Problem 2: Slow Motion
Simply changing animation-duration
while an animation is running results in jarring jumps. The browser applies the new duration from the animation's start, not its current state.
Several approaches are explored:
animation-duration
Change (Ineffective): Changing animation-duration
mid-animation causes visual glitches.--angle1
, --angle2
) to track the animation's progress and seamlessly transition between animations with different durations. This, however, requires browser support for @property
.The article concludes by acknowledging the inspiration from other CSS-only projects and providing a comprehensive overview of the challenges and solutions involved in manipulating CSS animations. The author encourages readers to explore the provided examples and share their thoughts.
The above is the detailed content of Hacking CSS Animation State and Playback Time. For more information, please follow other related articles on the PHP Chinese website!