<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>進度條演示</title>
<style>
.progress-bar { width:300px; height:24px; border:1px solid #ccc; border-radius:4px; overflow:hidden; background:#f0f0f0; }
.progress-fill { height:100%; background:linear-gradient(90deg, #4caf50, #8bc34a); width:0%; transition:width 0.3s; display:flex; align-items:center; justify-content:flex-end; padding-right:8px; color:#fff; font-size:12px; white-space:nowrap; }
.controls { margin-top:10px; }
button { padding:6px 12px; margin-right:8px; cursor:pointer; }
</style></head><body>
<div class="progress-bar"><div id="fill" class="progress-fill">0%</div></div>
<div class="controls">
<button id="start">開始</button>
<button id="pause" disabled>暫停</button>
<button id="reset" disabled>重置</button>
</div>
<script>
const fill = document.getElementById('fill');
const btns = {start:document.getElementById('start'), pause:document.getElementById('pause'), reset:document.getElementById('reset')};
let timer = null, progress = 0;
function setProgress(p) {
progress = Math.max(0, Math.min(100, p));
fill.style.width = progress + '%';
fill.textContent = progress + '%';
btns.start.disabled = progress >= 100 || progress > 0;
btns.pause.disabled = progress === 0 || progress >= 100;
btns.reset.disabled = progress === 0;
}
btns.start.onclick = () => {
if (timer) return;
timer = setInterval(() => {
setProgress(progress + 10);
if (progress >= 100) { clearInterval(timer); timer = null; }
}, 500);
};
btns.pause.onclick = () => { clearInterval(timer); timer = null; };
btns.reset.onclick = () => { clearInterval(timer); timer = null; setProgress(0); };
</script>
</body></html>
async function uploadWithProgress(file, url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const formData = new FormData();
formData.append('file', file);
xhr.upload.onprogress = e => {
if (e.lengthComputable) {
const percent = Math.round((e.loaded / e.total) * 100);
setProgress(percent); // 使用方案一的 setProgress
}
};
xhr.onload = () => xhr.status === 200 ? resolve(xhr.response) : reject(new Error(xhr.statusText));
xhr.onerror = () => reject(new Error('網絡錯誤'));
xhr.open('POST', url);
xhr.send(formData);
});
}
async function downloadWithProgress(url) {
const response = await fetch(url);
const total = Number(response.headers.get('Content-Length')) || 0;
let loaded = 0;
const reader = response.body.getReader();
const chunks = [];
while (true) {
const {done, value} = await reader.read();
if (done) break;
chunks.push(value);
loaded += value.length;
if (total) setProgress(Math.round((loaded / total) * 100));
}
return new Blob(chunks);
}
<!-- 確定進度 -->
<progress id="prog" value="0" max="100">0%</progress>
<script>
document.getElementById('prog').value = 42; // 直接設置 0-100
</script>
<!-- 不確定進度(旋轉/脈動) -->
<progress id="indeterminate"></progress> <!-- 無 value 屬性即為不確定模式 -->
優勢:語義化、屏幕閱讀器友好、瀏覽器原生樣式(可通過 `appearance:none` 重置自定義樣式)、無需 JS 維護寬度計算。
progress { -webkit-appearance:none; appearance:none; width:100%; height:20px; border-radius:10px; background:#eee; }
progress::-webkit-progress-bar { background:#eee; border-radius:10px; }
progress::-webkit-progress-value { background:#4caf50; border-radius:10px; transition:width 0.3s; }
progress::-moz-progress-bar { background:#4caf50; border-radius:10px; } /* Firefox */
progress { color:#4caf50; } /* IE/Edge 文字顏色 */
針對超大文件(>100MB)客戶端分片上傳/處理,避免主線程阻塞:
| 場景 | 推薦方案 | 關鍵點 |
|---|---|---|
| 演示/模擬/固定流程 | 方案一 (setInterval) | 簡單、可控 |
| 文件上傳 | 方案二 (XHR upload.onprogress) | 唯一能獲取上傳進度的方式 |
| 文件下載/流式響應 | 方案二 (Fetch + ReadableStream) | 現代標準、支持流式處理 |
| 語義化/無障礙/簡單進度 | 方案三 (` | 零依賴、原生無障礙、SEO 友好 |
| 大文件客戶端預處理 | 方案四 (Web Worker + Stream) | 不阻塞主線程、支持分片/斷點續傳 |