元素
接口的
animate()
method is a shortcut method which creates a new
动画
, applies it to the element, then plays the animation. It returns the created
动画
对象实例。
Elements can have multiple animations applied to them. You can get a list of the animations that affect an element by calling
Element.getAnimations()
.
var animation = element.animate(keyframes, options);
keyframes
Either an array of keyframe objects, or a keyframe object whose property are arrays of values to iterate over. See 关键帧格式 了解更多细节。
选项
id
可选
animate()
:
DOMString
with which to reference the animation.
delay
可选
The number of milliseconds to delay the start of the animation. Defaults to 0.
direction
可选
normal
), backwards (
reverse
), switches direction after each iteration (
alternate
), or runs backwards and switches direction after each iteration (
alternate-reverse
). Defaults to
"normal"
.
duration
可选
The number of milliseconds each iteration of the animation takes to complete. Defaults to 0. Although this is technically optional, keep in mind that your animation will not run if this value is 0.
easing
可选
"linear"
,
"ease"
,
"ease-in"
,
"ease-out"
,和
"ease-in-out"
, or a custom
"cubic-bezier"
value like
"cubic-bezier(0.42, 0, 0.58, 1)"
. Defaults to
"linear"
.
endDelay
可选
The number of milliseconds to delay after the end of an animation. This is primarily of use when sequencing animations based on the end time of another animation. Defaults to 0.
fill
可选
"backwards"
), retained after the animation has completed playing (
"forwards"
),或
both
. Defaults to
"none"
.
iterationStart
可选
Describes at what point in the iteration the animation should start. 0.5 would indicate starting halfway through the first iteration for example, and with this value set, an animation with 2 iterations would end halfway through a third iteration. Defaults to 0.0.
iterations
可选
1
, and can also take a value of
Infinity
to make it repeat for as long as the element exists.
You can also include a composite operation or iteration composite operation in your options list:
composite
可选
replace
.
add
dictates an additive effect, where each successive iteration builds on the last. For instance with
transform
,
translateX(-200px)
would not override an earlier
rotate(20deg)
value but result in
translateX(-200px) rotate(20deg)
.
accumulate
is similar but a little smarter:
blur(2)
and
blur(5)
become
blur(7)
, not
blur(2) blur(5)
.
replace
overwrites the previous value with the new one.
iterationComposite
可选
accumulate
or
replace
(see above). Defaults to
replace
.
返回
动画
.
In the demo
Down the Rabbit Hole (with the Web Animation API)
, we use the convenient
animate()
method to immediately create and play an animation on the
#tunnel
element to make it flow upwards, infinitely. Notice the array of objects passed as keyframes and also the timing options block.
document.getElementById("tunnel").animate([
// keyframes
{ transform: 'translateY(0px)' },
{ transform: 'translateY(-300px)' }
], {
// timing options
duration: 1000,
iterations: Infinity
});
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.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web Animations Level 2
The definition of 'KeyframeAnimationOptions.iterationComposite' in that specification. |
草案 |
添加
iterationComposite
选项。
|
|
Web 动画
The definition of 'animate()' in that specification. |
工作草案 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
animate
|
Chrome 36 | Edge 79 | Firefox 48 | IE No | Opera 23 |
Safari
Yes
Disabled
|
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
|
Edge
79
Disabled
|
Firefox
63
Disabled
|
IE No |
Opera
66
Disabled
|
Safari No | WebView Android No |
Chrome Android
79
Disabled
|
Firefox Android
63
Disabled
|
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
|
Edge No | Firefox 75 | IE No | Opera No |
Safari
部分支持
13.1
|
WebView Android No |
Chrome Android
No
|
Firefox Android No | Opera Android No |
Safari iOS
部分支持
13.4
|
Samsung Internet Android No |
iterationComposite
option
|
Chrome No | Edge No |
Firefox
63
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
63
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
pseudoElement
option
|
Chrome
部分支持
81
Disabled
|
Edge
部分支持
81
Disabled
|
Firefox 75 | IE No |
Opera
部分支持
68
Disabled
|
Safari No | WebView Android No |
Chrome Android
部分支持
81
Disabled
|
Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
部分支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
元素
accessKey
属性
childElementCount
children
classList
className
clientHeight
clientLeft
clientTop
clientWidth
currentStyle
firstElementChild
id
innerHTML
lastElementChild
localName
名称
namespaceURI
nextElementSibling
onfullscreenchange
onfullscreenerror
openOrClosedShadowRoot
outerHTML
part
prefix
previousElementSibling
runtimeStyle
scrollHeight
scrollLeft
scrollLeftMax
scrollTop
scrollTopMax
scrollWidth
shadowRoot
slot
tabStop
tagName
after()
animate()
append()
attachShadow()
before()
closest()
computedStyleMap()
createShadowRoot()
getAnimations()
getAttribute()
getAttributeNames()
getAttributeNode()
getAttributeNodeNS()
getAttributeNS()
getBoundingClientRect()
getClientRects()
getElementsByClassName()
getElementsByTagName()
getElementsByTagNameNS()
hasAttribute()
hasAttributeNS()
hasAttributes()
hasPointerCapture()
insertAdjacentElement()
insertAdjacentHTML()
insertAdjacentText()
matches()
msZoomTo()
prepend()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
releasePointerCapture()
remove()
removeAttribute()
removeAttributeNode()
removeAttributeNS()
replaceChildren()
replaceWith()
requestFullscreen()
requestPointerLock()
scroll()
scrollBy()
scrollIntoView()
scrollIntoViewIfNeeded()
scrollTo()
setAttribute()
setAttributeNode()
setAttributeNodeNS()
setAttributeNS()
setCapture()
setPointerCapture()
toggleAttribute()
afterscriptexecute
auxclick
blur
click
compositionend
compositionstart
compositionupdate
contextmenu
copy
cut
dblclick
DOMActivate
DOMMouseScroll
error
focus
focusin
focusout
fullscreenchange
fullscreenerror
gesturechange
gestureend
gesturestart
keydown
keypress
keyup
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
mousewheel
MozMousePixelScroll
msContentZoom
MSGestureChange
MSGestureEnd
MSGestureHold
MSGestureStart
MSGestureTap
MSInertiaStart
MSManipulationStateChanged
overflow
paste
scroll
select
show
touchcancel
touchend
touchmove
touchstart
underflow
webkitmouseforcechanged
webkitmouseforcedown
webkitmouseforceup
webkitmouseforcewillbegin
wheel