Animation

CSS

animation-name
@keyframes 指定的关键帧名称。none 表示无动画。(不建议使用关键字做 name)
animation-duration
设置动画一周期[one cycle]的时长。default: auto[0]
设置为 0 时, 会自动触发 animationstart 和 animationend 事件
但在 scroll-driven-animations 里, auto 覆盖整个周期; 设置具体的值, 同样是无效的
对于 scroll-driven-animations , firefox 下, 需要设置 animation-duration: 1ms; 才能生效
animation-timing-function
设置动画速度函数 - 动画在关键帧之间是如何变化。
linear[=linear(0,1)]/linear()/ease/ease-in/ease-out/ease-in-out/cubic-bezier(x1, y1, x2, y2)
step-start/step-end/steps(n,pos[jump-start|jump-end|jump-both|jump-none])/start[=steps(n, jump-start)]/end[=steps(n, jump-end)]
jump-start: 跳过第一帧; jump-end: 跳过最后一帧; jump-both: 同时跳过第一和最后一帧; jump-none: 不跳过任何一帧
可在 @keyframes 中不同时间段定义不同的 animation-timing-function - 自定义 (手写)
DEMO:
在线生成 cubic-bezier(x1, y1, x2, y2) 工具
在线生成 linear(...) 工具
animation-delay
设置延时,即从元素加载完成之后到动画序列开始执行的这段时间; 支持负值,表示从已运动 Math.abs(value) 时间时,元素应该所处位置开始; 同时,支持 s, ms 时间单位 [0]
支持 inherit. [浏览器支持]
animation-iteration-count
设置动画重复次数,默认值是 1。[n | infinite]
animation-direction
设置动画在每次运行完后是反向运行还是重新回到开始位置重复运行。
normal[default] | reverse | alternate | alternate-reverse
animation-fill-mode
描述动画开始前/结束后,@keyframes 中定义的样式对元素本身样式的影响
none 默认情况下,动画开始前 [animation-delay 这段时间] 和动画结束后,保留元素最初样式
backwards 动画开始前,应用动画定义中的开始样式,动画结束后,回退到元素初始样式。受 animation-direction/animation-iteration-count 影响。单次情况下:direction 为 normal/alternate 时,应用 from 样式; 为 reverse/alternate-reverse 时,应用 to 样式;
forwards 动画开始前,保持元素最初样式,动画结束后,应用动画定义中的结束样式。当 animation-iteration-count 为 0 时,forwards 和 backwards 相同;
both 同时使用 backwards 和 forwards 规则; 即:动画开始前应用动画定义中的开始样式,动画结束后应用动画定义中的结束样式
ps: 添加 animation-delay: 1s; 看动画效果
animation-play-state
停止和恢复动画。[running | paused]

简写形式

animation: name duration function delay count direction fill-mode play-state;
background-position
overflow: hidden + image + translateY
object-view-clip

none -> block

@starting-style
设置动画开始时的样式
transition-behavior
allow-discrete 设置动画支持离散值

动画合成

animation-composition
replace[default] 替换默认值
accumulate 值累积
add 效果累积

Animation Timeline caniuse

animation-timeline
none
auto[default] - DocumentTimeline
--scroll-timeline-name, scroll()
--view-timeline-name, view()
animation-range-start
动画开始位置
normal[default][0%]/percentage/length
view-timeline 情况下,还有额外几个值:cover/contain/entry/entry-crossing/exist/exist-crossing
重要提醒:subject 尺寸可能小于/等于/大于 viewport 尺寸
cover: subject 与 viewport 相交
contain: subject 在视觉上完全包含/被包含 viewport
常见情况是,subject 的尺寸小于 viewport 尺寸:entry/entry-crossing 的 100% 表示 subject 完全进入 viewport
当 subject 的尺寸大于 viewport 尺寸时:entry 100% 表示 subject 刚好覆盖 viewport; entry-crossing 100% 表示 subject 完整穿过 entry 线
可以在view-timeline-tool查看效果
animation-range-end
动画结束位置,和 animation-range-start 对应
上面两个属性的简写形式
animation-range: animation-range-start animation-range-end
当只有一个值时,第二个值为 normal
animation-range: 20% 相当于 animation-range: 20% normal

