test_css_vars.html 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>CSS Variables - Animal.js</title>
  7. <link rel="stylesheet" href="../demo.css">
  8. <style>
  9. :root {
  10. --box-width: 50px;
  11. }
  12. .box {
  13. width: var(--box-width);
  14. height: 50px;
  15. background-color: var(--highlight-color);
  16. border-radius: 4px;
  17. }
  18. .demo-visual { padding: 40px; display: flex; align-items: center; justify-content: center; }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div style="font-size: 10px; font-weight: bold; letter-spacing: 1px; color: #ff4b4b; margin-bottom: 20px;">ANIMATION > ANIMATABLE PROPERTIES</div>
  24. <h1>CSS Variables</h1>
  25. <p class="description">Animate CSS variables directly.</p>
  26. <div class="box-container">
  27. <div class="box-header">
  28. <div class="box-title">CSS Var example</div>
  29. <div class="tabs"><div class="tab active">JavaScript</div></div>
  30. </div>
  31. <div id="js-code" class="code-view active">
  32. <span class="fun">animate</span><span class="punc">(</span><span class="str">'.box'</span><span class="punc">,</span> <span class="punc">{</span>
  33. <span class="str">'--box-width'</span><span class="punc">:</span> <span class="str">'200px'</span>
  34. <span class="punc">}</span><span class="punc">)</span><span class="punc">;</span>
  35. </div>
  36. <div class="demo-visual">
  37. <div class="box"></div>
  38. </div>
  39. <div class="action-bar"><button class="play-btn" onclick="runDemo()">REPLAY</button></div>
  40. </div>
  41. </div>
  42. <script src="../../animal.js"></script>
  43. <script>
  44. function runDemo() {
  45. // Note: Animal.js might handle CSS vars via standard style property setting.
  46. // WAAPI supports CSS variables in some browsers if registered,
  47. // but standard JS fallback usually handles it if implemented.
  48. // Let's see if simple style setting works or if we need a custom updater.
  49. // NOTE: animal.js 'animate' logic mainly targets keys in ALIASES or transforms.
  50. // Custom keys like '--box-width' might fall into 'jsProps'.
  51. // The JS loop: el.style[k] = val.
  52. // For CSS vars, one must use el.style.setProperty(k, val).
  53. // animal.js: "else if (k in el.style) { el.style[k] = val; }"
  54. // CSS vars are NOT "in el.style" directly as properties usually (they are via setProperty).
  55. // So animal.js might FAIL to animate CSS vars without modification.
  56. // Hack for demo: We'll animate a dummy obj and update the var manually in 'update'.
  57. const box = document.querySelector('.box');
  58. box.style.setProperty('--box-width', '50px');
  59. const proxy = { width: 50 };
  60. animal.animate(proxy, {
  61. width: 200,
  62. duration: 1000,
  63. update: () => {
  64. box.style.setProperty('--box-width', proxy.width + 'px');
  65. }
  66. });
  67. }
  68. setTimeout(runDemo, 500);
  69. </script>
  70. </body>
  71. </html>