--- title: Element.animate() slug: Web/API/Element/animate tags: - js动画 - 动画 - 动画接口 translation_of: Web/API/Element/animate ---
{{domxref("Element")}} 接口的animate()
方法是一个创建新{{domxref("Animation")}}的便捷方法,将它应用于元素,然后运行动画。它将返回一个新建的 {{domxref("Animation")}} 对象实例
一个元素上可以应用多个动画效果。你可以通过调用此函数获得这些动画效果的一个列表: {{domxref("Element.getAnimations()")}}.
var animation = element.animate(keyframes, options);
keyframes 关键帧
一个对象,代表关键帧的一个集合
options 可选项
id {{optional_inline}}
animate()里可作为唯一标识的属性
: 一个用来引用动画的字符串( DOMString
)下面的可选项目前并非在所有地方都可用,但未来将会被加进来
composite {{optional_inline}} 合成
replace
.
add
表示追加影响,每一次连续的迭代建立在前一个的基础上。 比如transform
, translateX(-200px)
将不会覆盖 rotate(20deg)
的值,最终结果是 translateX(-200px) rotate(20deg)
accumulate
效果类似但是更智能一些: blur(2)
和blur(5)
的结果为blur(7)
, 而不是 blur(2) blur(5)
replace
新的值将会覆盖掉旧的iterationComposite {{optional_inline}} 迭代合成
accumulate
或者 replace
(同上)。默认值为 replace
.spacing {{optional_inline}}
distribute
.
distribute
分配的关键帧位置,使连续关键帧之间的距离相等。也就是说,没有任何偏移时,将会使关键帧均匀分到整个运行时间内paced
分配的关键帧位置,使连续关键帧之间的距离让某个步增的属性值的增长速度相同,也就是说,属性值差异越大,关键帧之间的距离越远
返回 {{domxref("Animation")}}.
在示例 Down the Rabbit Hole (with the Web Animation API) 中, 我们用 animate()
来快速创建并运行使 #tunnel
元素无限循环缓慢升起的动画。注意关键帧的对象数组和时间可选项
document.getElementById("tunnel").animate([ // keyframes { transform: 'translateY(0px)' }, { transform: 'translateY(-300px)' } ], { // timing options duration: 1000, iterations: Infinity });
规范 | 状态 | 注释 |
---|---|---|
{{SpecName('Web Animations', '#the-animatable-interface', 'animate()' )}} | {{Spec2('Web Animations')}} | Initial definition |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | {{CompatChrome("36")}} | {{CompatNo}} | {{ CompatGeckoDesktop("48.0")}} | {{CompatNo}} | 23 | {{CompatUnknown}} |
id option |
{{CompatChrome(50.0)}} | {{CompatNo}} | {{ CompatGeckoDesktop("48.0")}} | {{CompatNo}} | 37 | {{CompatUnknown}} |
composite , iterationComposite , and spacing options |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatChrome(39.0)}} | {{CompatChrome(39.0)}} | {{ CompatGeckoMobile("48.0")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
id option |
{{CompatNo}} | {{CompatChrome(50.0)}} | {{CompatChrome(50.0)}} | {{ CompatGeckoMobile("48.0")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
composite , iterationComposite , and spacing options |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |