传递参数以设置回调函数的动画
Pass parameter to animate callback function
我有下面的jQuery片段,我试图将一个参数传递给animate方法的函数,但无法正确执行。
function move() {
var points = [13, 8, 5];
for(var pointIdx=0; pointIdx < points.length-1; pointIdx++) {
..
..
// animate move.
$('#my_id', gameWindow.document).animate({ left: "50px" ,top: "50px" }, 1500, 'linear',
function(pointIdx) {
console.log("Animation: Iteration=" + pointIdx); // pointIdx is undefined at this point, why?
...
...
}
)
}
}
如何做对?
谢谢!
pointIdx
未定义,因为jQuery动画的完整回调没有任何可用参数。
http://api.jquery.com/animate/
完成
类型:Function()
动画完成后要调用的函数。
因此,当你在animate函数中包含参数pointIdx
时,完成像这样的回调
function(pointIdx) {
您正在覆盖变量pointIdx
。因为JavaScript使用一个词法变量环境堆栈,所以pointIdx
会被推送到堆栈中,并带有从完整回调中传入的值。这个值是undefined
,当您尝试在完整回调的执行上下文中读取变量pointIdx
的值时,它会获得堆栈的最顶端值,即undefined
。这就是为什么此处未定义pointIdx
的原因。
为了将pointIdx
的值存储在此回调中,您需要将其从参数中删除,还需要使用IIFE关闭它。
jsFiddle Demo
for(var pointIdx=0; pointIdx < points.length; pointIdx++) {
..
..
// animate move.
//close over pointIdx
(function(pointIdx){
//now the execution context is inside of the anonymous function
//and the value of pointIdx in the loop is stored in
//variable pointIdx (same name for consistency) is at the top of that variable environment
$('#my_id', gameWindow.document).animate({ left: "50px" ,top: "50px" }, 1500, 'linear',
function() {
console.log("Animation: Iteration=" + pointIdx); // pointIdx will now contain the iteration value from the for loop
...
...
}
)
})(pointIdx);
}
问题在于时间-动画回调发生在1500毫秒之后,但for循环几乎立即完成。你需要这样重写:
var points = [13, 8, 5];
var pointIdx = 0;
function animateForPointIndex(index) {
$('#my_id').animate({
left: "50px",
top: "50px"
}, 1500, 'linear', function () {
pointIdx++;
if (pointIdx < points.length) {
console.log("Animation: Iteration=" + pointIdx);
// do what you need to here
animateForPointIndex(pointIdx);
}
});
}
animateForPointIndex(0);
只有当点索引小于点数组的长度时,才会在每次完成后递归调用animate函数。
相关文章:
- 在动画结束之前调用函数
- jQuery animate()函数没有't设置动画
- 在Zepto动画函数中使用变量
- HTML5(Bootstrap)通过函数调用运行动画
- 调用函数和单击动画
- 在没有大量回调函数的情况下在列表元素上循环播放同一动画
- 如何简化jquery动画函数代码
- 将项目放在动画和 inserbefore 函数混乱之后
- Jquery动画和when函数
- 更改 jQuery 队列中动画的缓动函数
- 将对象函数传递给请求动画帧时丢失对象引用
- Jquery动画函数不工作
- 从jQuery UI滑块更改为'noUiSlider'-用于更新动画进度滑块的函数
- 通过函数(动画)每次重新迭代时重置画布笔划.怎样
- 未捕获的类型错误:使用 ig 时,未定义不是一个函数.动画表
- jQuery ScrollTop() 函数动画菜单
- JQuery,再次启动函数?动画队列
- 函数.动画百分比不起作用
- 尝试编写javascript函数动画scrollTo方法
- 如何在一个回调函数动画chrome扩展图标