首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检测元素是否应用CSS动画的最佳方法是什么?

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

Stack Overflow用户
提问于 2017-09-21 15:13:24
回答 3查看 2.8K关注 0票数 5

当CSS动画完成时,我试图在web组件上触发一个事件,但是用户可能会使用animation: none;清除元素中的动画,这意味着transitionend事件永远不会触发:

代码语言:javascript
运行
复制
// 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 
    }));
});

为了确保我的自定义事件总是触发,我需要确定元素或它的任何子元素是否应用了动画,如果没有,立即启动pure-dialog-closed事件。

我让已尝试检查了style.animationNameself.style.transition,但是它似乎不起作用。我需要一种简单的方法来检查一个元素或它的任何子元素是否应用了CSS动画。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-09-21 19:48:31

谢谢你@Thusitha。我使用window.getComputedStyle以及animation-durationtransition-duration来确定动画是否存在,因为动画的播放需要大于0。

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

代码语言:javascript
运行
复制
/**
 * 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);
代码语言:javascript
运行
复制
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;
  }
}
代码语言:javascript
运行
复制
<div class="one">
  <div class="two"> <!-- animation on child element -->
    <div class="three">
      Hello World
    </div>
  </div>
</div>

票数 3
EN

Stack Overflow用户

发布于 2017-09-21 15:35:40

您可以使用getComputedStyle函数。下面是一个读取div的transition属性的示例。

在这里读更多关于这个的文章。

https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

代码语言:javascript
运行
复制
function getTheStyle() {
  var elem = document.getElementById("elem-container");
  var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("transition");
  document.getElementById("output").innerHTML = theCSSprop;
}

getTheStyle();
代码语言:javascript
运行
复制
#elem-container {
  position: absolute;
  left: 100px;
  top: 200px;
  height: 100px;
  transition: all 1s;
}
代码语言:javascript
运行
复制
<div id="elem-container"></div>
<div id="output"></div>

票数 7
EN

Stack Overflow用户

发布于 2017-09-21 15:50:06

您可以监听动画结束动画启动事件。

代码语言:javascript
运行
复制
let element = document.getElementById("square");

element.addEventListener("animationstart", function(event) {
  element.innerHTML = "RED!"
  element.setAttribute('data-animating', true);
}, false);

element.addEventListener("animationend", function(event) {
  element.innerHTML = "YELLOW!"
  element.setAttribute('data-animating', false);
}, false);

setInterval(() => {
  console.log(element.getAttribute('data-animating'))
}, 500)
代码语言:javascript
运行
复制
#square{
  width: 100px;
  height: 100px;
  animation-name: anim;
  animation-duration: 4s;
  background-color: red;
  animation-fill-mode: forwards;
  text-align: center;
  vertical-align: middle;
  line-height: 100px;
}

@keyframes anim {
    to {background-color: yellow;}
}
代码语言:javascript
运行
复制
<div id="square"></div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46347531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档