当前位置:首页 > JavaScript

js 实现进度条

2026-01-16 13:35:10JavaScript

使用 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 更新进度

使用 style.width 属性动态更新进度条宽度:

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

// 示例:更新到50%
updateProgress(50);

实现动画效果

结合 requestAnimationFrame 实现平滑动画:

js 实现进度条

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

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

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

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

  requestAnimationFrame(step);
}

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

监听实际任务进度

对于真实任务(如文件上传),可以结合事件监听:

// 模拟上传进度事件
const uploadTask = {
  start: function() {
    let progress = 0;
    const interval = setInterval(() => {
      progress += 5;
      updateProgress(progress);

      if (progress >= 100) {
        clearInterval(interval);
      }
    }, 200);
  }
};

// 触发上传
document.querySelector('button').addEventListener('click', () => uploadTask.start());

添加文本百分比显示

在进度条内显示当前进度:

js 实现进度条

<div class="progress-bar" id="progressBar">
  <span class="progress-text">0%</span>
</div>

更新 JavaScript:

function updateProgress(percent) {
  const progressBar = document.getElementById('progressBar');
  progressBar.style.width = percent + '%';
  progressBar.querySelector('.progress-text').textContent = Math.round(percent) + '%';
}

响应式设计考虑

通过 CSS 变量实现可定制化:

.progress-container {
  --progress-color: #4CAF50;
  --progress-height: 10px;
}

.progress-bar {
  background-color: var(--progress-color);
  height: var(--progress-height);
}

浏览器兼容性处理

对于旧版浏览器,添加前缀或降级方案:

function updateProgress(percent) {
  const progressBar = document.getElementById('progressBar');
  progressBar.style.width = percent + '%';
  progressBar.style.webkitTransition = 'width 0.3s ease'; // 针对Webkit内核
}

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

相关文章

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 使用JavaScript实现拖拽功能需要监听鼠标事件,包括mousedown、mousemove和mouseup。以下是实现的基本逻辑: const draggableEleme…

vue实现水波球进度条

vue实现水波球进度条

实现水波球进度条的方法 使用 SVG 和 CSS 动画 创建一个圆形 SVG 元素,通过 CSS 动画实现水波效果。定义一个波浪路径,使用 animateTransform 或 CSS keyfram…

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.…

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的H…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document…