示例工程 35-keyframes

35-keyframes/app/index.html

html · 4.9 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>
    <input type="checkbox" id="pause" class="pause-input" />
    <div class="app">
      <header class="site-header">
        <span class="brand">ANIMATION LAB</span>
        <nav aria-label="页面导航">
          <a href="#basic">基础</a>
          <a href="#multi">多阶段</a>
          <a href="#direction">方向</a>
          <a href="#props">属性</a>
        </nav>
      </header>

      <main class="page">
        <p class="eyebrow">LESSON / 35</p>
        <h1>关键帧:定义一段时间里的变化</h1>
        <p class="lead">
          @keyframes 描述 0% 到 100% 的形态;animation 决定播放多久、几次、往哪边、结束后停在哪。
        </p>

        <section class="panel" id="basic">
          <h2>1. 四个基础动画</h2>
          <p>勾选「暂停」可以冻结当前帧,方便观察。</p>
          <label class="pause-btn" for="pause">暂停</label>

          <div class="basics">
            <div class="anim-card">
              <div class="anim pulse" aria-hidden="true"></div>
              <span>pulse</span>
            </div>
            <div class="anim-card">
              <div class="anim bounce" aria-hidden="true"></div>
              <span>bounce</span>
            </div>
            <div class="anim-card">
              <div class="anim spin" aria-hidden="true"></div>
              <span>spin</span>
            </div>
            <div class="anim-card">
              <div class="anim shake" aria-hidden="true"></div>
              <span>shake</span>
            </div>
          </div>
        </section>

        <section class="panel" id="multi">
          <h2>2. 多阶段关键帧</h2>
          <p>小球按 0/25/50/75/100% 走一个方框,同时改变颜色。</p>

          <div class="orbit-stage">
            <div class="orbit" aria-hidden="true"></div>
          </div>

          <pre><code>@keyframes orbit {
  0%   { transform: translate(0, 0); background: #2563eb; }
  25%  { transform: translate(160px, 0); background: #7c3aed; }
  50%  { transform: translate(160px, 120px); background: #db2777; }
  75%  { transform: translate(0, 120px); background: #f59e0b; }
  100% { transform: translate(0, 0); background: #2563eb; }
}</code></pre>
        </section>

        <section class="panel" id="direction">
          <h2>3. 方向与填充</h2>
          <p>同一个进度条:normal、reverse、alternate。</p>

          <div class="bars">
            <div class="bar-row">
              <code>normal</code>
              <div class="track"><span class="fill f-normal"></span></div>
            </div>
            <div class="bar-row">
              <code>reverse</code>
              <div class="track"><span class="fill f-reverse"></span></div>
            </div>
            <div class="bar-row">
              <code>alternate</code>
              <div class="track"><span class="fill f-alternate"></span></div>
            </div>
          </div>
        </section>

        <section class="panel" id="props">
          <h2>4. 动画属性</h2>
          <div class="table-wrap">
            <table>
              <thead>
                <tr><th scope="col">属性</th><th scope="col">作用</th><th scope="col">本页值</th></tr>
              </thead>
              <tbody>
                <tr><td><code>animation-name</code></td><td>关键帧名称</td><td><code>pulse</code></td></tr>
                <tr><td><code>animation-duration</code></td><td>一轮时长</td><td><code>1.6s</code></td></tr>
                <tr><td><code>animation-iteration-count</code></td><td>播放次数</td><td><code>infinite</code></td></tr>
                <tr><td><code>animation-direction</code></td><td>方向</td><td><code>alternate</code></td></tr>
                <tr><td><code>animation-fill-mode</code></td><td>前后状态</td><td><code>both</code></td></tr>
                <tr><td><code>animation-play-state</code></td><td>播放/暂停</td><td><code>running</code></td></tr>
              </tbody>
            </table>
          </div>
        </section>

        <section class="panel">
          <h2>5. 注意</h2>
          <ul class="notes">
            <li>关键帧里的属性要能插值;`display` 不行。</li>
            <li>无限动画只用于装饰;不要让用户等它结束。</li>
            <li>`prefers-reduced-motion: reduce` 下停止动画。</li>
            <li>动画不改变布局;需要占位时用静态元素。</li>
          </ul>
        </section>
      </main>

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