Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to read out or change CSS animation keyframes

In JavaScript it's easy to get the name and properties of CSS animations applied to an element:

var animName = element.style.webkitAnimationName;
// element.style.mozAnimationName
// etc...

But is there a way to read out or even change the CSS keyframes for animations?

like image 224
dan-lee Avatar asked Nov 11 '22 17:11

dan-lee


1 Answers

Thanks to the comments I was able to get to this solution:

var allStyles = document.styleSheets;
var keyframeType = window.CSSRule.WEBKIT_KEYFRAMES_RULE || window.CSSRule.KEYFRAMES_RULE;

for (var declaration in allStyles) {
  if (allStyles.hasOwnProperty(declaration)) {
    var ruleSet = allStyles[declaration].cssRules;

    for (var rule in ruleSet) {
      if (ruleSet.hasOwnProperty(rule)) {
        var currentRule = ruleSet[rule];

        if (currentRule.type == keyframeType) {
          console.log(currentRule);
        }
      }
    }
  }
}
like image 138
dan-lee Avatar answered Nov 15 '22 04:11

dan-lee