我正在使用minifiedjs.com的图书馆。使用这个脚本,我有一个div闪烁两次:
vbox1.animate({$backgroundColor: 'grey'}, 100)
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })正如您所看到的,它只是简单地将背景设置为灰色,返回到透明,然后又返回到灰色。问题是,我希望这个div能闪烁X次数。
我知道我可以通过链接更多的.then()动画来做到这一点,但这似乎有点重复。有人能帮我清理一下吗?
vbox1.animate({$backgroundColor: 'grey'}, 100)
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })发布于 2015-02-26 20:49:19
承诺的方式:
function next() {
return vbox1.animate({$backgroundColor: 'transparent'}, 100)
.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(next.total-- ? next : Boolean);
}
next.total=100;
vbox1.animate({$backgroundColor: 'grey'}, 100).then(next);我使用的是next.total而不是x,但是主要的想法是从尾部进行自我调用,直到完成为止,而不是提前循环/查询。
编辑:
作为可重用的(允许自定义目标、延迟和代表的#):
function animateMyStuff(target, period, reps){
reps=+reps||100; //default and coerce to real number
period=+period||100; //default and coerce to real number
function next() {
return target.animate({$backgroundColor: 'transparent'}, period)
.then(function() { return target.animate({$backgroundColor: 'grey'}, period) })
.then(reps-- ? next : Boolean);
}
return target.animate({$backgroundColor: 'grey'}, period).then(next);
}要像以前一样使用:animateMyStuff(vbox1, 100, 100);,或使用默认值animateMyStuff(vbox1);
发布于 2015-02-26 20:40:02
使用循环:
var animation = vbox1.animate({$backgroundColor: 'grey'}, 100)
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) });
for (var i=0; i < numberOfBlinks - 1; i++) {
animation = animation.then(function() { return vbox1.animate({$backgroundColor: 'grey'}, 100) })
.then(function() { return vbox1.animate({$backgroundColor: 'transparent'}, 100) });
}您只需附加尽可能多的.then即可。
https://stackoverflow.com/questions/28752235
复制相似问题