Web/API/Web Animations API/Keyframe Formats

From Get docs

This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.


Element.animate(), KeyframeEffect.KeyframeEffect(), and KeyframeEffect.setKeyframes() all accept objects formatted to represent a set of keyframes. There are several options to this format, which are explained below.

Syntax

There are two different ways to format keyframes:

  1. An array of objects (keyframes) consisting of properties and values to iterate over. This is the canonical format returned by the getKeyframes() method.

    element.animate([ 
      { // from
        opacity: 0,
        color: "#fff"
      }, 
      { // to
        opacity: 1,
     ​   color: "#000"
      }
    ], 2000);

    Offsets for each keyframe can be specified by providing an offset value.

    element.animate([ { opacity: 1 },
                      { opacity: 0.1, offset: 0.7 },
                      { opacity: 0 } ],
                    2000);

    Note: offset values, if provided, must be between 0.0 and 1.0 (inclusive) and arranged in ascending order.

    It is not necessary to specify an offset for every keyframe. Keyframes without a specified offset will be evenly spaced between adjacent keyframes.

    The easing to apply between keyframes can be specified by providing an easing value as illustrated below.

    element.animate([ { opacity: 1, easing: 'ease-out' },
                      { opacity: 0.1, easing: 'ease-in' },
                      { opacity: 0 } ],
                    2000);

    In this example, the specified easing only applies from the keyframe where it is specified until the next keyframe. Any easing value specified on the options argument, however, applies across a single iteration of the animation — for the entire duration.

  2. An object containing key-value pairs consisting of the property to animate and an array of values to iterate over.

    element.animate({
      opacity: [ 0, 1 ],          // [ from, to ]
      color:   [ "#fff", "#000" ] // [ from, to ]
    }, 2000);

    Using this format, the number of elements in each array does not need to be equal. The provided values will be spaced out independently.

    element.animate({
      opacity: [ 0, 1 ], // offset: 0, 1
      backgroundColor: [ "red", "yellow", "green" ], // offset: 0, 0.5, 1
    }, 2000);

    The special keys offset, easing, and composite (described below) may be specified alongside the property values.

    element.animate({
      opacity: [ 0, 0.9, 1 ],
      offset: [ 0, 0.8 ], // Shorthand for [ 0, 0.8, 1 ]
      easing: [ 'ease-in', 'ease-out' ],
    }, 2000);

    After generating a suitable set of keyframes from the property value lists, each supplied offset is applied to the corresponding keyframe. If there are insufficient values, or if the list contains null values, the keyframes without specified offsets will be evenly spaced as with the array format described above.

    If there are too few easing or composite values, the corresponding list will be repeated as needed.

Implicit to/from keyframes

In newer browser versions, you are able to set a beginning or end state for an animation only (i.e. a single keyframe), and the browser will infer the other end of the animation if it is able to. For example, consider [[../../../../../../../mdn.github.io/dom-examples/web-animations-api/implicit-keyframes|this simple animation]] — the Keyframe object looks like so:

let rotate360 = [
  { transform: 'rotate(360deg)' }
];

We have only specified the end state of the animation, and the beginning state is implied.

Attributes

Keyframes may specify property-value pairs for any of the animatable css properties. The property names are specified using camel-case so for example background-color becomes backgroundColor and background-position-x becomes backgroundPositionX. Shorthand values such as margin are also permitted.

Two exceptional CSS properties are:

  • float, which must be written as cssFloat since "float" is a reserved word in JavaScript. It's just for reference here, this will have no effect on animation since "float" is not an animatable CSS property.
  • offset, which must be written as cssOffset since "offset" represents the keyframe offset as described below.

The following special attributes may also be specified:

offset
The offset of the keyframe specified as a number between 0.0 and 1.0 inclusive or null. This is equivalent to specifying start and end states in percentages in CSS stylesheets using @keyframes. If this value is null or missing, the keyframe will be evenly spaced between adjacent keyframes.
easing
The timing function used from this keyframe until the next keyframe in the series.
composite
The KeyframeEffect.composite operation used to combine the values specified in this keyframe with the underlying value. This will be auto if the composite operation specified on the effect is being used.


Specifications

Specification Status Comment
Web AnimationsThe definition of 'Keyframe object formats' in that specification. Working Draft Initial definition

Browser compatibility

Element.animate

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet

animate

Experimental'

Chrome

Full support 36

Edge

Full support 79

Firefox

Full support 48

IE

No support No

Opera

Full support 23

Safari Full support 13.1


Full support 13.1


