简体   繁体   English

运行动画后如何删除该元素?

[英]How to delete this element after it runs the animation?

The delelement code is working fine but I want to delete the element after the css animation is finished for "delorean", then I need the animation to start again every time the button is clicked, how can I use my "delelement" code to achieve this? 删除代码工作正常,但是我想在完成“ delorean”的css动画后删除元素,然后每次单击按钮时都需要重新启动动画,如何使用“删除”代码来实现这个? thanks. 谢谢。

function touchDelorean(event) {
    event.preventDefault();
    if (showDelorean == true) {
        delorean();
    }
}

function delorean() {
    var image = document.createElement("img");
    image.id = "delorean";
    image.src = "Images/delorean/" + where + ".png";
    document.getElementById("deloreanContainer").appendChild(image);

//  delelement("deloreanContainer");
}

function delelement(elem) {
    var element = document.getElementById(elem);
    while (element.firstChild) { element.removeChild(element.firstChild); }
}

CSS CSS

#delorean {
position: absolute;
top: 0%;
left: 0%;
width: 29%;
height: 9.8%;
opacity: 1.0;
-webkit-animation: delorean_anim 10s linear;
-webkit-animation-direction: normal, horizontal ;
-webkit-animation-timing-function: cubic-bezier(1, 0, 1, 0);
}

@-webkit-keyframes delorean_anim {
0% { -webkit-transform: translate(-24vw, 0vw) scale(0.6, 0.6) rotate(0deg) }
20% { -webkit-transform: translate(137vw, 36vw) scale(3.5, 3.5) rotate(0deg) }
100% { -webkit-transform: translate(137vw, 36vw) scale(3.5, 3.5) rotate(0deg)}
}

On the element that runs the animation, given that it is image you can add an eventlistener.. 在运行动画的元素上,假设它是image ,则可以添加一个事件侦听器。

// Chrome, Safari and Opera
image.addEventListener('webkitAnimationEnd', function() {
  image.remove()
});

// Standard syntax
image.addEventListener('animationend', function() {
  image.remove()
});

If you're dealing with a css transition and not a css animation then you're looking for transitionend . 如果您要处理的是CSS过渡而不是CSS动画,那么您正在寻找transitionend These events can however be unreliable sometimes so if you know the length of the animation, you're better off with setTimeout as suggested above. 但是,有时这些事件可能不可靠,因此,如果您知道动画的长度,最好使用上面建议的setTimeout

In react.js the infamous CSSTransitionGroup component had many problems due to the transition event not fireing as expected. 在react.js中,臭名昭著的CSSTransitionGroup组件由于过渡事件未按预期触发而出现了许多问题。 They "solved" the problem by requiring specifying transition timeout values for the component.. 他们通过要求为组件指定过渡超时值来“解决”该问题。

Use setTimeout() and do a time equal or greater to that of your CSS Transition time... so if: 使用setTimeout()并等于或大于CSS转换时间的时间...因此,如果:

transition: all 3s ease;

then... 然后...

function delorean() {
    var image = document.createElement("img");
    image.id = "delorean";
    image.src = "Images/delorean/" + where + ".png";
    document.getElementById("deloreanContainer").appendChild(image);

    setTimeout( function { delelement("deloreanContainer"); }, 3000 );
}

function delelement(elem) {
    var element = document.getElementById(elem);
    while (element.firstChild) { element.removeChild(element.firstChild); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM