<cs-animation>
StableHelpersSince 0.1

Animate elements declaratively with nearly 100 baked-in presets, or roll your own with custom keyframes. Powered by the Web Animations API.

To animate an element, wrap it in <cs-animation> and set the name attribute. The animation will not start until you add the play attribute. Refer to the properties table for a list of all animation options.

The animation will only be applied to the first child element found in <cs-animation>.

Respect users who prefer reduced motion.
<cs-animation> plays regardless of the user’s motion preferences. Gate decorative animations behind a prefers-reduced-motion media query so they don’t play for people who’ve asked to minimize motion.

Examples

Animations & Easings

This example demonstrates all of the baked-in animations and easings. Animations are based on those found in the popular Animate.css library.

Using Intersection Observer

Use an Intersection Observer to control the animation when an element enters or exits the viewport. For example, scroll the box below in and out of your screen. The animation stops when the box exits the viewport and restarts each time it enters the viewport.

Custom Keyframe Formats

Supply your own keyframe formats to build custom animations.

Playing Animations on Demand

Animations won’t play until you apply the play attribute. You can omit it initially, then apply it on demand such as after a user interaction. In this example, the button will animate once every time the button is clicked.

Click me

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/animation/animation.js';
import './cornerstone/components/animation/animation.js';
import CsAnimation from '@cruglobal/cornerstone-components/react/animation/index.js';

Slots

Learn more about using slots.

NameDescription
(default)The element to animate. Avoid slotting in more than one element, as subsequent ones will be ignored. To animate multiple elements, either wrap them in a single container or use multiple <cs-animation> elements.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
currentTimeGets and sets the current animation time.CSSNumberish
delaydelayThe number of milliseconds to delay the start of the animation.number0
directiondirectionDetermines the direction of playback as well as the behavior when reaching the end of an iteration. Learn morePlaybackDirection'normal'
durationdurationThe number of milliseconds each iteration of the animation takes to complete.number1000
easingeasingThe easing function to use for the animation. This can be a Cornerstone easing function or a custom easing function such as cubic-bezier(0, 1, .76, 1.14).string'linear'
endDelayend-delayThe number of milliseconds to delay after the active period of an animation sequence.number0
fillfillSets how the animation applies styles to its target before and after its execution.FillMode'auto'
iterationsiterationsThe number of iterations to run before the animation completes. Defaults to Infinity, which loops.numberInfinity
iterationStartiteration-startThe offset at which to start the animation, usually between 0 (start) and 1 (end).number0
keyframesThe keyframes to use for the animation. If this is set, name will be ignored.Keyframe[] | undefined
namenameThe name of the built-in animation to use. For custom animations, use the keyframes prop.string'none'
playplayPlays the animation. When omitted, the animation will be paused. This attribute will be automatically removed when the animation finishes or gets canceled.booleanfalseYes
playbackRateplayback-rateSets the animation's playback rate. The default is 1, which plays the animation at a normal speed. Setting this to 2, for example, will double the animation's speed. A negative value can be used to reverse the animation. This value can be changed without causing the animation to restart.number1

Methods

Learn more about methods.

NameDescriptionArguments
cancel()Clears all keyframe effects caused by this animation and aborts its playback.
finish()Sets the playback time to the end of the animation corresponding to the current playback direction.

Events

Learn more about events.

NameDescription
cs-cancelEmitted when the animation is canceled.
cs-finishEmitted when the animation finishes.
cs-startEmitted when the animation starts or restarts.

SSR

Learn more about Server-Side Rendering (SSR).

<cs-animation> renders during SSR without causing layout shift, but won't play its animation until the component hydrates on the client. Playback is driven by the Web Animations API, which is only available in the browser.