当前位置:首页 > JavaScript

js 进度条的实现

2026-01-12 13:26:02JavaScript

使用 HTML 和 CSS 创建基础进度条

HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度:

<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 + '%';
}

// 示例:更新进度到75%
updateProgress(75);

实现动画效果

结合 requestAnimationFrame 实现平滑动画:

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

  function step(currentTime) {
    const elapsed = currentTime - startTime;
    const progress = Math.min(elapsed / duration, 1);
    const currentWidth = startWidth + (targetPercent - startWidth) * progress;
    progressBar.style.width = currentWidth + '%';

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

  requestAnimationFrame(step);
}

// 示例:动画形式过渡到90%
animateProgress(90);

使用 HTML5 <progress> 元素

原生进度条元素更简洁:

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

JavaScript 控制:

const progressElement = document.getElementById('nativeProgress');
progressElement.value = 50; // 设置进度为50%

圆形进度条实现

通过 SVG 或 Canvas 实现圆形进度条:

<svg class="circular-progress" viewBox="0 0 36 36">
  <path class="track" 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="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 控制:

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

updateCircleProgress(75);

进度条事件监听

添加事件监听实现交互:

js 进度条的实现

document.getElementById('progressButton').addEventListener('click', () => {
  let currentProgress = 0;
  const interval = setInterval(() => {
    currentProgress += 5;
    updateProgress(currentProgress);
    if (currentProgress >= 100) clearInterval(interval);
  }, 200);
});

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

相关文章

css制作网格进度条

css制作网格进度条

使用 CSS Grid 制作网格进度条 CSS Grid 是一种强大的布局工具,可以用来创建网格进度条。以下是实现方法: HTML 结构 <div class="progress-grid"…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。…

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

js节流实现

js节流实现

节流的概念 节流(Throttle)是一种限制函数执行频率的技术,确保函数在一定时间间隔内只执行一次。常用于滚动事件、窗口调整等高频触发的场景。 基础实现方法 使用时间戳判断是否执行函数: fun…