0

只是一个简单的问题。

我有这个链:

$(this).fadeOut(800); //which works good.

然后尝试删除它:

$(this).fadeOut(800).remove(); //makes it remove instantly, without fadeOut effect.

有什么帮助吗?

http://api.jquery.com/fadeOut/#fadeOut-duration-complete

4

5 回答 5

9

您想尝试使用完整的回调函数fadeOut

$(this).fadeOut(800, function(){
  $(this).remove(); //This will execute once the fadeOut is completed.
});

可能缓存$(this)

 var $this = $(this);
   $this.fadeOut(800, function(){
      $this.remove(); //This will execute once the fadeOut is completed.
    });

句法:

.fadeOut([持续时间] [,完成])

在调用remove()作为链之后fadeOut()它会在启动动画后立即被调用并且不会等待动画完成;因此,您想利用完整的回调来确保在动画完成后完成。

只是用另一个选项扩展答案,使用promise()done()

 var $this = $(this);
 $this.fadeOut(800).css('background-color', 'blue')
                    .promise()
                      .done(function () {
                         $this.remove();
                      });

演示

于 2013-06-13T20:41:37.077 回答
3

使用回调:

$(this).fadeOut(800, function(){
    $(this).remove();
});

问题是该remove()函数不等待动画完成,而是简单地删除元素;而remove()仅在动画完成后使用回调调用。

参考:

于 2013-06-13T20:42:06.140 回答
2

remove在方法的回调中执行fadeOut方法

$(this).fadeOut(800,function(){
  $(this).remove();
});
于 2013-06-13T20:41:50.740 回答
2

改为使用$(this).fadeOut(800, function() { $(this).remove(); });。通过放置.remove()回调,它只会在动画完成后触发。

于 2013-06-13T20:42:21.497 回答
1

使用淡出回调函数

$(this).fadeOut(800,function(){
  $(this).remove();
})
于 2013-06-13T20:41:54.860 回答