当前位置:首页 > JavaScript

js 进度条的实现

2026-04-03 18:19:23JavaScript

使用HTML和CSS创建基础结构

在HTML中创建一个进度条容器和一个表示进度的子元素:

<div class="progress-container">
  <div class="progress-bar" id="progressBar"></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控制进度

使用JavaScript动态更新进度条宽度:

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

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

实现动画效果

为进度变化添加平滑动画:

js 进度条的实现

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

  function update(timestamp) {
    const elapsed = timestamp - startTime;
    const progress = Math.min(elapsed / duration, 1);
    const currentPercentage = startPercentage + (targetPercentage - startPercentage) * progress;

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

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

  requestAnimationFrame(update);
}

// 示例:从当前进度动画到75%
animateProgress(75);

响应式进度条

创建响应不同状态的进度条样式:

.progress-bar.warning {
  background-color: #ff9800;
}

.progress-bar.error {
  background-color: #f44336;
}

.progress-bar.complete {
  background-color: #2196F3;
}

JavaScript中添加状态判断:

js 进度条的实现

function setProgress(percentage) {
  const progressBar = document.getElementById('progressBar');
  progressBar.style.width = percentage + '%';

  progressBar.classList.remove('warning', 'error', 'complete');

  if (percentage < 30) {
    progressBar.classList.add('error');
  } else if (percentage < 70) {
    progressBar.classList.add('warning');
  } else {
    progressBar.classList.add('complete');
  }
}

圆形进度条实现

HTML结构:

<div class="circular-progress">
  <svg 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>
  <div class="percentage-text">0%</div>
</div>

CSS样式:

.circular-progress {
  position: relative;
  width: 100px;
  height: 100px;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 3;
}

.circle-fill {
  fill: none;
  stroke: #4CAF50;
  stroke-width: 3;
  stroke-linecap: round;
  transition: stroke-dasharray 0.5s ease;
}

.percentage-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
}

JavaScript控制:

function updateCircularProgress(percent) {
  const circle = document.querySelector('.circle-fill');
  const text = document.querySelector('.percentage-text');
  const circumference = 2 * Math.PI * 15.9155;
  const dashValue = (percent / 100) * circumference;

  circle.style.strokeDasharray = `${dashValue}, ${circumference}`;
  text.textContent = `${percent}%`;
}

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

相关文章

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

js实现自举

js实现自举

实现 JavaScript 自举的方法 JavaScript 自举(Bootstrap)通常指通过 JavaScript 动态加载自身或依赖的脚本,实现运行时环境的初始化或扩展。以下是几种常见实现方式…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…