【问题标题】:How to remove a element after animation?如何在动画后删除元素?
【发布时间】:2015-02-26 11:16:25
【问题描述】:

我没有太多的 CSS 动画。我想在动画后删除一个元素。所以我使用animate.css。我已经尝试了下面的代码,但它仍然无法正常工作。

function deleteDevice(id) {
    $('#'+id).addClass("animated bounceOutRight");
    $('#'+id).queue(function() {
        $(this).remove();
        $(this).dequeue();
   });
}

动画后如何移除元素?

【问题讨论】:

标签: jquery css animation


【解决方案1】:

您可以检测动画何时完成。阅读here

$('#yourElement').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', doSomething);

$(document).ready(function() {  
  $('button').on('click', function() {
    deleteDevice('element');
  });
});


function deleteDevice(id) {
  $('#' + id).addClass("animated bounceOutRight");

  $('#element').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
    $(this).remove();
  });
}
@import url(http://daneden.github.io/animate.css/animate.min.css
);
 div {
  background: #abcdef;
  height: 100px;
  width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Delete</button>
<div id="element"></div>

【讨论】:

    【解决方案2】:

    添加将在动画结束时触发的回调函数,此时您可以删除/隐藏您的 HTML。

    在下面的代码中,我从 DOM 中删除了动画 div,你可能只是隐藏它。

    http://jsfiddle.net/yhwebLon/2/

    <button>Start Animation</button>
    
    <div id="target" style="background:#98bf21;height:100px;width:100px;position:absolute;"></div>
    
    
    
    
    $(document).ready(function(){
        $("button").click(function(){
            $("#target").animate({left: '250px'}, function(){
                alert('animation done');
                $("#target").remove();
            });
        });
    });
    

    【讨论】:

    • 我的动画会弹跳并向左移动。而且我不熟悉css动画。我想使用“animate.css”。那么还有其他可能吗?
    猜你喜欢
    • 2010-11-28
    • 2017-04-26
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多