[英]Why does jQuery .animate callback occur before the animation?
Why is the callback on this .animate
function happening, before the animation? 为什么在动画之前会发生这个
.animate
函数的回调?
Refer: http://jsfiddle.net/93Qyq/7/ 参考: http : //jsfiddle.net/93Qyq/7/
This is the Javascript portion: 这是Javascript部分:
$('#clickButton').click(function() {
$('.spanClass').animate({
left: '+=200'
}, 500, positionReset());
});
function positionReset(){
alert('complete!');
$('.spanClass').animate({'left':'-=200'})
}
This is the HTML: 这是HTML:
<div class="divClass">
<span id="a" class="spanClass">A</span>
<span id="b" class="spanClass">B</span>
<span id="c" class="spanClass">C</span>
</div>
<br>
<div id="clickButton">CLICK BUTTON</div><br>
That's because you're not passing your function to animate()
-- you're calling it , and passing the value it returns (which is not a function, and is subsequently ignored by the framework). 那是因为你没有将你的函数传递给
animate()
- 你正在调用它 ,并传递它返回的值 (这不是一个函数,随后被框架忽略)。
You have to remove the parentheses after your function: 您必须在函数后删除括号:
$('.spanClass').animate({
left: '+=200'
}, 500, positionReset); // No parentheses.
Instead of: 代替:
$('.spanClass').animate({
left: '+=200'
}, 500, positionReset());
Should be: 应该:
$('#clickButton').click(function() {
$('.spanClass').animate({
left: '+=200'
}, 500, positionReset); //not positionReset() or you will autocall callback function
});
Because you are invoking the function by putting brackets after it. 因为您通过在其后面放置括号来调用该函数。 To pass a reference to the function you just use it's name, without the brackets.
要传递对函数的引用,只需使用它的名称,不带括号。 eg
例如
$('#clickButton').click(function() {
$('.spanClass').animate({
left: '+=200'
}, 500, positionReset);
});
positionReset()
will call the function as its a function call. positionReset()
将函数调用为函数调用。 You need to pass the function object to the animate callback as positionReset
您需要将函数对象作为
positionReset
传递给animate回调
$('#clickButton').click(function() {
$('.spanClass').animate({
left: '+=200'
}, 500, positionReset);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.