stretch()JS

更改动画的总时长及其补间时长以适应特定时间。
总时长等于一次迭代的时长乘以总迭代次数。因此,如果动画时长为1000毫秒且循环两次(总共3次迭代),总时长将为3000毫秒(1000 * 3)。

animation.stretch(duration);

参数

名称 类型 描述
duration Number 动画的新总时长(毫秒)

将动画拉伸到0也会将其所有过渡的持续时间设置为0,这将在随后调用stretch()时使它们的长度都相同。

返回

动画本身

可以与其他动画方法链式使用。

stretch() 代码示例

import { animate, utils, stagger } from 'animejs';

const [ $range ] = utils.$('.range');
const [ $totalDuration ] = utils.$('.value');

const animation = animate('.square', {
  x: '17rem',
  ease: 'inOutSine',
  delay: stagger(200),
});

const stretchAnimation = () => {
  const newDuration = +$range.value;
  $totalDuration.textContent = newDuration;
  animation.stretch(newDuration).restart();
}

stretchAnimation();

$range.addEventListener('input', stretchAnimation);
<pre class="large log row">
  <span class="label">total duration</span>
  <span class="value">0</span>
</pre>
<div class="medium row">
  <div class="square"></div>
</div>
<div class="medium row">
  <div class="square"></div>
</div>
<div class="medium centered row">
  <fieldset class="controls">
    <input type="range" min=100 max=2000 value=1000 step=100 class="seek range" />
  </fieldset>
</div>