Scroll Timeline caniuse

scroll-timeline-name
使用 css 中自定义变量的命名方式命名,用来标识当前具有滚动条的元素滚动进度作为 timeline 提供给 animation 使用
scroll-timeline-axis
元素可能在水平/垂直两个方向上都存在滚动条,因此需要指定使用哪个,默认使用逻辑属性 block
block[default]/inline/x/y
上面两个属性的简写形式
scroll-timeline: scroll-timeline-name scroll-timeline-axis;
因为 scroll-timeline-axis 有默认值,可以忽略不写,只定义 timeline-name 即可
scroll()
按滚动条进度表示 timeline, 需要有可滚动的元素做 scroller,而 scroll() 函数提供简便方法,也就是匿名 scroll-timeline
scroll(nearest[default]) 默认值,可以省略参数,意思是把最近具有滚动条的祖先元素做 scroller, 按它的滚动进度做 timeline
scroll(root) 用文档的根元素做 scroller
scroll(self) 元素自身做 scroller; ps: 尽管设置 overflow: hidden, 但如果内容使 scroller 存在 [隐藏滚动条] 时,同样可以让 animation 生效
scroll(block[default]/inline/x/y)
scroll(nearest, inline), scroll(x nearest) 关键字是固定的几个,位置任意写,都可以识别
The scroll() CSS function can be used with animation-timeline to indicate a scrollable element (scroller) and scrollbar axis that will provide an anonymous scroll progress timeline for animating the current element. The scroll progress timeline is progressed through by scrolling the scroller between top and bottom (or left and right). The position in the scroll range is converted into a percentage of progress — 0% at the start and 100% at the end.
查看更多 收起

View Timeline caniuse

属性的语义基本和 scroll-timeline 类似,定义名称和方向

view-timeline-name
view-timeline-axis
上面两个属性的简写形式
view-timeline: view-timeline-name view-timeline-axis
view-timeline-inset
和 animation-range 作用类似,用于定义位置来计算 timeline; 和 scroll-padding 一样扩展 viewport 区间,定义 port 点
view()
可以理解成 view-timeline-axis 和 view-timeline-inset 的简写形式
@keyframes 支持 animation-range percentage

scope

timeline-scope
可以理解为,给元素定义好 view-timeline/scroll-timeline, 便于任意其他元素使用
*-timeline-name 存在 scope 概念,只能作用于后代元素上
可以在 *-timeline-name 元素的祖先元素上定义 timeline-scope: --custom-timeline-name 属性,意思是把 scope 向上提高,然后作用于更多后代元素上

Multiple Animation

语法:animation: animation-defined[, animation-defined]*;
单个动画不支持元素在同一时间内,不同属性使用不同 animation-timing-function. Multiple-Animation 可实现 (transform 不能组合使用,性能不佳)
为了使用 transform 来实现曲线过程动画,可利用容器嵌套方法。见第三个例子。

Animation Event

animationstart
event.animationName - @keyframes 名称
event.elapsedTime - 动画开始与现在的时间间隔
animationiteration
animationend
animationcancel
查看开发者工具中 animation 相关事件
JavaScript/UI 和 CSS 动画不在一个进程,即:相互不阻塞。

offset - 代替原 motion 语法

offset-anchor
定义相对位置关系
auto = transform-origin
<position>
offset-path
ray
url
circle/ellipse/inset/polygon/path/xywh/rect/shape - basic shape
content-box/padding-box/border-box/fill-box/stroke-box/view-box - box-edge
offset-distance
定义在 path 上的偏移距离
example: offset-distance: 50%;
offset-position
path 缺省起点位置 [example: ray(0deg); circle(80px)] 时,使用 offset-position 定义
normal[default] = 50% 50%
auto = left top
<position>
offset-rotation
auto - 从 x 轴正方向旋转到 path 方向的角度
angle - 旋转固定角度 (ps: 向上是 0deg)
auto <angle> - 在 auto 值基础上累加 angle 值得到最终旋转角度
reverse - auto 180deg
定义 animation - 见上面,定义@keyframes.
@keyframes { from { offset-distance: 0%; } to { offset-distance: 100%; } }
DEMO:

JavaScript Animation

相关文档/链接