检测元素是否应用CSS动画的最佳方法是什么

Joh*_*rty 4 javascript css3 web-component css-transitions css-animations

我正在尝试在CSS动画完成后在Web组件上触发事件,但是用户可能会使用事件从不会触发的animation: none;含义从元素中清除动画transitionend

// wait for dialog close animation to end before firing closed event
element.addEventListener('transitionend', function() {

    // fire dialog closed event
    self.dispatchEvent(new CustomEvent('pure-dialog-closed', { 
        bubbles: true, 
        cancelable: true 
    }));
});
Run Code Online (Sandbox Code Playgroud)

为了确保自定义事件始终触发,我需要确定元素或其任何子级是否应用了动画,如果没有,请pure-dialog-closed立即触发该事件。

我已尝试检查style.animationNameself.style.transition但似乎无法正常工作。我需要一种简单的方法来检查某个元素或其任何子元素是否应用了CSS动画。

Thu*_*tha 5

您可以使用该getComputedStyle功能。以下是读取transitiondiv属性的示例。

在这里阅读更多有关此的内容。
https://developer.mozilla.org/zh-CN/docs/Web/API/Window/getComputedStyle

function getTheStyle() {
  var elem = document.getElementById("elem-container");
  var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("transition");
  document.getElementById("output").innerHTML = theCSSprop;
}

getTheStyle();
Run Code Online (Sandbox Code Playgroud)
#elem-container {
  position: absolute;
  left: 100px;
  top: 200px;
  height: 100px;
  transition: all 1s;
}
Run Code Online (Sandbox Code Playgroud)
<div id="elem-container"></div>
<div id="output"></div>
Run Code Online (Sandbox Code Playgroud)


Joh*_*rty 3

谢谢@Thusitha。我使用window.getComputedStyleanimation-durationtransition-duration确定动画是否存在,因为其中任何一个都需要大于 0 秒才能播放动画/过渡。

以下检查所有元素,包括传入的元素:

/**
 * Determine if an element of any or its children have a CSS animation applied
 * @param {HTMLElement} el - element to inspect
 * @returns {boolean} true if an animation/transition detected, otherwise false
 */
function hasCssAnimation(el) {

  // get a collection of all children including self
  var items = [el].concat(Array.prototype.slice.call(el.getElementsByTagName("*")));

  // go through each item in reverse (faster)
  for (var i = items.length; i--;) {

    // get the applied styles
    var style = window.getComputedStyle(items[i], null);

    // read the animation/transition duration - defaults to 0
    var animDuration = parseFloat(style.getPropertyValue('animation-duration') || '0');
    var transDuration = parseFloat(style.getPropertyValue('transition-duration') || '0');

    // if we have any duration greater than 0, an animation exists
    if (animDuration > 0 || transDuration > 0) {
      return true;
    }
  }

  return false;
}

var elementToTest = document.querySelector('.one');
var result = hasCssAnimation(elementToTest);

alert(result);
Run Code Online (Sandbox Code Playgroud)
div {
  font-size: 14px;
  padding: 20px;
  color: #fff;
}

.one {
  background: red;
}

.two {
  background: green;
  animation: zoomIn 3s ease; /* <-- animation applied to child */
}

.three {
  background: blue;
}

@keyframes zoomIn {
  from {
    opacity: 0;
    transform: scale3d(.3, .3, .3);
  }
  50% {
    opacity: 1;
  }
}
Run Code Online (Sandbox Code Playgroud)
<div class="one">
  <div class="two"> <!-- animation on child element -->
    <div class="three">
      Hello World
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)