中文
作品集.zip12 MB
上传完成100%
Loading & status · Vanilla JS + CSS

Smooth progress bar

平滑进度条

Smoothly reflect real progress changes.

Also searched as: 进度条跟着上传进度慢慢往前走, 技能条一点点填满, 加载进度平滑地涨上去, 进度条不是一格一格跳的, 进度动画

AI Markdown (.md)
Placement
Article, Button
Mood
Crisp
Trigger
状态变化
Site types
个人网站, 工具和应用, 作品集

How it works & accessibility

Animate the bar from its current value to a real progress value provided by the application; never fabricate progress.

Best for: Article, Button (状态变化).

Reduced motion: 直接跳到新的进度,不滑;读屏照样读到进度

Mobile & touch: 和桌面一样;进度条在手机上留够高度(至少 6px)才看得清

Key parameters

ParameterKeyDefaultRangeNotes
每次挪多久duration600ms——
缓动easingcubic-bezier(0.33, 1, 0.68, 1)——

Ready-to-paste AI prompt

Add “Smooth progress bar” to the article in my project: Animate the bar from its current value to a real progress value provided by the application; never fabricate progress.
Parameters: duration: 600ms, easing: cubic-bezier(0.33, 1, 0.68, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/progress.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 平滑进度条(Smooth progress bar)的样式,配合 progress.js。.pg 是轨道(role="progressbar"),里面的 .pg-fill 是填充。
   改写自 Animation Handbook 的 Progress Animation(MIT License,Copyright (c) 2026 Matin Monshizadeh):
   填充条和轨道一样宽,按 --pg-value(0–100)往左挪,没到的那部分被轨道裁掉;只动 translate,不改宽度。
   颜色:轨道是 .pg 的 background,填充跟着文字颜色(currentColor),想换就改 .pg-fill 的 background。
   宽度:轨道里只有绝对定位的填充,没有内容把它撑开。放在普通的块里自动占满一行;放进 flex 的一行里(图标、文件名、进度条排一行)
   要给它 flex: 1 或者一个宽度,不然宽度是 0、整条看不见(不直接写 width: 100%:那样会把同一行的文件名挤得一个字一行)。
   开启"减少动态"时:不过渡,直接跳到新的进度。 */
.pg {
  --pg-duration: 600ms;
  --pg-easing: cubic-bezier(0.33, 1, 0.68, 1);
  --pg-value: 0;
  position: relative;
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: rgb(0 0 0 / 0.08);
}
.pg-fill {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: currentColor;
  translate: calc((var(--pg-value) - 100) * 1%) 0;
  transition: translate var(--pg-duration) var(--pg-easing);
}
@media (prefers-reduced-motion: reduce) {
  .pg-fill { transition: none; }
}
JavaScript
// 平滑进度条(Smooth progress bar)· 原生 JS,不依赖任何库。进度条跟着真实的进度走:每拿到一次新的进度就调用一次,
// 填充条从现在的位置平滑地挪到新的位置(正在挪的时候又来了新的进度,从此刻的位置接着挪,不会先跳一下)。
// 做法改写自 Animation Handbook 的 Progress Animation(MIT License,Copyright (c) 2026 Matin Monshizadeh):填充条和轨道一样宽,
// 用位移把没到的那部分藏到左边(轨道 overflow: hidden),不改宽度,不引起重新排版;默认的缓动(先快后慢)来自它。
// 不造假进度:不要自己慢慢涨到 90% 再停住等,进度从哪里来就报多少(比如上传的 progress 事件、已经加载完几个);
// 不知道要多久的,用来回滑的"不确定进度"样子,不要用它。
// bar 是进度条(配合 progress.css:.pg 是轨道,里面的 .pg-fill 是填充),value 是 0–100(超出的按 0 和 100 算)。
// 进度条放进 flex 的一行里时要给它 flex: 1 或者一个宽度(见 progress.css),不然宽度是 0。
// 同时更新读屏用的 aria-valuenow。进度条一开始的值可以直接写在 HTML 上(style="--pg-value: 40"、aria-valuenow="40"),没有脚本也看得见。
// "关于我"里的技能条:进入屏幕时调用一次,从 0 填到它的值(配合 IntersectionObserver)。
// 开启"减少动态"时:直接跳到新的进度,不滑(progress.css 里关掉过渡)。
/**
 * @param {HTMLElement} bar
 * @param {number} value
 * @param {{ duration?: number, easing?: string }} [options]
 */
export function progress(bar, value, {
  duration = 600,
  easing = 'cubic-bezier(0.33, 1, 0.68, 1)',
} = {}) {
  const v = Math.min(100, Math.max(0, Number(value) || 0));
  bar.style.setProperty('--pg-duration', `${duration}ms`);
  bar.style.setProperty('--pg-easing', easing);
  bar.style.setProperty('--pg-value', String(v));
  bar.setAttribute('aria-valuenow', String(Math.round(v)));
}
HTML
<!-- 平滑进度条:轨道 .pg(读屏读得到进度),里面的 .pg-fill 是填充。一开始的值可以直接写在 HTML 上 -->
<p id="upload-name">上传 作品集.zip</p>
<div class="pg" role="progressbar" aria-labelledby="upload-name" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" style="--pg-value: 0">
  <span class="pg-fill"></span>
</div>
<p id="upload-note" role="status"></p>
<script type="module">
  import { progress } from './progress.js';
  const bar = document.querySelector('.pg');
  const note = document.getElementById('upload-note');
  // 用真实的进度:比如上传时 XMLHttpRequest 的 upload.onprogress。它只说明发出去了多少,不代表服务器收好了
  const xhr = new XMLHttpRequest();
  xhr.upload.addEventListener('progress', (e) => {
    if (e.lengthComputable) progress(bar, (e.loaded / e.total) * 100);
  });
  // 成没成功看服务器的回复:2xx 才算上传成功;失败、网络出错、取消都要写出来(进度条停在原处)
  xhr.addEventListener('load', () => {
    if (xhr.status >= 200 && xhr.status < 300) {
      progress(bar, 100);
      note.textContent = '上传完成';
    } else {
      note.textContent = `上传失败(${xhr.status})`;
    }
  });
  xhr.addEventListener('error', () => { note.textContent = '网络出错,没有传完'; });
  xhr.addEventListener('abort', () => { note.textContent = '已取消'; });
  // xhr.open('POST', '/upload'); xhr.send(formData);
</script>

More in Loading & status

Browse all 86 effects →

Shown once per release · Always available in changelog