全部效果滚动与入场

滚动逐帧播放

Scroll-Scrubbed Frame Sequence

滚动位置决定显示哪一帧。向下滚动会逐步拆开镜头组件,反向滚动会重新组装。

在预览里上下滚动,或聚焦后按方向键

适用场景

适合展示产品的装配结构,或让读者按自己的节奏查看分步插画。 提前选好读者需要停下来查看的关键画面,并备好足够的中间帧,方便上下滚动来回对照。

应用示例

机械表的产品页可以让机芯随滚动分解,依次露出发条、齿轮组和擒纵机构。顾客往回滚一点,就能再看一遍两部分是怎样接在一起的。

AI 提示词

为【目标区块】添加 Scroll-Scrubbed Frame Sequence。让展示区随滚动轨道吸顶,把滚动进度限制到 0 到 1 后换算成帧索引,仅在索引改变时重绘。反向滚动用同样的映射,显示当前帧和总帧数,减少动态效果时提供完整的静态帧。

示例代码

保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。

查看 HTML
<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>滚动逐帧播放</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .sequence-scroll {
      --viewport: clamp(126px, calc(100vh - 33px), 200px);
      width: min(100%, 430px);
      height: var(--viewport);
      overflow: hidden auto;
      overscroll-behavior: contain;
      scrollbar-width: thin;
      scrollbar-color: #9ea9ee #282745;
      border: 1px solid #b3b2ff55;
      border-radius: 10px;
      background: #10233a;
    }
    .sequence-track { height: 530px; }
    .sequence-stage {
      position: sticky; top: 0; height: calc(var(--viewport) - 2px);
      display: grid; grid-template-rows: minmax(0, 1fr) 27px;
    }
    .sequence-stage canvas { display: block; width: 100%; height: 100%; object-fit: contain; }
    .sequence-status {
      display: grid; grid-template-columns: auto minmax(0, 1fr) auto;
      align-items: center; gap: 7px; padding: 0 8px;
      color: #dce6ff; background: #172442; border-top: 1px solid #aabaff30;
      font: 620 10px/1.2 system-ui, sans-serif;
    }
    .sequence-status progress { width: 100%; height: 7px; accent-color: #98efd9; }
    .frame-index { white-space: nowrap; font-variant-numeric: tabular-nums; }
    @media (prefers-reduced-motion: reduce) {
      .sequence-scroll { overflow: hidden; }
      .sequence-track { height: calc(var(--viewport) - 2px); }
      .sequence-status { grid-template-columns: 1fr; }
      .sequence-status progress, .frame-index { display: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="sequence-scroll" role="region" tabindex="0" aria-label="镜头拆解滚动演示">
      <div class="sequence-track">
        <div class="sequence-stage">
          <canvas width="432" height="162" role="img" aria-label="镜头组件拆解图"></canvas>
          <div class="sequence-status">
            <span>镜头组件</span>
            <progress value="1" max="24" aria-label="当前播放帧"></progress>
            <span class="frame-index" aria-hidden="true">01 / 24</span>
          </div>
        </div>
      </div>
    </div>
  </main>
  <script>
    const scrollport = document.querySelector('.sequence-scroll');
    const canvas = scrollport.querySelector('canvas');
    const context = canvas.getContext('2d');
    const indicator = scrollport.querySelector('progress');
    const frameIndex = scrollport.querySelector('.frame-index');
    const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');
    const frameCount = indicator.max;
    const parts = [
      { position: 166, offset: -81, radius: 35, depth: 16, tone: '#9eebdf' },
      { position: 187, offset: -43, radius: 28, depth: 11, tone: '#a9cbff' },
      { position: 198, offset: -5, radius: 23, depth: 8, tone: '#ffbdca' },
      { position: 211, offset: 32, radius: 29, depth: 10, tone: '#aabaff' },
      { position: 226, offset: 58, radius: 36, depth: 35, tone: '#8498e8' },
    ];
    let previousFrame = -1;
    let animation = 0;

    function drawEllipse(x, y, horizontal, vertical, fill) {
      context.beginPath();
      context.ellipse(x, y, horizontal, vertical, 0, 0, Math.PI * 2);
      context.fillStyle = fill;
      context.fill();
    }

    function drawPart(part, travel) {
      const x = part.position + part.offset * travel;
      const y = 81;
      const halfWidth = part.radius / 3;
      const shading = context.createLinearGradient(0, y - part.radius, 0, y + part.radius);
      shading.addColorStop(0, part.tone);
      shading.addColorStop(.47, '#394b75');
      shading.addColorStop(1, '#121c34');
      drawEllipse(x + part.depth, y, halfWidth, part.radius, '#222f50');
      context.fillStyle = shading;
      context.fillRect(x, y - part.radius, part.depth, part.radius * 2);
      drawEllipse(x, y, halfWidth, part.radius, part.tone);
      drawEllipse(x - 1, y, halfWidth * .73, part.radius - 5, '#1b2448');
      drawEllipse(x - 2, y, halfWidth * .53, part.radius - 10, '#455486');
      drawEllipse(x - 3, y - part.radius * .21, 2.5, 10, '#d6fff2');
    }

    function drawFrame(index) {
      const { width, height } = canvas;
      const normalized = index / (frameCount - 1);
      const travel = normalized * normalized * (3 - 2 * normalized);
      const background = context.createRadialGradient(214, 80, 5, 214, 80, 230);
      background.addColorStop(0, '#2d435e');
      background.addColorStop(1, '#10233a');
      context.fillStyle = background;
      context.fillRect(0, 0, width, height);
      context.beginPath();
      for (let x = 24; x < width; x += 24) { context.moveTo(x, 12); context.lineTo(x, height - 12); }
      for (let y = 24; y < height; y += 24) { context.moveTo(12, y); context.lineTo(width - 12, y); }
      context.strokeStyle = '#b3d9ff19';
      context.stroke();
      context.beginPath();
      context.setLineDash([4, 5]);
      context.moveTo(43, 81);
      context.lineTo(385, 81);
      context.strokeStyle = '#b5efeb80';
      context.stroke();
      context.setLineDash([]);
      for (let index = parts.length - 1; index >= 0; index -= 1) drawPart(parts[index], travel);
    }

    function updateFrame() {
      animation = 0;
      const scrollable = scrollport.scrollHeight - scrollport.clientHeight;
      const progress = scrollable ? Math.min(1, Math.max(0, scrollport.scrollTop / scrollable)) : 0;
      const index = reducedMotion.matches ? frameCount - 1 : Math.round(progress * (frameCount - 1));
      if (index === previousFrame) return;
      previousFrame = index;
      drawFrame(index);
      indicator.value = index + 1;
      frameIndex.textContent = String(index + 1).padStart(2, '0') + ' / ' + frameCount;
    }

    function scheduleUpdate() {
      if (!animation) animation = requestAnimationFrame(updateFrame);
    }

    scrollport.addEventListener('scroll', scheduleUpdate, { passive: true });
    reducedMotion.addEventListener('change', () => {
      scrollport.tabIndex = reducedMotion.matches ? -1 : 0;
      scheduleUpdate();
    });
    new ResizeObserver(scheduleUpdate).observe(scrollport);
    scrollport.tabIndex = reducedMotion.matches ? -1 : 0;
    updateFrame();
  </script>
</body>
</html>

UI Skills 原创示例

相关参考