萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 實現進度條組件:從基礎條塊到動畫、無障礙與框架封裝

核心 HTML 結構(語義化 + 無障礙)

<div class="progress" role="progressbar" aria-valuenow="35" aria-valuemin="0" aria-valuemax="100" aria-label="文件上傳進度">
  <div class="progress__bar" style="width: 35%;"></div>
  <span class="progress__label">35%</span>
</div>
  • role="progressbar":告知螢幕閱讀器此為進度條
  • aria-valuenow/min/max:當前/最小/最大值,必須同步更新
  • aria-label:無可見標籤時提供語義
  • 內部 .progress__bar 寬度由 CSS/JS 控制

CSS 樣式系統(設計 Token 化)

:root {
  --progress-height: 8px;
  --progress-radius: 4px;
  --progress-bg: #e0e0e0;
  --progress-fill: #0066ff;
  --progress-transition: width 0.3s ease-out, background-color 0.2s ease;
}

.progress {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 400px;
  font-size: 0.75rem;
  color: #666;
}

.progress__track {
  flex: 1;
  height: var(--progress-height);
  background: var(--progress-bg);
  border-radius: var(--progress-radius);
  overflow: hidden;          /* 裁切內部條塊圓角 */
  position: relative;
}

.progress__bar {
  height: 100%;
  background: var(--progress-fill);
  border-radius: inherit;
  transition: var(--progress-transition);
  /* 動畫關鍵:width 從 0% 過渡到目標值 */
  transform-origin: left center; /* 確保從左向右展開 */
}

/* 顏色變體 */
.progress--success .progress__bar { background: var(--color-success, #10b981); }
.progress--warning .progress__bar { background: var(--color-warning, #f59e0b); }
.progress--danger .progress__bar { background: var(--color-danger, #ef4444); }

/* 條紋動畫(進行中狀態) */
.progress--striped .progress__bar {
  background-image: linear-gradient(
    45deg,
    rgba(255,255,255,.15) 25%,
    transparent 25%,
    transparent 50%,
    rgba(255,255,255,.15) 50%,
    rgba(255,255,255,.15) 75%,
    transparent 75%,
    transparent
  );
  background-size: 1rem 1rem;
  animation: progress-stripe 1s linear infinite;
}
@keyframes progress-stripe {
  from { background-position: 0 0; }
  to { background-position: 1rem 0; }
}

/* 不確定進度(Indeterminate):來回滑動 */
.progress--indeterminate .progress__bar {
  width: 30% !important;
  animation: progress-indeterminate 1.5s ease-in-out infinite;
}
@keyframes progress-indeterminate {
  0% { transform: translateX(0); }
  50% { transform: translateX(calc(100% - 30%)); }
  100% { transform: translateX(0); }
}

JS 控制邏輯(原生 + 框架無關)

class ProgressBar {
  constructor(element) {
    this.el = element;
    this.track = element.querySelector('.progress__track');
    this.bar = element.querySelector('.progress__bar');
    this.label = element.querySelector('.progress__label');
    this.value = parseFloat(element.getAttribute('aria-valuenow')) || 0;
  }

  setValue(value, animate = true) {
    const clamped = Math.max(0, Math.min(100, value));
    this.value = clamped;
    this.el.setAttribute('aria-valuenow', clamped);
    
    if (animate) {
      this.bar.style.width = `${clamped}%`;
    } else {
      // 關閉過渡瞬間跳轉
      this.bar.style.transition = 'none';
      this.bar.style.width = `${clamped}%`;
      // 強制重繪後恢復過渡
      this.bar.offsetHeight; // reflow
      this.bar.style.transition = '';
    }
    
    if (this.label) this.label.textContent = `${clamped}%`;
    
    // 狀態類切換
    this.el.classList.toggle('progress--complete', clamped === 100);
  }

  setIndeterminate(isIndeterminate) {
    this.el.classList.toggle('progress--indeterminate', isIndeterminate);
    if (isIndeterminate) {
      this.el.removeAttribute('aria-valuenow');
    } else {
      this.el.setAttribute('aria-valuenow', this.value);
    }
  }
}

// 使用
const progress = new ProgressBar(document.querySelector('.progress'));
progress.setValue(35);
// 模擬上傳
let v = 0;
const timer = setInterval(() => {
  v += Math.random() * 10;
  if (v >= 100) { v = 100; clearInterval(timer); }
  progress.setValue(v);
}, 300);

原文示例現代化重構(語義化 + Token + 動畫)

<style>
  :root {
    --progress-track-bg: #f8f8f8;
    --progress-border: #e7e7e7;
    --progress-radius: 4px;
    --progress-height: 22px; /* 原文 18px + padding 2px*2 */
  }
  .progress-modern {
    border: 1px solid var(--progress-border);
    padding: 10px;
    width: 250px;
    background: var(--progress-track-bg);
    border-radius: var(--progress-radius);
    font-family: Arial, sans-serif;
  }
  .progress-modern h2 {
    color: #666; font: 700 18px Arial; margin: 0 0 10px;
  }
  .progress-modern .progress__track {
    height: var(--progress-height);
    background: #f0efef;
    border: 1px solid #ccc;
    border-radius: var(--progress-radius);
    overflow: hidden;
    position: relative;
  }
  .progress-modern .progress__bar {
    height: 100%;
    line-height: 18px;
    color: #fff;
    font: 700 13px Arial;
    text-align: left;
    padding-left: 8px;
    border-radius: inherit;
    transition: width 0.3s ease-out;
  }
  /* 原文顏色類映射 */
  .progress-modern .bar-orange { background: #ff6600; }
  .progress-modern .bar-green  { background: #66cc33; }
  .progress-modern .bar-blue   { background: #3399cc; }
  .progress-modern .bar-red    { background: #dc3545; }
  .progress-modern .bar-black  { background: #555; }
</style>

<div class="progress-modern" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" aria-label="示例進度">
  <h2>BarGraphs Example</h2>
  <div class="progress__track"><div class="progress__bar bar-orange" style="width:35%">orange:35%</div></div>
  <div class="progress__track"><div class="progress__bar bar-green" style="width:90%">green:90%</div></div>
  <div class="progress__track"><div class="progress__bar bar-blue" style="width:75%">blue:75%</div></div>
  <div class="progress__track"><div class="progress__bar bar-red" style="width:85%">red:85%</div></div>
  <div class="progress__track"><div class="progress__bar bar-black" style="width:100%">black:100%</div></div>
</div>

框架封裝建議

  • Vue/React/Svelte:封裝為 <ProgressBar :value="35" :max="100" variant="primary" striped indeterminate />
  • Props:value (Number)、max (Number, default 100)、variant (primary/success/warning/danger)、size (sm/md/lg)、showLabel (Boolean)、striped (Boolean)、indeterminate (Boolean)
  • 暴露 setValue 方法或 v-model 雙向綁定
  • 內部使用 requestAnimationFrame 批次更新,避免佈局抖動

無障礙測試清單

  1. NVDA/VoiceOver 讀出:「進度條,35%」
  2. 鍵盤 Tab 可聚焦進度條容器(可選,通常進度條不可聚焦,但需有標籤關聯)
  3. 動態更新時 aria-valuenow 同步,螢幕閱讀器自動播報(需 aria-live="polite" 或 role="status" 容器)
  4. 高對比度模式下條塊可見(邊框/背景色系統色覆蓋)
  5. 縮放 200% 文字不截斷、佈局不崩潰
「進度條看似簡單,實則是『狀態同步、動畫性能、無障礙語義、設計系統一致性』四重考驗的縮影。」
copyright © 萬盛學電腦網 all rights reserved