transitionend event fires twice

后端 未结 6 779
渐次进展
渐次进展 2020-12-04 19:33

I have the following code and my problem is that the transitionend event is fired twice. I don\'t know what\'s causing this. I suspected the vendor prefixes cau

6条回答
  •  無奈伤痛
    2020-12-04 20:12

    For anyone looking for a simple, one time copy and paste solution (I've only included the necessary css). This doesn't answer the question and it does answer what I was looking for when I landed here.

    CSS:

    .my-elem {
        transition: height 0.5s ease-out, opacity 0.5s ease-out;
    }
    

    JavaScript:

    var elem = document.querySelector(".my-elem");
    
    var transitionCounter = 0;
    
    var transitionProp = window.getComputedStyle(elem , null)["transition-property"] || "";
    
    // We just need to know how many transitions there are
    var numTransitionProps = transitionProp.split(",").length;
    
    elem.addEventListener("transitionend", (event) => {
      // You could read event.propertyName to find out which transition was ended, 
      // but it's not necessary if you just want to know when they are all done.
      if (transitionCounter < (numTransitionProps - 1)) {
        transitionCounter++;
      } else {
        transitionCounter = 0; // reset
        alert("I'm done!!!"); // do what you need to
      }
    }, false);
    

    Tested in IE11, Chrome 48 and Firefox 37.

提交回复
热议问题