วิธีการลบองค์ประกอบอย่างช้าๆด้วย jQuery?


179

$target.remove() สามารถลบองค์ประกอบออกได้ แต่ตอนนี้ฉันต้องการให้กระบวนการลงไปด้วยภาพเคลื่อนไหวความรู้สึกบางอย่างทำอย่างไร?

คำตอบ:


355
$target.hide('slow');

หรือ

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

เพื่อเรียกใช้ภาพเคลื่อนไหวแล้วลบออกจาก DOM


7
เมธอด. Remove () ลบโหนดออกจาก DOM โดยเฉพาะ วิธีการ. leather () เปลี่ยนเฉพาะการแสดงผลของแอททริบิวที่มองไม่เห็น แต่ยังคงมีอยู่
micahwittman

2
@Envil ผู้โพสต์ถามถึงวิธีลบอย่างช้าๆ . ลบ () ทำทันที
พิกเซลที่

4
@pixelearth ใส่$(this).remove()เข้าไปในฟังก์ชั่นการโทรกลับ ใช้งานได้ดีกว่า$target.remove()
Envil


20

หากคุณต้องการซ่อนแล้วลบองค์ประกอบใช้วิธีการลบภายในฟังก์ชั่นการโทรกลับของวิธีการซ่อน

สิ่งนี้น่าจะใช้ได้

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

+1 สำหรับการมีคำตอบที่ถูกต้องตามความคิดเห็นข้างต้น อย่างใดฉันชอบ$(this)แทนการทำซ้ำ$targetเกินไป
goodeye

ตรงนี้เป็นสิ่งที่ฉันต้องการหลังจากที่ฉันพยายามคำตอบที่ได้รับการยอมรับมันดูเรียบเนียนมาก :)
Catalin Hoha


11

คำตอบทั้งหมดเป็นสิ่งที่ดี แต่ฉันพบว่าพวกเขาทั้งหมดขาด "ขัดเงา" แบบมืออาชีพ

ฉันมากับสิ่งนี้ซีดจางเลื่อนขึ้นแล้วเอาออก:

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

3

ฉันไปงานเลี้ยงเล็ก ๆ น้อย ๆ แต่สำหรับคนอย่างฉันที่มาจากการค้นหาของ 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

ฉันได้แก้ไขคำตอบของ Greg เพื่อให้เหมาะกับกรณีของฉัน นี่มันคือ:

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

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.