DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

Reverse a CSS Animation Mid-Flight: Use the Web Animations API

To reverse an animation during playback, call reverse() on its Web Animations API Animation object. Learn how to control API-created and CSS animations, and when timeline state matters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To reverse an animation while it is already moving, call reverse() on its Web Animations API Animation object. CSS animation-direction configures how animation iterations run; it is not a command to flip an animation that is currently in progress.

Why changing animation-direction does not reverse a running animation

CSS animation-direction sets the direction used for an animation’s iterations. Values such as reverse, alternate and alternate-reverse describe how those configured cycles run; changing the property is not the same as telling an existing animation to turn around at its current position. See the MDN reference for animation-direction.

As an Amazon Associate I earn from qualifying purchases.

For declarative effects with a predetermined iteration pattern, CSS remains appropriate. When the direction must change in response to a user action or application state while the effect is underway, use playback control on the animation object instead.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reverse an animation created with the Web Animations API

The Web Animations API exposes playback controls—including play, pause, reverse, finish and cancel—through an Animation object. If you created an effect with element.animate(), keep the returned object and call reverse() when the direction should change:

const animation = element.animate(
  [
    { transform: "translateX(0)" },
    { transform: "translateX(100px)" }
  ],
  { duration: 500, fill: "both" }
);

// Changes direction from the animation's current position.
animation.reverse();

reverse() inverts playback direction and continues playback. If the animation is paused, calling it resumes playback in reverse; if it has not yet played, it plays backward. It does not restart the effect from its original endpoint. See MDN’s Animation.reverse() reference.

Reverse a CSS animation already applied to an element

A CSS animation can have an associated Web Animations API Animation object. Retrieve animations associated with the target element using getAnimations(), then choose the specific one to reverse:

const animations = element.getAnimations();
const animation = animations.find(candidate => /* identify the intended animation */);

if (animation) {
  animation.reverse();
}

The example deliberately leaves identification to your application: an element can have more than one associated animation, and reversing every returned object may affect unrelated effects. For broader discovery, MDN’s guide to finding animations with Document.getAnimations() explains the document-level method. Inspect the returned objects and target the intended animation rather than applying reversal indiscriminately.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What happens to playback time when direction reverses?

The Web Animations specification defines reverse playback as a change to the animation’s playback direction. With a negative playback rate, animation time decreases as timeline time advances; the specification describes a playback rate of -1 as decreasing current time at the same rate that the timeline’s time values increase. Read the W3C Web Animations specification.

That is why reversing an active animation can continue from its current position instead of jumping immediately to the opposite endpoint. The animation still depends on its timeline and play state, so the operation is not simply a CSS property swap.

Choose the right control for the job

Approach What it controls Use it when
CSS animation-direction Direction for configured animation iterations The effect’s cycle pattern is known in advance
Web Animations API reverse() Playback direction on a particular Animation object Code must change direction during playback
finish() Moves an animation to its endpoint in the current direction The effect should complete immediately, not turn around
cancel() Aborts playback and clears the effect The effect should stop, not play in reverse

The API is imperative: your code must obtain and manage the relevant animation object. CSS is declarative: its properties describe the effect and iteration behavior. The API’s additional control is useful for state changes during playback, but it makes selecting the right animation and accounting for its timeline part of the implementation.

Rank #4
Blank Flip Book Paper with Holes - 240 Sheets (480 Pages) Flipbook Animation Paper : Works with Flip Book Kit Light Pads: for Drawing, Sketching Supplies/Comic Book Kit - Drawing Paper Animation Kit
  • Complete animation paper set … A great animation starter kit! Our 240 sheet (480 pages) flipbook paper with holes is quality 4.5inch x 2.5inch 120 gsm flippable paper and binding screws!
  • Perfect starter kits ... No more making your own flipbooks with scraps and staples. Our kits come with 2 sizes of binding screws, allowing you to trace and make flipbooks of many different sizes!
  • Individual pages ... Creating your own movies and animation has never been easier. No more limits on your animations that sewn binding books give you - With individual pages YOU get to decide!
  • Tracing made easy ... With our beautiful thick individual pages it is much easier to use with a light source, such as flip book light pads (not included) to trace your animations
  • Easy drawing ... No more spiral binding or pesky sewn book spines getting in your way. Our sketch pad paper is individual and free, just like your stop motion animations
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle errors and unexpected behavior

If reverse() throws

Check whether the animation has an associated active timeline. The W3C specification’s reverse procedure throws InvalidStateError if there is no associated timeline or if that timeline is inactive. Consult the specification’s Web Animations procedures when diagnosing timeline-related behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If the wrong effect changes direction

Inspect the objects returned for the element and identify the intended one before calling reverse(). An element may have multiple animations, so treating the result as a single guaranteed effect can cause unintended changes.

If the animation jumps or does not appear to reverse

Confirm that you are calling reverse() on the object associated with the effect you want to control, and consider whether its current play state or timeline allows playback to continue. finish() and cancel() are not alternatives for changing direction: they respectively complete or abort the effect.

Browser availability

MDN labels Animation.reverse() widely available and reports support across browsers since March 2020. That is a broad compatibility summary, not a guarantee for every browser version or a substitute for checking the environments your application supports. See MDN’s compatibility information.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.