简而言之,
如何用好
立即学习“前端免费学习笔记(深入)”;
<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 方法来平滑地更新
除了文件上传,