【发布时间】:2018-04-24 14:32:05
【问题描述】:
我尝试制作一个旋转块,其旋转速度由<input type="range" 控制。问题是我找不到速度更新时不重启动画的解决方案。
我尝试了三种变体:
通过JS直接设置CSS动画速度。 — 重新开始动画;
jQuery 的
animate— 不适用于转换;库
anime.js— 它有变速的方法,但它也 重新启动动画(或者只是让块跳转,不清楚)
什么方法可以通过JS动画来创建平滑变化?
let block = anime({
targets: '#transforms .block',
rotateY: '360',
easing: 'linear',
loop: true,
duration: 1000,
});
var el = document.querySelectorAll('#range')[0];
el.addEventListener('change', function() {
// console.log(value);
// console.log(pos);
var value = this.value;
anime.speed = value/20;
// console.log(block);
})
.block {
width: 500px;
height: 300px;
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.2.0/anime.js"></script>
<div id="transforms">
<div class="block"></div>
</div>
<input id="range" type="range" min="0" max="20" step="1" value="10">
anime.js 示例代码On Codepen
【问题讨论】:
-
我也在尝试这样做。通过循环多次发出 animate 命令,我与 Velocity.js 非常接近。唯一的问题是在更改速度和实际生效之间存在延迟。动画至少是一致的,可能是因为它只是在完成 360 度旋转后才调整速度。
Velocity(rotor, { rotateZ: 360, }, {duration: 5000, loop: true, easing: "linear"});Velocity(rotor, { rotateZ: 360, }, {duration: 1000, loop: true, easing: "linear"});
标签: javascript css anime.js