controls.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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>Controls (chain / seek / scroll) - Animal.js</title>
  7. <link rel="stylesheet" href="../demo.css">
  8. <style>
  9. .demo-visual {
  10. gap: 18px;
  11. }
  12. .row {
  13. width: 100%;
  14. display: flex;
  15. align-items: center;
  16. justify-content: space-between;
  17. gap: 14px;
  18. }
  19. .box {
  20. width: 48px;
  21. height: 48px;
  22. border-radius: 10px;
  23. background: var(--highlight-color);
  24. transform: translateX(0px);
  25. }
  26. .bar-wrap {
  27. width: 100%;
  28. background: rgba(255,255,255,0.05);
  29. border: 1px solid rgba(255,255,255,0.08);
  30. border-radius: 999px;
  31. overflow: hidden;
  32. height: 10px;
  33. }
  34. .bar {
  35. height: 100%;
  36. width: 0%;
  37. background: var(--orange);
  38. }
  39. .hint {
  40. font-size: 12px;
  41. color: #888;
  42. }
  43. .spacer {
  44. height: 80vh; /* enough to scroll inside the iframe */
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="container">
  50. <div class="page-top">
  51. <div class="crumb">ANIMATION &gt; EXAMPLES</div>
  52. <div class="since">CONTROLS</div>
  53. </div>
  54. <h1>Controls</h1>
  55. <p class="description">
  56. <code class="inline">animate()</code> returns a <strong>thenable controls object</strong>.
  57. You can <code class="inline">play/pause</code>, <code class="inline">seek()</code> (0..1),
  58. and wire it to <code class="inline">scroll()</code>.
  59. </p>
  60. <div class="box-container">
  61. <div class="box-header">
  62. <div class="box-title">Controls example</div>
  63. <div class="tabs">
  64. <div class="tab active" onclick="switchTab('js')">JavaScript</div>
  65. <div class="tab" onclick="switchTab('html')">HTML</div>
  66. <div class="tab" onclick="switchTab('css')">CSS</div>
  67. </div>
  68. </div>
  69. <pre id="js-code" class="code-view active">const ctl = xjs('.ex-box').animate({
  70. x: 220,
  71. rotate: 180,
  72. opacity: 0.6,
  73. duration: 800,
  74. autoplay: false
  75. });
  76. // chainable controls
  77. ctl.seek(0.25).play();
  78. // still awaitable / thenable
  79. ctl.then(() =&gt; console.log('done'));
  80. // scroll-linked (works with WAAPI + JS fallback)
  81. const progress = xjs('.ex-bar').animate({
  82. width: ['0%', '100%'],
  83. autoplay: false,
  84. duration: 1000
  85. });
  86. xjs.scroll(progress);</pre>
  87. <pre id="html-code" class="html-view">&lt;div class="ex-box"&gt;&lt;/div&gt;
  88. &lt;div class="bar-wrap"&gt;
  89. &lt;div class="ex-bar"&gt;&lt;/div&gt;
  90. &lt;/div&gt;</pre>
  91. <pre id="css-code" class="css-view">.demo-visual { gap: 18px; }
  92. .row {
  93. width: 100%;
  94. display: flex;
  95. align-items: center;
  96. justify-content: space-between;
  97. gap: 14px;
  98. }
  99. .box {
  100. width: 48px;
  101. height: 48px;
  102. border-radius: 10px;
  103. background: var(--highlight-color);
  104. transform: translateX(0px);
  105. }
  106. .bar-wrap {
  107. width: 100%;
  108. background: rgba(255,255,255,0.05);
  109. border: 1px solid rgba(255,255,255,0.08);
  110. border-radius: 999px;
  111. overflow: hidden;
  112. height: 10px;
  113. }
  114. .bar { height: 100%; width: 0%; background: var(--orange); }
  115. .hint { font-size: 12px; color: #888; }
  116. .spacer { height: 80vh; }</pre>
  117. <div class="feature-desc">
  118. <strong>功能说明:</strong>演示 controls 对象的 <code class="inline">play/pause/seek</code> 与滚动联动:点击 REPLAY 后,在面板内滚动会驱动进度条动画。
  119. </div>
  120. <div class="demo-visual">
  121. <div class="row">
  122. <div class="box ex-box" aria-label="demo box"></div>
  123. <div class="hint">Click REPLAY, then scroll inside this panel.</div>
  124. </div>
  125. <div class="bar-wrap">
  126. <div class="bar ex-bar" aria-label="progress bar"></div>
  127. </div>
  128. <div class="spacer"></div>
  129. <div class="hint">Scroll target area (spacer)</div>
  130. </div>
  131. <div class="action-bar">
  132. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  133. <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
  134. <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
  135. </div>
  136. </div>
  137. </div>
  138. <script src="../highlight_css.js"></script>
  139. <script src="../../xjs.js"></script>
  140. <script>
  141. function switchTab(tab) {
  142. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  143. document.querySelectorAll('.code-view, .html-view, .css-view').forEach(v => v.classList.remove('active'));
  144. if (tab === 'js') {
  145. document.querySelector('.tabs .tab:nth-child(1)')?.classList.add('active');
  146. document.getElementById('js-code').classList.add('active');
  147. } else if (tab === 'html') {
  148. document.querySelector('.tabs .tab:nth-child(2)')?.classList.add('active');
  149. document.getElementById('html-code').classList.add('active');
  150. } else {
  151. document.querySelector('.tabs .tab:nth-child(3)')?.classList.add('active');
  152. document.getElementById('css-code')?.classList.add('active');
  153. }
  154. }
  155. let cleanup = null;
  156. let __demoControls = [];
  157. function runDemo() {
  158. // reset
  159. const box = document.querySelector('.ex-box');
  160. const bar = document.querySelector('.ex-bar');
  161. box.style.transform = 'none';
  162. box.style.opacity = '1';
  163. bar.style.width = '0%';
  164. // Remove previous scroll listener if any
  165. if (cleanup) cleanup();
  166. cleanup = null;
  167. __demoControls = [];
  168. const ctl = xjs('.ex-box').animate({
  169. x: 220,
  170. rotate: 180,
  171. opacity: 0.6,
  172. duration: 800,
  173. autoplay: false
  174. });
  175. __demoControls.push(ctl);
  176. ctl.seek(0.25).play();
  177. const progress = xjs('.ex-bar').animate({
  178. width: ['0%', '100%'],
  179. autoplay: false,
  180. duration: 1000
  181. });
  182. __demoControls.push(progress);
  183. cleanup = xjs.scroll(progress);
  184. }
  185. function pauseDemo() {
  186. __demoControls.forEach(c => { try { c?.pause?.(); } catch {} });
  187. }
  188. function resumeDemo() {
  189. __demoControls.forEach(c => {
  190. try {
  191. if (typeof c?.resume === 'function') c.resume();
  192. else if (typeof c?.play === 'function') c.play();
  193. } catch {}
  194. });
  195. }
  196. setTimeout(runDemo, 300);
  197. </script>
  198. </body>
  199. </html>