Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Web Animations API
  4. Keyframe Formats

Keyframe Formats

Element.animate(),KeyframeEffect(), andKeyframeEffect.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. Anarray of objects (keyframes) consisting of properties and values to iterate over. This is the canonical format returned by thegetKeyframes() method.

    js
    element.animate(  [    {      // from      opacity: 0,      color: "white",    },    {      // to      opacity: 1,      color: "black",    },  ],  2000,);

    Offsets for each keyframe can be specified by providing anoffset value.

    js
    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 aneasing value as illustrated below.

    js
    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. Anyeasing value specified on theoptions argument, however, applies across a single iteration of the animation — for the entire duration.

  2. Anobject containing key-value pairs consisting of the property to animate and anarray of values to iterate over.

    js
    element.animate(  {    opacity: [0, 1], // [ from, to ]    color: ["white", "black"], // [ 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.

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

    The special keysoffset,easing, andcomposite (described below) may be specified alongside the property values.

    js
    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 containsnull values, the keyframes without specified offsets will be evenly spaced as with the array format described above.

    If there are too feweasing orcomposite values, the corresponding list will be repeated as needed.

Implicit to/from keyframes

The browser can infer the start or end state of an animation by using the current state. By default, if a single keyframe is provided, it's treated as the end state, and the start state is inferred from the element's current computed style. However, you can specify theoffset to indicate where the provided keyframe should be placed in the animation timeline. For more information, seeElement.animate().

js
// Animate from the current state to translateX(300px)logo.animate({ transform: "translateX(300px)" }, 1000);// Animate from translateX(300px) to the current statelogo.animate({ transform: "translateX(300px)", offset: 0 }, 1000);// Animate from the current state to translateX(300px) and back to the current statelogo.animate({ transform: "translateX(300px)", offset: 0.5 }, 1000);

Attributes

Keyframes specify property-value pairs of theCSS properties to be animated. The property names are specified usingcamel case so for examplebackground-color becomesbackgroundColor andbackground-position-x becomesbackgroundPositionX. Shorthand values such asmargin are also permitted.

Two exceptional CSS properties are:

  • float, which must be written ascssFloat 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 ascssOffset 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 between0.0 and1.0 inclusive ornull. This is equivalent to specifying start and end states in percentages in CSS stylesheets using@keyframes. If this value isnull or missing, the keyframe will be evenly spaced between adjacent keyframes.

easing

Theeasing function used from this keyframe until the next keyframe in the series.

composite

TheKeyframeEffect.composite operation used to combine the values specified in this keyframe with the underlying value. This will beauto if the composite operation specified on the effect is being used.

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp