当前位置:首页 > JavaScript

js实现进度条

2026-04-03 18:10:03JavaScript

使用HTML和CSS创建基础结构

进度条需要一个容器和一个表示进度的子元素。HTML结构可以这样设计:

<div class="progress-container">
  <div class="progress-bar" id="myProgressBar"></div>
</div>

CSS样式控制外观:

.progress-container {
  width: 100%;
  height: 20px;
  background-color: #f0f0f0;
  border-radius: 10px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background-color: #4CAF50;
  transition: width 0.3s ease;
}

通过JavaScript控制进度

使用DOM操作更新进度条宽度:

function updateProgress(percent) {
  const progressBar = document.getElementById('myProgressBar');
  progressBar.style.width = percent + '%';
}

// 示例:设置进度为50%
updateProgress(50);

实现动画效果

使用requestAnimationFrame实现平滑动画:

function animateProgress(targetPercent, duration = 1000) {
  const progressBar = document.getElementById('myProgressBar');
  const startPercent = parseFloat(progressBar.style.width) || 0;
  const startTime = performance.now();

  function update(currentTime) {
    const elapsed = currentTime - startTime;
    const progress = Math.min(elapsed / duration, 1);
    const currentPercent = startPercent + (targetPercent - startPercent) * progress;

    progressBar.style.width = currentPercent + '%';

    if (progress < 1) {
      requestAnimationFrame(update);
    }
  }

  requestAnimationFrame(update);
}

// 示例:动画过渡到75%
animateProgress(75);

监听任务进度

结合实际任务更新进度:

function simulateTask() {
  let progress = 0;
  const interval = setInterval(() => {
    progress += Math.random() * 10;
    if (progress >= 100) {
      progress = 100;
      clearInterval(interval);
    }
    updateProgress(progress);
  }, 300);
}

// 启动模拟任务
simulateTask();

使用HTML5 progress元素

原生progress元素更简单:

<progress id="html5Progress" value="0" max="100"></progress>

<script>
  const progressElement = document.getElementById('html5Progress');
  progressElement.value = 30; // 设置当前进度
</script>

圆形进度条实现

通过SVG创建圆形进度条:

<svg class="circular-progress" viewBox="0 0 36 36">
  <path class="circle-bg"
    d="M18 2.0845
      a 15.9155 15.9155 0 0 1 0 31.831
      a 15.9155 15.9155 0 0 1 0 -31.831"
  />
  <path class="circle-fill"
    stroke-dasharray="0, 100"
    d="M18 2.0845
      a 15.9155 15.9155 0 0 1 0 31.831
      a 15.9155 15.9155 0 0 1 0 -31.831"
  />
</svg>

JavaScript控制:

js实现进度条

function setCircleProgress(percent) {
  const circle = document.querySelector('.circle-fill');
  const circumference = 2 * Math.PI * 15.9155;
  const offset = circumference - (percent / 100) * circumference;
  circle.style.strokeDasharray = `${circumference} ${circumference}`;
  circle.style.strokeDashoffset = offset;
}

标签: 进度条js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterva…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 jQ…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…