如何使用jQuery缓慢删除元素?

Mas*_*ask 174 javascript jquery

$target.remove() 可以删除元素,但是现在我希望这个过程有一些感觉动画,怎么做?

Gre*_*reg 339

$target.hide('slow');
Run Code Online (Sandbox Code Playgroud)

要么

$target.hide('slow', function(){ $target.remove(); });
Run Code Online (Sandbox Code Playgroud)

运行动画,然后从DOM中删除它

  • .remove()方法非常具体地从DOM中删除节点..hide()方法仅更改要显示的显示属性不可见,但仍然存在. (7认同)
  • @pixelearth在回调函数中放置了`$(this).remove()`.这比`$ target.remove()`效果更好 (4认同)
  • @Envil海报询问如何慢慢将其删除。.remove()立即执行。 (2认同)

mic*_*man 95

target.fadeOut(300, function(){ $(this).remove();});
Run Code Online (Sandbox Code Playgroud)

要么

$('#target_id').fadeOut(300, function(){ $(this).remove();});
Run Code Online (Sandbox Code Playgroud)

重复:如何在jQuery中"淡出"和"删除"div?


rah*_*hul 17

如果需要隐藏然后删除元素,请使用hide方法的回调函数中的remove方法.

这应该工作

$target.hide("slow", function(){ $(this).remove(); })
Run Code Online (Sandbox Code Playgroud)


小智 17

$('#ur_id').slideUp("slow", function() { $('#ur_id').remove();});
Run Code Online (Sandbox Code Playgroud)


Sha*_*rpC 7

所有答案都很好,但我发现他们都缺乏专业的"润色".我想出了这个,淡出,向上滑动,然后移除:

$target.fadeTo(1000, 0.01, function(){ 
    $(this).slideUp(150, function() {
        $(this).remove(); 
    }); 
});
Run Code Online (Sandbox Code Playgroud)