css转换的可选promise()函数
alternative promise() function for css transitions
我制作了一个带有8*8个div元素的游戏,这些元素正在制作动画,目前我正在使用JQuery中的动画功能。
当我调用动画启动的函数时,我使用以下代码来检查动画是否全部停止:
function start_animation()
{
//the animations start here
$("div").promise().done(function() {
//here comes the code for when the animations stoped
}
}
现在我想用css转换来制作动画,但我找不到检查所有动画是否都完成的解决方案。
我试过了:
$("div").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });
但这会在每次完成1个动画时进行回调。
有其他方法可以做到这一点吗?
查看promise
函数的作用https://github.com/jquery/jquery/blob/master/src/queue.js#L119.您将需要使用类似的代码,只是您不会将resolve
挂接到动画队列上,而是将其绑定为事件处理程序。
简而言之:
function start_animation() {
var divs = $("div");
// the animations start here
var count = divs.length;
// using `.one` automatically unbinds the handler
divs.one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function() {
if ( --count == 0) {
// here comes the code for when all the animations stoped
}
}
}
相关文章:
- 根据是否解析了 Promise 从函数返回值
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- AngularJS和promise值在调用本地函数时的效果-未定义
- Promise函数在.then之后未运行函数化
- 如何在异步函数中正确使用Promise.all()和then()
- 函数参数在内部不可见,返回新的Promise(函数(resolve,reject).传递给被promise化的函数的
- 多次调用promise函数,直到另一个promise函数满足条件
- 使用promise通过外部函数从DOM中检索属性
- 在promise完成后,依次运行返回promise的函数
- 如何在将Node异步函数转换为promise时使用Q库
- ES2015 Promise链-为什么promiseized函数会立即运行
- 如果没有其他函数链接到promise,则默认行为
- 如何正确使用基于Javascript Promise的函数
- 如何将多个onError函数分配给promise(由angular's$http.post返回)
- 当数组中的某个函数没有返回promise时,我可以使用$q.all吗
- bluebird promise catch() 没有使用 Promise.CancelError 调用内部函数
- 在 promise 中获取构造函数变量
- 使用 Promise 在另一个函数中调用带有 Bluebird 承诺库的函数
- JavaScript Promise 解析并拒绝函数调用序列
- Angularjs$http删除$q promise导致TypeError:对象不是函数