这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

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

句法

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() 方法。

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

    Offsets for each keyframe can be specified by providing an offset 值。

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

    注意 : 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 选项 argument, however, applies across a single iteration of the animation — for the entire duration.

  2. An 对象 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 ,和 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 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.

属性

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

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

composite

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.

规范

规范 状态 注释
Web 动画
The definition of 'Keyframe object formats' in that specification.
工作草案 初始定义

浏览器兼容性

Element.animate

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
animate Chrome 36 Edge 79 Firefox 48 IE 不支持 No Opera 23 Safari Yes Disabled
Yes Disabled
Disabled This feature is behind the Web 动画 preference and the CSS Animations via Web Animations preference.
WebView Android 37 Chrome Android 36 Firefox Android 48 Opera Android 24 Safari iOS 13.4 Samsung Internet Android 3.0
composite option Chrome 79 Disabled
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 79 Disabled
79 Disabled
Disabled From version 79: this feature is behind the Experimental Web Platform Features preference.
Firefox 63 Disabled
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.
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 Opera 66 Disabled
66 Disabled
Disabled From version 66: this feature is behind the Experimental Web Platform Features preference.
Safari 不支持 No WebView Android 不支持 No Chrome Android 79 Disabled
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 63 Disabled
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.
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 Safari iOS 不支持 No Samsung Internet Android 不支持 No
id option Chrome 50 Edge 79 Firefox 48 IE 不支持 No Opera 37 Safari 不支持 No WebView Android 50 Chrome Android 50 Firefox Android 48 Opera Android 37 Safari iOS 不支持 No Samsung Internet Android 5.0
Implicit to/from keyframes are supported Chrome 不支持 No 注意事项
No 注意事项
Currently Chrome Canary only
Edge 不支持 No Firefox 75 IE 不支持 No Opera 不支持 No Safari 部分支持 13.1 注意事项
部分支持 13.1 注意事项
Implementation seems somewhat buggy. More information will follow when available.
WebView Android 不支持 No Chrome Android 不支持 No 注意事项
No 注意事项
Currently Chrome Canary only
Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 部分支持 13.4 注意事项
部分支持 13.4 注意事项
Implementation seems somewhat buggy. More information will follow when available.
Samsung Internet Android 不支持 No
iterationComposite option Chrome 不支持 No Edge 不支持 No Firefox 63 Disabled
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.
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 Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 63 Disabled
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.
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 Safari iOS 不支持 No Samsung Internet Android 不支持 No
pseudoElement option Chrome 部分支持 81 注意事项 Disabled
部分支持 81 注意事项 Disabled
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 部分支持 81 注意事项 Disabled
部分支持 81 注意事项 Disabled
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 75 IE 不支持 No Opera 部分支持 68 注意事项 Disabled
部分支持 68 注意事项 Disabled
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 WebView Android 不支持 No Chrome Android 部分支持 81 注意事项 Disabled
部分支持 81 注意事项 Disabled
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 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

部分支持

部分支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. Web_Animations_API

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1