| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Controls (chain / seek / scroll) - Animal.js</title>
- <link rel="stylesheet" href="../demo.css">
- <style>
- .demo-visual {
- gap: 18px;
- }
- .row {
- width: 100%;
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 14px;
- }
- .box {
- width: 48px;
- height: 48px;
- border-radius: 10px;
- background: var(--highlight-color);
- transform: translateX(0px);
- }
- .bar-wrap {
- width: 100%;
- background: rgba(255,255,255,0.05);
- border: 1px solid rgba(255,255,255,0.08);
- border-radius: 999px;
- overflow: hidden;
- height: 10px;
- }
- .bar {
- height: 100%;
- width: 0%;
- background: var(--orange);
- }
- .hint {
- font-size: 12px;
- color: #888;
- }
- .spacer {
- height: 80vh; /* enough to scroll inside the iframe */
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="page-top">
- <div class="crumb">ANIMATION > EXAMPLES</div>
- <div class="since">CONTROLS</div>
- </div>
- <h1>Controls</h1>
- <p class="description">
- <code class="inline">animate()</code> returns a <strong>thenable controls object</strong>.
- You can <code class="inline">play/pause</code>, <code class="inline">seek()</code> (0..1),
- and wire it to <code class="inline">scroll()</code>.
- </p>
- <div class="box-container">
- <div class="box-header">
- <div class="box-title">Controls example</div>
- <div class="tabs">
- <div class="tab active" onclick="switchTab('js')">JavaScript</div>
- <div class="tab" onclick="switchTab('html')">HTML</div>
- <div class="tab" onclick="switchTab('css')">CSS</div>
- </div>
- </div>
- <pre id="js-code" class="code-view active">const ctl = xjs('.ex-box').animate({
- x: 220,
- rotate: 180,
- opacity: 0.6,
- duration: 800,
- autoplay: false
- });
- // chainable controls
- ctl.seek(0.25).play();
- // still awaitable / thenable
- ctl.then(() => console.log('done'));
- // scroll-linked (works with WAAPI + JS fallback)
- const progress = xjs('.ex-bar').animate({
- width: ['0%', '100%'],
- autoplay: false,
- duration: 1000
- });
- xjs.scroll(progress);</pre>
- <pre id="html-code" class="html-view"><div class="ex-box"></div>
- <div class="bar-wrap">
- <div class="ex-bar"></div>
- </div></pre>
- <pre id="css-code" class="css-view">.demo-visual { gap: 18px; }
- .row {
- width: 100%;
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 14px;
- }
- .box {
- width: 48px;
- height: 48px;
- border-radius: 10px;
- background: var(--highlight-color);
- transform: translateX(0px);
- }
- .bar-wrap {
- width: 100%;
- background: rgba(255,255,255,0.05);
- border: 1px solid rgba(255,255,255,0.08);
- border-radius: 999px;
- overflow: hidden;
- height: 10px;
- }
- .bar { height: 100%; width: 0%; background: var(--orange); }
- .hint { font-size: 12px; color: #888; }
- .spacer { height: 80vh; }</pre>
- <div class="feature-desc">
- <strong>功能说明:</strong>演示 controls 对象的 <code class="inline">play/pause/seek</code> 与滚动联动:点击 REPLAY 后,在面板内滚动会驱动进度条动画。
- </div>
- <div class="demo-visual">
- <div class="row">
- <div class="box ex-box" aria-label="demo box"></div>
- <div class="hint">Click REPLAY, then scroll inside this panel.</div>
- </div>
- <div class="bar-wrap">
- <div class="bar ex-bar" aria-label="progress bar"></div>
- </div>
- <div class="spacer"></div>
- <div class="hint">Scroll target area (spacer)</div>
- </div>
- <div class="action-bar">
- <button class="play-btn" onclick="runDemo()">REPLAY</button>
- <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
- <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
- </div>
- </div>
- </div>
- <script src="../highlight_css.js"></script>
- <script src="../../xjs.js"></script>
- <script>
- function switchTab(tab) {
- document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
- document.querySelectorAll('.code-view, .html-view, .css-view').forEach(v => v.classList.remove('active'));
- if (tab === 'js') {
- document.querySelector('.tabs .tab:nth-child(1)')?.classList.add('active');
- document.getElementById('js-code').classList.add('active');
- } else if (tab === 'html') {
- document.querySelector('.tabs .tab:nth-child(2)')?.classList.add('active');
- document.getElementById('html-code').classList.add('active');
- } else {
- document.querySelector('.tabs .tab:nth-child(3)')?.classList.add('active');
- document.getElementById('css-code')?.classList.add('active');
- }
- }
- let cleanup = null;
- let __demoControls = [];
- function runDemo() {
- // reset
- const box = document.querySelector('.ex-box');
- const bar = document.querySelector('.ex-bar');
- box.style.transform = 'none';
- box.style.opacity = '1';
- bar.style.width = '0%';
- // Remove previous scroll listener if any
- if (cleanup) cleanup();
- cleanup = null;
- __demoControls = [];
- const ctl = xjs('.ex-box').animate({
- x: 220,
- rotate: 180,
- opacity: 0.6,
- duration: 800,
- autoplay: false
- });
- __demoControls.push(ctl);
- ctl.seek(0.25).play();
- const progress = xjs('.ex-bar').animate({
- width: ['0%', '100%'],
- autoplay: false,
- duration: 1000
- });
- __demoControls.push(progress);
- cleanup = xjs.scroll(progress);
- }
- function pauseDemo() {
- __demoControls.forEach(c => { try { c?.pause?.(); } catch {} });
- }
- function resumeDemo() {
- __demoControls.forEach(c => {
- try {
- if (typeof c?.resume === 'function') c.resume();
- else if (typeof c?.play === 'function') c.play();
- } catch {}
- });
- }
- setTimeout(runDemo, 300);
- </script>
- </body>
- </html>
|