Full support Yes

Disabled'

Disabled' This feature is behind the Web Animations preference and the CSS Animations via Web Animations preference.

WebView Android

Full support 37

Chrome Android

Full support 36

Firefox Android

Full support 48

Opera Android

Full support 24

Safari iOS

Full support 13.4

Samsung Internet Android

Full support 3.0

composite option

Experimental'

Chrome Full support 79

Disabled'

Full support 79

Disabled'

Disabled' From version 79: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.

Edge Full support 79

Disabled'

Full support 79

Disabled'

Disabled' From version 79: this feature is behind the Experimental Web Platform Features preference.

Firefox Full support 80


Full support 80


No support 63 — 79

Disabled'

Disabled' From version 63 until version 79 (exclusive): this feature is behind the dom.animations-api.compositing.enabled preference. To change preferences in Firefox, visit about:config. No support 53 — 63

Disabled'

Disabled' From version 53 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.

IE

No support No

Opera Full support 66

Disabled'

Full support 66

Disabled'

Disabled' From version 66: this feature is behind the Experimental Web Platform Features preference.

Safari

No support No

WebView Android

No support No

Chrome Android Full support 79

Disabled'

Full support 79

Disabled'

Disabled' From version 79: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.

Firefox Android Full support 63

Disabled'

Full support 63

Disabled'

Disabled' From version 63: this feature is behind the dom.animations-api.compositing.enabled preference. To change preferences in Firefox, visit about:config. No support 53 — 63

Disabled'

Disabled' From version 53 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

id option

Experimental'

Chrome

Full support 50

Edge

Full support 79

Firefox

Full support 48

IE

No support No

Opera

Full support 37

Safari

No support No

WebView Android

Full support 50

Chrome Android

Full support 50

Firefox Android

Full support 48

Opera Android

Full support 37

Safari iOS

No support No

Samsung Internet Android

Full support 5.0

Implicit to/from keyframes are supported

Experimental'

Chrome No support No

Notes'

No support No

Notes'

Notes' Currently Chrome Canary only

Edge

No support No

Firefox

Full support 75

IE

No support No

Opera

No support No

Safari Partial support 13.1

Notes'

Partial support 13.1

Notes'

Notes' Implementation seems somewhat buggy. More information will follow when available.

WebView Android

No support No

Chrome Android No support No

Notes'

No support No

Notes'

Notes' Currently Chrome Canary only

Firefox Android

No support No

Opera Android

No support No

Safari iOS Partial support 13.4

Notes'

Partial support 13.4

Notes'

Notes' Implementation seems somewhat buggy. More information will follow when available.

Samsung Internet Android

No support No

iterationComposite option

Experimental'

Chrome

No support No

Edge

No support No

Firefox Full support 80


Full support 80


No support 63 — 79

Disabled'

Disabled' From version 63 until version 79 (exclusive): this feature is behind the dom.animations-api.compositing.enabled preference. To change preferences in Firefox, visit about:config. No support 51 — 63

Disabled'

Disabled' From version 51 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.

IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android Full support 63

Disabled'

Full support 63

Disabled'

Disabled' From version 63: this feature is behind the dom.animations-api.compositing.enabled preference. To change preferences in Firefox, visit about:config. No support 51 — 63

Disabled'

Disabled' From version 51 until version 63 (exclusive): this feature is behind the dom.animations-api.core.enabled preference. To change preferences in Firefox, visit about:config.

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

pseudoElement option

Experimental'

Chrome Partial support 81

Notes' Disabled'

Partial support 81

Notes' Disabled'

Notes' A valid animation object is returned but the targeted pseudoelement is not visually animated. Disabled' From version 81: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.

Edge Partial support 81

Notes' Disabled'

Partial support 81

Notes' Disabled'

Notes' A valid animation object is returned but the targeted pseudoelement is not visually animated. Disabled' From version 81: this feature is behind the Experimental Web Platform Features preference.

Firefox

Full support 75

IE

No support No

Opera Partial support 68

Notes' Disabled'

Partial support 68

Notes' Disabled'

Notes' A valid animation object is returned but the targeted pseudoelement is not visually animated. Disabled' From version 68: this feature is behind the Experimental Web Platform Features preference.

Safari

No support No

WebView Android

No support No

Chrome Android Partial support 81

Notes' Disabled'

Partial support 81

Notes' Disabled'

Notes' A valid animation object is returned but the targeted pseudoelement is not visually animated. Disabled' From version 81: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.

Firefox Android

No support No

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.


See also