jQuery动画函数回调错误

jQuery animate function callback bug

本文关键字:错误 回调 函数 动画 jQuery      更新时间:2024-02-14

我有下面的代码来滑出div:

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

由于某种原因,回调函数不起作用!但是如果我删除选项{queue:false, duration:2000}并将其替换为,2000,function()....,回调函数就会工作。

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

为什么?

如果使用.animate方法的第二个参数作为选项对象,则不能将回调作为第三个参数发送。

在您的情况下,您需要使用options对象的完整参数。

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

该方法接收的两个可选参数集是:

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

.animate( properties, options )

但不能同时两者。

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

因为.animate()只有两个声明:

  1. .animate(属性[,持续时间][,放松][,完成])
  2. .animate(属性,选项)

没有供您使用,您可以在此处查看有关.animate()的更多信息:http://api.jquery.com/animate/

动画:

.animate( properties, options )

在此处查找参考资料:http://api.jquery.com/animate/

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

如果你想使用options(队列和持续时间),你不能有这样的回调函数;您应该在options中包含回调函数(请参阅文档):

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