Guest User

Untitled

a guest
May 20th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. // Creating CSS Animations dynamically
  2. // - jamesu
  3.  
  4. //
  5. // Assuming:
  6. // - styleSheets[0] is the main stylesheet
  7. // - frames[] is a list of translation,rotation,scale keyframe values
  8. // Note:
  9. // If you modify an existing animation, the old keyframes
  10. // will continue being used until you re-assign the animation.
  11. //
  12.  
  13. function buildAnim(name, frames) {
  14. var frames = "";
  15. var len=frames.length;
  16. for (var i=0; i<len; i++) {
  17. var kf = frames[i];
  18. var dt = (i/(len-1)) * 100;
  19. frames += dt + "% { -webkit-transform: translate(" + kf[0] + "px," + kf[1] + "px) rotate(" + kf[2] + "deg) scale(" + kf[3] + "," + kf[4] + "); } ";
  20. }
  21. var idx = document.styleSheets[0].cssRules.length;
  22. document.styleSheets[0].insertRule("@-webkit-keyframes " + name + " { " + frames + "}", idx);
  23. }
  24.  
  25. // e.g.
  26. buildAnim('rotate', [
  27. [0,0,0,1.0,1.0],
  28. [0,0,90,1.0,1.0],
  29. [0,0,180,1.0,1.0],
  30. [0,0,270,1.0,1.0],
  31. [0,0,360,1.0,1.0]
  32. ]);
  33.  
  34. // Get spinner and animate it!
  35. var spinner = document.getElementById('spinner');
  36. spinner.style.webkitAnimationIterationCount = 'infinite';
  37. spinner.style.webkitAnimationTimingFunction = 'linear';
  38. spinner.style.webkitAnimationDuration = '2.0s';
  39. spinner.style.webkitAnimation = 'rotate';
Add Comment
Please, Sign In to add comment