javascript - 在 JavaScript 对象中制作重复动画循环
问题描述
我想将动画循环排序为 JavaScript 中的对象。对象的功能会是这样的
var a;
var move = new Repeat(10,function(){a++});
它会以动画速度运行,而不是立即运行。
这是我的原始代码(不起作用)。
function obj(id){return document.getElementById(id);}
function Repeat(fn,rpt){
this.function = fn;
this.repeat = rpt;
this.counter = 0;
this.id=setInterval(this.start,5);
this.start = function(){
if (this.counter == this.repeat) {
clearInterval(id);
} else {
this.repeat++;
this.function();
}
}
}
var box = obj('animation');
var a=0;
var move = new Repeat(function(){
a++;
box.style.top=a+'px';
box.style.left=a+'px';
},300);
move.start();
因此,我正在寻找实现这一目标的建议。
解决方案
也许您可以使用 Web Animations API 来做到这一点,我想这会更容易https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
推荐阅读
- php - Select2 - 无法选择 ajax 结果(不可点击)
- php - 将函数参数传递给 sql 查询
- scala - 如何解释 fold 和 foldK 之间的区别?
- angular - 模块构建失败:错误:找不到模块“node-sass”
- php - 仅对继承的方法禁用 squiz.commenting.functioncomment.missing phpcs 嗅探
- vba - 根据表格中提供的输入,将其他演示文稿中的特定幻灯片插入到当前
- matlab - 在 MATLAB 中生成 1 和 -1
- unix - 用字符替换列中的空格
- r - 当轴位于图形的顶部和底部时,按 y 轴对齐水平图
- php - Laravel 5.6 单击激活链接仅重定向到主页