简体   繁体   English

jQuery动画函数回调错误

[英]jQuery animate function callback bug

I have the following code for sliding out a div: 我有以下代码用于滑出div:

var current = $('.s_text:visible');
current.animate({
  right: 1014,
  opacity:0,
},{queue: false, duration:2000}, function() {
  current.hide();
});

for some reason, the callback function doesn't work! 由于某种原因,回调函数不起作用! but! 但! If I remove the option {queue:false, duration:2000} and replace it with ,2000,function().... the callback function works. 如果我删除选项{queue:false, duration:2000}并将其替换为,2000,function()....则回调函数有效。

current.animate({
      right: 1014,
      opacity:0,
    },2000, function() { // this one works...
      current.hide();
    });

Why is that? 这是为什么?

If you are using the second parameter of the .animate method as an options object, you cannot send the callback as a third parameter. 如果将.animate方法的第二个参数用作选项对象,则不能将回调作为第三个参数发送。

In your case, you need to use the complete parameter of the options object. 在您的情况下,您需要使用options对象的complete参数。

var current = $('.s_text:visible');
current.animate({
  right: 1014,
  opacity:0,
},{queue: false, duration:2000, complete:function() {
  current.hide();
}});

the two optional parameter sets this method receives are: 此方法接收的两个可选参数集是:

.animate( properties [, duration ] [, easing ] [, complete ] )

or 要么

.animate( properties, options )

but not both at once. 但不是两者都一次。

source: http://api.jquery.com/animate/ 来源: http//api.jquery.com/animate/

Animate: 动画:

.animate( properties, options )

Find reference here: http://api.jquery.com/animate/ 在此处找到参考: http : //api.jquery.com/animate/

var current = $('.s_text:visible');
current.animate(
  {
    right    : 1014,
    opacity  :0
  },{
    queue   : false, 
    duration:2000,
    complete: function() {
         current.hide();
    }
 });

If you want to use options (queue and duration) you cannot have a callback function like that; 如果要使用options (队列和持续时间),则不能有这样的回调函数。 you should include the callback function in options (see documentation ): 您应该在options包含回调函数(请参阅文档 ):

var current = $('.s_text:visible');
current.animate({
    right: 1014,
    opacity:0
},{
    queue: false, 
    duration:2000,
    complete: function() {
        current.hide();
    }
);

Because the .animate() has only two declarations: 因为.animate()只有两个声明:

  1. .animate( properties [, duration ] [, easing ] [, complete ] ) .animate(属性[,持续时间] [,缓动] [,完整])
  2. .animate( properties, options ) .animate(属性,选项)

none is for your useage, you could see more about .animate() here: http://api.jquery.com/animate/ 没有一个供您使用,您可以在此处查看有关.animate()更多信息: http : .animate()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM