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

时间:2009-11-27 07:05:21

标签: javascript jquery

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

8 个答案:

答案 0 :(得分:333)

$target.hide('slow');

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

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

答案 1 :(得分:93)

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

$('#target_id').fadeOut(300, function(){ $(this).remove();});

重复:How to "fadeOut" & "remove" a div in jQuery?

答案 2 :(得分:17)

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

这应该有效

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

答案 3 :(得分:17)

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

答案 4 :(得分:7)

所有答案都很好,但我发现他们都缺乏那种专业和#34;擦亮"。我想出了这个,渐渐消失,向上滑动,然后移开:

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

答案 5 :(得分:1)

我参加派对的时间已经很晚了,但对于像我这样来自谷歌搜索的人并没有找到合适的答案。不要误解我这里有好的答案,但不完全是我想要的,不用多说,这就是我做的:

$(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>

答案 6 :(得分:0)

我已经修改了Greg的答案,以适应我的情况,并且它有效。这是:

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

答案 7 :(得分:-4)

你的意思是

$target.hide('slow')