全部效果滚动与入场

阅读滚动进度

Scroll Progress

进度条随着滚动填充,反映已滚过的内容比例。

在演示区域内滚动

适用场景

用于长文章、课程或文档的阅读进度提示。 进度的计算范围建议用正文区域。评论区、页脚和推荐内容在阅读任务之外时,可以从总长度中去掉。

应用示例

菜谱文章顶部有一道细进度条。读者滚到最后一步时,进度条填满,下方还有评论也不会影响这个进度。

AI 提示词

为【目标位置】添加 Scroll Progress(阅读滚动进度)。用 scrollTop 除以 scrollHeight 与 clientHeight 的差值,限制到 0~1,再更新原生 progress。内容尺寸变化时重新计算。在减少动态效果模式下保持内容可读,并保留键盘操作。

示例代码

保存为 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;
    }

    .scroll-panel {
      position: relative;
      width: min(100%, 215px);
      height: 133px;
      overflow: auto;
      overscroll-behavior: contain;
      scrollbar-width: thin;
      scrollbar-color: #9881cb #2e2540;
      border: 1px solid #806ca8;
      border-radius: 9px;
      background: #2d2342;
      color: #f5efff;
      font: 650 12px system-ui, sans-serif;
    }
    .scroll-panel p { margin: 0; }
    .scroll-panel hr { border: 0; margin: 6px 0; height: 5px; border-radius: 9px; background: #ae96dc4a; }
    .scroll-panel hr:nth-of-type(2n) { width: 72%; }
    .scroll-note { color: #ddcff1; text-align: center; font-size: 10px; }
    .scroll-panel:focus-visible { outline-offset: 2px; }
    @media (prefers-reduced-motion: reduce) {
      .scroll-panel, .scroll-panel *, .scroll-panel *::before, .scroll-panel *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }

    .reading-section { min-height: 110px; padding: 8px 12px; }
    .reading-section h3 { color: #f3eaff; margin: 0 0 8px; font-size: 14px; font-weight: 720; }

    .reading-top { position: sticky; z-index: 1; top: 0; display: flex; align-items: center; gap: 8px; background: #423259; padding: 8px; }
    .reading-top progress { width: 100%; height: 9px; accent-color: #d1aaff; }
    .reading-top span { white-space: nowrap; }
  </style>
</head>
<body>
  <main class="demo">
    <div class="scroll-panel" role="region" tabindex="0" aria-label="阅读进度演示">
    <header class="reading-top">
      <span>阅读进度</span>
      <progress max="100" value="0" aria-label="阅读进度"></progress>
    </header>
    <section class="reading-section"><h3>色彩</h3><hr><hr><hr><hr><hr><hr></section>
    <section class="reading-section"><h3>字体</h3><hr><hr><hr><hr><hr><hr></section>
    <section class="reading-section"><h3>布局</h3><hr><hr><hr><hr><hr><hr></section>
    </div>
  </main>
  <script>
    const panel = document.querySelector('.scroll-panel');
    const progress = panel.querySelector('progress');
    function update() {
      const max = panel.scrollHeight - panel.clientHeight;
      progress.value = max ? Math.round(panel.scrollTop / max * 100) : 0;
    }
    panel.addEventListener('scroll', update, { passive: true });
    new ResizeObserver(update).observe(panel);
    update();
  </script>
</body>
</html>

UI Skills 原创示例