KeyframeEffect.setKeyframes() - Web APIs 编辑

Experimental

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

The setKeyframes() method of the KeyframeEffect interface replaces the keyframes that make up the affected KeyframeEffect with a new set of keyframes.

Syntax

existingKeyframeEffect.setKeyframes(keyframes);

BCD tables only load in the browser

keyframes
A keyframe object or null. If set to null, the keyframes are replaced with a sequence of empty keyframes.

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.

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

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.

SpecificationStatusComment
Web Animations
The definition of 'Keyframe object formats' in that specification.
Working DraftInitial definition

Return value

Void.

Exceptions

ExceptionExplanation
TypeErrorOne or more of the frames were not of the correct type of object, the keyframes were not loosely sorted by offset, or a keyframe existed with an offset of less than 0 or more than 1.

Note: If the keyframes cannot be processed or are malformed, the KeyframeEffect's keyframes are not modified.

Examples

// passing an array of keyframe objects
existingKeyframeEffect.setKeyframes(
[
  { color: 'blue' },
    { color: 'green', left: '10px' }
  ]
);

// passing an object with arrays for values
existingKeyframeEffect.setKeyframes(
  {
    color: ['blue', 'green'],
    left: [ '0', '10px']
  }
);

// passing a single-member object
existingKeyframeEffect.setKeyframes(
  {
    color: 'blue'
  }
);

Specifications

SpecificationStatusComment
Web Animations
The definition of 'KeyframeEffect.setKeyframes()' in that specification.
Working DraftEditor's draft.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:128 次

字数:10629

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文