简体   繁体   English

为什么jQuery .animate回调发生在动画之前?

[英]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回调

See THE DEMO 参见THE DEMO

$('#clickButton').click(function() {
  $('.spanClass').animate({
    left: '+=200'
  }, 500, positionReset);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么要在动画之前执行此Jquery动画的回调函数? - Why would the callback function on this Jquery animation be executing before the animation does? jQuery动画-在动画完成之前执行回调 - Jquery animate - callback executing before animation is complete jquery 动画回调:完成/完成或 promise().done() 在 animation 之前开始 - jquery animate callback : complete/done or promise().done() starts before animation 为什么jQuery的.animate()在完成动画之前运行“ complete”功能? - Why does jQuery's .animate() run the “complete” function before it completes the animation? 为什么这个jQuery片段之前没有动画一次? - Why does this jQuery snippet not animate once before it? jQuery动画不动画 - jquery animation does not animate jquery .animate()-动画时间之后突然发生更改,没有可见的过渡 - jquery .animate() - changes occur suddenly after the animation time, with no visible transition 动画完成之前的jQuery回调调用 - jquery callback calls before animation completes 为什么 CSS 过渡不能很好地与基于 jQuery 的 animate 方法的另一个动画配合使用? - Why does a CSS transition not work well with another animation based on jQuery's animate method? 为什么动画前.animate to scrollTop跳到0? - Why does .animate to scrollTop jump to 0 before animating?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM