示例工程 36-scroll-animations

36-scroll-animations/app/index.html

html · 5.1 KB

运行:滚动驱动动画

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>滚动驱动动画</title>
    <link rel="stylesheet" href="tokens.css" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="reading-bar" aria-hidden="true"></div>

    <div class="app">
      <header class="site-header">
        <span class="brand">SCROLL LAB</span>
        <nav aria-label="页面导航">
          <a href="#reveal">揭示</a>
          <a href="#inline">容器进度</a>
          <a href="#notes">说明</a>
        </nav>
      </header>

      <main class="page">
        <section class="hero">
          <p class="eyebrow">LESSON / 36</p>
          <h1>让滚动位置驱动动画</h1>
          <p class="lead">
            滚动时间线把「滚了多少」变成进度;不需要 JS 监听 scroll,也不掉帧。
          </p>
          <p class="hint">顶部细条是整页阅读进度。</p>
        </section>

        <section class="panel" id="reveal">
          <h2>1. 进入视口时揭示</h2>
          <p>滚动下面的面板:卡片进入视野时淡入上移,离开时反向。</p>

          <div class="reveal-panel">
            <article class="reveal-wrap">
              <div class="reveal-card">
                <span class="card-tag">01</span>
                <h3>view-timeline</h3>
                <p>元素进入滚动容器时开始。</p>
              </div>
            </article>
            <article class="reveal-wrap">
              <div class="reveal-card">
                <span class="card-tag">02</span>
                <h3>animation-range</h3>
                <p>entry 0% 到 entry 60% 之间完成。</p>
              </div>
            </article>
            <article class="reveal-wrap">
              <div class="reveal-card">
                <span class="card-tag">03</span>
                <h3>fill both</h3>
                <p>范围之前保持起点,之后保持终点。</p>
              </div>
            </article>
            <article class="reveal-wrap">
              <div class="reveal-card">
                <span class="card-tag">04</span>
                <h3>无 JS</h3>
                <p>滚动事件由浏览器合成,不阻塞主线程。</p>
              </div>
            </article>
            <article class="reveal-wrap">
              <div class="reveal-card">
                <span class="card-tag">05</span>
                <h3>可降级</h3>
                <p>不支持时直接显示,不影响阅读。</p>
              </div>
            </article>
            <article class="reveal-wrap">
              <div class="reveal-card">
                <span class="card-tag">06</span>
                <h3>尊重偏好</h3>
                <p>reduce 下取消位移与淡入。</p>
              </div>
            </article>
          </div>
        </section>

        <section class="panel" id="inline">
          <h2>2. 容器内进度</h2>
          <p>这个面板自己滚动;顶部进度条绑定它的滚动进度。</p>

          <div class="inline-panel">
            <div class="inline-bar" aria-hidden="true"></div>
            <div class="inline-content">
              <p>向下滚动这个面板,顶部条会同步增长。</p>
              <p>进度条使用 <code>scroll-timeline</code> 与 <code>animation-timeline</code>。</p>
              <p>滚动容器必须能滚动,否则时间线没有进度。</p>
              <p>这是「阅读进度」「横向画廊」的通用做法。</p>
              <p>继续滚动。</p>
              <p>接近底部。</p>
              <p>到底了。</p>
            </div>
          </div>
        </section>

        <section class="panel" id="notes">
          <h2>3. 属性与降级</h2>
          <div class="table-wrap">
            <table>
              <thead>
                <tr><th scope="col">属性</th><th scope="col">作用</th></tr>
              </thead>
              <tbody>
                <tr><td><code>scroll-timeline-name</code></td><td>给滚动容器命名</td></tr>
                <tr><td><code>view-timeline-name</code></td><td>给元素的视口进度命名</td></tr>
                <tr><td><code>animation-timeline</code></td><td>指定动画使用哪条时间线</td></tr>
                <tr><td><code>animation-range</code></td><td>限定生效的区间</td></tr>
              </tbody>
            </table>
          </div>

          <ul class="notes">
            <li>支持较新;不支持时动画按普通时间线播放一次,或直接显示。</li>
            <li>滚动动画不能阻塞内容;静态信息必须完整。</li>
            <li>`prefers-reduced-motion` 下关闭位移与淡入。</li>
            <li>滚动容器要有明确高度与 `overflow`,否则没有进度。</li>
          </ul>
        </section>

        <div class="filler" aria-hidden="true"></div>
      </main>

      <footer class="site-footer">
        <p>示例数据,仅用于教程演示。</p>
        <p>SCROLL ANIMATIONS · 2026</p>
      </footer>
    </div>
  </body>
</html>