添加动画
可以使用 add() 方法或 sync() 方法将动画添加到时间轴。
动画创建
使用 add() 方法直接创建动画并将其添加到时间轴。
这允许将补间值与时间轴的现有子项进行合成。
timeline.add(targets, parameters, position);
参数
| 名称 | 接受 |
|---|---|
| targets | 目标 |
| 参数 | 一个包含可动画属性、补间参数、播放设置和动画回调的Object。 |
| 位置 (可选) | 时间位置 |
动画同步
使用 sync() 方法同步现有动画。
补间值合成在动画创建时处理,添加时不会影响时间轴的现有子项。
const animation = animate(target, { x: 100 });
timeline.sync(animation, position);
参数
| 名称 | 接受 |
|---|---|
| 动画 | 动画 |
| 位置 (可选) | 时间位置 |
返回
时间轴本身
可以与其他时间轴方法链式调用。
添加动画代码示例
import { createTimeline, animate } from 'animejs';
const circleAnimation = animate('.circle', {
x: '15rem'
});
const tl = createTimeline()
.sync(circleAnimation)
.add('.triangle', {
x: '15rem',
rotate: '1turn',
duration: 500,
alternate: true,
loop: 2,
})
.add('.square', {
x: '15rem',
});
<div class="large row">
<div class="medium pyramid">
<div class="triangle"></div>
<div class="square"></div>
<div class="circle"></div>
</div>
</div>