Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the webkit-keyframes from/to parameter with JavaScript

Tags:

Is there any way to set the from or to of a webkit-keyframe with JavaScript?

like image 649
Ricardo Avatar asked Jul 25 '10 11:07

Ricardo


2 Answers

A solution of sorts:

var cssAnimation = document.createElement('style'); cssAnimation.type = 'text/css'; var rules = document.createTextNode('@-webkit-keyframes slider {'+ 'from { left:100px; }'+ '80% { left:150px; }'+ '90% { left:160px; }'+ 'to { left:150px; }'+ '}'); cssAnimation.appendChild(rules); document.getElementsByTagName("head")[0].appendChild(cssAnimation); 

Just adds a style definition to the header. Would be much cleaner/better to define it though the DOM if possible.

Edit: Error in Chrome with old method

like image 119
Adam Heath Avatar answered Sep 19 '22 19:09

Adam Heath


You can use the CSS DOM interface. For instance:

<html>     <body>         <style>         @keyframes fadeout {             from { opacity:1; }             to { opacity:0; }         }         </style>         <script text="javascript">             var stylesheet = document.styleSheets[0];             var fadeOutRule = stylesheet.cssRules[0];             alert( fadeOutRule.name ); // alerts "fadeout"              var fadeOutRule_From = fadeOutRule.cssRules[0];             var fadeOutRule_To = fadeOutRule.cssRules[1];             alert( fadeOutRule_From.keyText ); // alerts "0%" ( and not "from" as you might expect)             alert( fadeOutRule_To.keyText ); // alerts "100%"              var fadeOutRule_To_Style = fadeOutRule_To.style;              alert( fadeOutRule_To_Style.cssText ); // alerts "opacity:0;"              fadeOutRule_To_Style.setProperty('color', 'red'); // add the style color:red             fadeOutRule_To_Style.removeProperty('opacity'); // remove the style opacity              alert( fadeOutRule_To_Style.cssText ); // alerts "color:red;"         </script>     </body> </html> 
like image 36
Matthew Wilcoxson Avatar answered Sep 21 '22 19:09

Matthew Wilcoxson