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


183

$target.remove() 可以删除该元素,但是现在我希望该过程随着动画的进行而降低,该怎么做?

Answers:


358
$target.hide('slow');

要么

$target.hide('slow', function(){ $target.remove(); });

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


7
.remove()方法非常明确地从DOM中删除了该节点。.hide()方法仅将显示属性更改为不可见,但仍然存在。
micahwittman,2009年

3
@Envil海报询问如何慢慢将其删除。.remove()立即执行。
pixelearth

4
@pixelearth放在$(this).remove()回调函数中。比$target.remove()
Envil


20

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

这应该工作

$target.hide("slow", function(){ $(this).remove(); })

+1表示答案正确,因为以上评论已得到它。我以某种方式喜欢$(this)而不是重复$target
眼神

这正是我尝试接受的答案后想要的,它看起来更加平滑:)
Catalin Hoha

18
$('#ur_id').slideUp("slow", function() { $('#ur_id').remove();});

12

所有的答案都是好的,但我发现它们都缺少专业的“抛光”。

我想出了这一点,淡出,向上滑动,然后移除:

$target.fadeTo(1000, 0.01, function(){ 
    $(this).slideUp(150, function() {
        $(this).remove(); 
    }); 
});

4

我参加聚会的时间不晚,但是对于像我这样的人来说,他们来自Google搜索,却找不到正确的答案。不要误会我的意思,这里有很好的答案,但不完全是我要找的东西,事不宜迟,这就是我所做的:

$(document).ready(function() {
    
    var $deleteButton = $('.deleteItem');

    $deleteButton.on('click', function(event) {
      event.preventDefault();

      var $button = $(this);

      if(confirm('Are you sure about this ?')) {

        var $item = $button.closest('tr.item');

        $item.addClass('removed-item')
        
            .one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
          
                $(this).remove();
        });
      }
      
    });
    
});
/**
 * Credit to Sara Soueidan
 * @link https://github.com/SaraSoueidan/creative-list-effects/blob/master/css/styles-4.css
 */

.removed-item {
    -webkit-animation: removed-item-animation .6s cubic-bezier(.55,-0.04,.91,.94) forwards;
    -o-animation: removed-item-animation .6s cubic-bezier(.55,-0.04,.91,.94) forwards;
    animation: removed-item-animation .6s cubic-bezier(.55,-0.04,.91,.94) forwards
}

@keyframes removed-item-animation {
    from {
        opacity: 1;
        -webkit-transform: scale(1);
        -ms-transform: scale(1);
        -o-transform: scale(1);
        transform: scale(1)
    }

    to {
        -webkit-transform: scale(0);
        -ms-transform: scale(0);
        -o-transform: scale(0);
        transform: scale(0);
        opacity: 0
    }
}

@-webkit-keyframes removed-item-animation {
    from {
        opacity: 1;
        -webkit-transform: scale(1);
        transform: scale(1)
    }

    to {
        -webkit-transform: scale(0);
        transform: scale(0);
        opacity: 0
    }
}

@-o-keyframes removed-item-animation {
    from {
        opacity: 1;
        -o-transform: scale(1);
        transform: scale(1)
    }

    to {
        -o-transform: scale(0);
        transform: scale(0);
        opacity: 0
    }
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
  
  <table class="table table-striped table-bordered table-hover">
    <thead>
      <tr>
        <th>id</th>
        <th>firstname</th>
        <th>lastname</th>
        <th>@twitter</th>
        <th>action</th>
      </tr>
    </thead>
    <tbody>
      
      <tr class="item">
        <td>1</td>
        <td>Nour-Eddine</td>
        <td>ECH-CHEBABY</td>
        <th>@__chebaby</th>
        <td><button class="btn btn-danger deleteItem">Delete</button></td>
      </tr>
      
      <tr class="item">
        <td>2</td>
        <td>John</td>
        <td>Doe</td>
        <th>@johndoe</th>
        <td><button class="btn btn-danger deleteItem">Delete</button></td>
      </tr>
      
      <tr class="item">
        <td>3</td>
        <td>Jane</td>
        <td>Doe</td>
        <th>@janedoe</th>
        <td><button class="btn btn-danger deleteItem">Delete</button></td>
      </tr>
    </tbody>
  </table>
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


</body>
</html>


绝对在这里指出它看起来很棒。:-)
SharpC

0

我已经修改了格雷格的答案以适合我的情况,并且有效。这里是:

$("#note-items").children('.active').hide('slow', function(){ $("#note-items").children('.active').remove(); });

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.