Search code examples
javascriptjquerycsskeyframe

How can I change keyframe state in Javascript


I want to change a keyframe state dynamiclly with JQuery.
Here's my keyframe:

@keyframes moveItUp {
    10% {
        top: 18%;
    }
    20% {
        top: 16%;
    }
    30% {
        top: 14%;
    }
    40% {
        top: 12%;
    }
    50% {
        top: 10%;
    }
    60% {
        top: 8%;
    }
    70% {
        top: 6%;
    }
    80% {
        top: 4%;
    }
    90% {
        top: 2%;
    }
    100% {
        top: -20px;
    }
}

For example, I want to switch from 20% to 40% ect.. dynamiclly with Javascript

How can I do that? Thanks


Solution

  • There's a great blog article on MSDN that explains exactly how to do this.

      var rule;    
      var ss = document.styleSheets;
    
      for (var i = 0; i < ss.length; ++i) {
    
          // loop through all the rules!
          for (var x = 0; x < ss[i].cssRules.length; ++x) {
    
              rule = ss[i].cssRules[x];
              if (rule.name == "moveItUp" && rule.type == CSSRule.KEYFRAMES_RULE) {
    
                  // do what you need to do here
              }
          }
      }