萬盛學電腦網

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

CSS 純樣式實現進度條/條形圖表:語義化 HTML、無障礙、動畫、響應式完整方案

語義化 HTML 結構

<!-- 單個進度條 -->
<div class="progress" role="progressbar" aria-valuenow="64" aria-valuemin="0" aria-valuemax="100" aria-label="項目完成度">
  <div class="progress__bar" style="--value: 64%"></div>
</div>

<!-- 帶標籤進度條 -->
<div class="progress progress--labeled" role="progressbar" aria-valuenow="64" aria-valuemin="0" aria-valuemax="100">
  <div class="progress__label">CSS 技能掌握度</div>
  <div class="progress__track">
    <div class="progress__bar" style="--value: 64%"></div>
  </div>
  <output class="progress__value" for="progress-1">64%</output>
</div>

<!-- 條形圖表(多數據系列) -->
<figure class="chart-bar" aria-labelledby="chart-title">
  <figcaption id="chart-title">2024 Q1-Q4 季度營收</figcaption>
  <div class="chart-bar__axes">
    <div class="chart-bar__y-axis">
      <span>8000萬</span><span>6000萬</span><span>4000萬</span><span>2000萬</span><span>0</span>
    </div>
    <div class="chart-bar__bars">
      <div class="chart-bar__bar" style="--height: 65%; --color: #3b82f6"><span class="chart-bar__value">5200萬</span></div>
      <div class="chart-bar__bar" style="--height: 80%; --color: #22c55e"><span class="chart-bar__value">6400萬</span></div>
      <div class="chart-bar__bar" style="--height: 45%; --color: #f59e0b"><span class="chart-bar__value">3600萬</span></div>
      <div class="chart-bar__bar" style="--height: 90%; --color: #ef4444"><span class="chart-bar__value">7200萬</span></div>
    </div>
    <div class="chart-bar__x-axis">
      <span>Q1</span><span>Q2</span><span>Q3</span><span>Q4</span>
    </div>
  </div>
</figure>

核心 CSS — CSS 變量驅動、無 JS 計算

:root {
  --progress-track-h: 8px;
  --progress-radius: 9999px;
  --progress-bg: #e5e7eb;
  --progress-transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 進度條基礎 */
.progress {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  width: 100%;
  max-width: 400px;
}
.progress__track {
  width: 100%;
  height: var(--progress-track-h);
  background: var(--progress-bg);
  border-radius: var(--progress-radius);
  overflow: hidden;
  position: relative;
}
.progress__bar {
  height: 100%;
  width: 0; /* 初始 0,動畫到目標 */
  border-radius: inherit;
  background: var(--progress-color, #3b82f6);
  transition: var(--progress-transition);
  /* 關鍵:用 CSS 讀取 --value 變量 */
  width: var(--value, 0%);
}

/* 條形圖表 */
.chart-bar {
  --bar-gap: 12px;
  --bar-min-w: 40px;
  width: 100%;
  max-width: 600px;
}
.chart-bar__axes { display: grid; grid-template-columns: auto 1fr auto; gap: 1rem; align-items: end; }
.chart-bar__y-axis { display: flex; flex-direction: column; justify-content: space-between; height: 200px; font-size: 0.75rem; color: #666; }
.chart-bar__bars { display: flex; align-items: flex-end; gap: var(--bar-gap); height: 200px; }
.chart-bar__bar {
  flex: 1 1 var(--bar-min-w);
  max-width: 80px;
  height: var(--height, 0%);
  background: var(--color, #3b82f6);
  border-radius: 4px 4px 0 0;
  position: relative;
  transition: height 0.8s cubic-bezier(0.4, 0, 0.2, 1);
}
.chart-bar__value {
  position: absolute; top: -1.5rem; left: 50%; transform: translateX(-50%);
  font-size: 0.7rem; white-space: nowrap; color: #333;
}
.chart-bar__x-axis { display: flex; justify-content: space-between; padding: 0 calc(var(--bar-gap)/2); font-size: 0.875rem; color: #666; }

動畫觸發策略

1. CSS @property 註冊(平滑動畫數值插值)

@property --value {
  syntax: "";
  inherits: false;
  initial-value: 0%;
}
@keyframes progress-in { from { width: 0%; } to { width: var(--value); } }
.progress__bar { animation: progress-in 0.6s forwards ease-out; }

2. IntersectionObserver 進視口觸發(推薦)

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const bar = entry.target.querySelector('.progress__bar');
      if (bar) bar.style.width = bar.style.getPropertyValue('--value');
      observer.unobserve(entry.target);
    }
  });
}, { threshold: 0.5 });
document.querySelectorAll('.progress').forEach(el => observer.observe(el));

3. 純 CSS :target / :hover 觸發(無 JS 降級)

/* 點擊按鈕觸發 */
#show-progress:target .progress__bar { width: var(--value); }

/* 懸停卡片顯示進度 */
.card:hover .progress__bar { width: var(--value); }

無障礙 (a11y) 完整清單

  • ✅ `role="progressbar"` + `aria-valuenow/min/max`
  • ✅ `aria-label` 或 `aria-labelledby` 提供可訪問名稱
  • ✅ `` 元素關聯 `for` 顯示當前值
  • ✅ 顏色對比度 ≥ 3:1(非文字) / 4.5:1(文字)
  • ✅ `prefers-reduced-motion: reduce` 禁用動畫
  • ✅ 鍵盤可聚焦(進度條本身不可聚焦,但信息可讀)

現代替代:原生 <progress> <meter> 元素

<progress value="64" max="100">64%</progress>
<meter value="64" min="0" max="100" low="30" high="80" optimum="90">64%</meter>

/* 樣式化(跨瀏覽器極其痛苦,建議僅簡單場景用) */
progress { width: 100%; height: 8px; appearance: none; }
progress::-webkit-progress-bar { background: #e5e7eb; border-radius: 9999px; }
progress::-webkit-progress-value { background: #3b82f6; border-radius: 9999px; }
progress::-moz-progress-bar { background: #3b82f6; border-radius: 9999px; }

純 CSS 進度條/條形圖 = 語義化 HTML + CSS 變量驅動寬高 + IntersectionObserver 觸發動畫 + 完整 ARIA。複雜圖表建議引入 Chart.js/ECharts/Recharts,但簡單場景原生方案零依賴、極致性能。

copyright © 萬盛學電腦網 all rights reserved