簡而言之,
如何用好
立即學習“前端免費學習筆記(深入)”;
<progress value="70" max="100"></progress>
這段代碼會顯示一個進度條,表示任務已經完成了 70%。如果省略 value 屬性,進度條會顯示一個不確定的進度,通常表現為動畫效果,表明任務正在進行中,但無法確定具體進度。
自定義
雖然
progress { width: 200px; height: 10px; background-color: #eee; border: none; border-radius: 5px; /* 圓角 */ } progress::-webkit-progress-bar { background-color: #eee; border-radius: 5px; } progress::-webkit-progress-value { background-color: #4CAF50; /* 進度條顏色 */ border-radius: 5px; } progress::-moz-progress-bar { background-color: #4CAF50; border-radius: 5px; }
這段 CSS 代碼演示了如何使用偽元素 ::-webkit-progress-bar、::-webkit-progress-value 和 ::-moz-progress-bar 來修改進度條的樣式。請注意,不同瀏覽器可能需要不同的偽元素。
雖然
const progressBar = document.querySelector('progress'); const fileInput = document.querySelector('input[type="file"]'); fileInput.addEventListener('change', function() { const file = fileInput.files[0]; const reader = new FileReader(); reader.addEventListener('progress', function(event) { if (event.lengthComputable) { const percentComplete = (event.loaded / event.total) * 100; progressBar.value = percentComplete; } }); reader.readAsArrayBuffer(file); });
這段 JavaScript 代碼演示了如何監聽文件上傳的進度,并更新
除了
當然。雖然
<div class="progress-bar"> <div class="progress-bar-inner" style="width: 70%;"></div> </div>
.progress-bar { width: 200px; height: 10px; background-color: #eee; border-radius: 5px; overflow: hidden; /* 確保內部元素不會超出邊界 */ } .progress-bar-inner { height: 100%; background-color: #4CAF50; border-radius: 5px; }
這段代碼使用
- 文件上傳:顯示文件上傳的進度。
- 數據加載:顯示數據加載的進度。
- 游戲加載:顯示游戲資源加載的進度。
- 安裝過程:顯示軟件安裝的進度。
- 視頻緩沖:顯示視頻緩沖的進度。
使用
雖然
如何讓
- 使用漸變色:可以使用 CSS 漸變色來創建更加吸引人的進度條。
- 添加動畫效果:可以使用 CSS 動畫來讓進度條更加生動。
- 自定義圖標:可以使用自定義圖標來代替默認的進度條。
- 結合文本提示:可以在進度條旁邊顯示文本提示,例如“已完成 70%”。
雖然
如何使用 JavaScript 平滑地更新
直接設置
let currentProgress = 0; const targetProgress = 70; // 目標進度 const duration = 1000; // 動畫持續時間,單位毫秒 const startTime = performance.now(); function animateProgress(currentTime) { const elapsedTime = currentTime - startTime; const progress = Math.min(elapsedTime / duration, 1); // 確保進度不超過 1 currentProgress = progress * targetProgress; progressBar.value = currentProgress; if (progress < 1) { requestAnimationFrame(animateProgress); } } requestAnimationFrame(animateProgress);
這段代碼使用 requestAnimationFrame 方法來平滑地更新
除了文件上傳,