当前位置:首页 > 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);

实现动画效果

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

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中添加状态判断:

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控制:

js 进度条的实现

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 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…

js防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js实现滑动

js实现滑动

实现滑动效果的方法 在JavaScript中实现滑动效果可以通过多种方式完成,以下是几种常见的实现方法: 使用CSS过渡和JavaScript触发 通过CSS定义过渡效果,JavaScript控制触…

js计算器的实现

js计算器的实现

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

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…

js实现排序

js实现排序

数组排序方法 JavaScript提供了内置的sort()方法用于数组排序。默认情况下,sort()将元素转换为字符串并按照Unicode码点排序。对于数字排序,需传入比较函数。 const num…