当前位置:首页 > React

react 实现进度条

2026-01-27 11:12:47React

使用 React 实现进度条

使用原生 HTML5 <progress> 元素

React 可以直接渲染 HTML5 的 <progress> 元素,适用于简单的进度展示:

function ProgressBar({ value, max = 100 }) {
  return <progress value={value} max={max} />;
}
// 使用方式
<ProgressBar value={30} />

基于 CSS 的样式化进度条

通过 div 和 CSS 实现更灵活的样式控制:

react 实现进度条

function StyledProgressBar({ percentage }) {
  return (
    <div style={{ width: '100%', backgroundColor: '#e0e0e0', borderRadius: 5 }}>
      <div 
        style={{ 
          width: `${percentage}%`, 
          backgroundColor: '#4CAF50',
          height: 20,
          borderRadius: 5,
          transition: 'width 0.3s ease'
        }}
      />
    </div>
  );
}
// 使用方式
<StyledProgressBar percentage={75} />

使用第三方库(react-progressbar)

对于更复杂的需求,可以使用专门库如 react-progressbar

npm install react-progressbar

实现代码:

react 实现进度条

import ProgressBar from 'react-progressbar';

function CustomProgress() {
  return <ProgressBar completed={60} />;
}

动画进度条实现

结合 React 状态和 useEffect 实现动画效果:

import { useState, useEffect } from 'react';

function AnimatedProgressBar({ targetPercentage }) {
  const [currentPercentage, setCurrentPercentage] = useState(0);

  useEffect(() => {
    const timer = setInterval(() => {
      setCurrentPercentage(prev => 
        prev >= targetPercentage ? targetPercentage : prev + 1
      );
    }, 30);
    return () => clearInterval(timer);
  }, [targetPercentage]);

  return (
    <div style={{ width: '100%', backgroundColor: '#ddd' }}>
      <div style={{ 
        width: `${currentPercentage}%`, 
        backgroundColor: '#2196F3',
        height: 20,
        textAlign: 'center',
        color: 'white'
      }}>
        {currentPercentage}%
      </div>
    </div>
  );
}
// 使用方式
<AnimatedProgressBar targetPercentage={85} />

圆形进度条实现

使用 SVG 实现圆形进度条:

function CircularProgress({ radius = 50, strokeWidth = 10, progress }) {
  const normalizedRadius = radius - strokeWidth * 2;
  const circumference = normalizedRadius * 2 * Math.PI;
  const strokeDashoffset = circumference - (progress / 100) * circumference;

  return (
    <svg height={radius * 2} width={radius * 2}>
      <circle
        stroke="#ddd"
        fill="transparent"
        strokeWidth={strokeWidth}
        r={normalizedRadius}
        cx={radius}
        cy={radius}
      />
      <circle
        stroke="#4CAF50"
        fill="transparent"
        strokeWidth={strokeWidth}
        strokeDasharray={circumference + ' ' + circumference}
        style={{ strokeDashoffset }}
        r={normalizedRadius}
        cx={radius}
        cy={radius}
      />
      <text 
        x="50%" 
        y="50%" 
        textAnchor="middle" 
        dy=".3em"
        fill="#333"
      >
        {progress}%
      </text>
    </svg>
  );
}
// 使用方式
<CircularProgress progress={65} />

关键注意事项

  • 动态进度更新时建议添加 CSS transition 属性实现平滑动画
  • 确保进度值始终在 0 到 100 之间(或自定义的 max 值范围内)
  • 考虑添加 ARIA 属性提升无障碍体验:
    <div 
    role="progressbar" 
    aria-valuenow={percentage}
    aria-valuemin="0"
    aria-valuemax="100"
    >
    {percentage}%
    </div>

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

相关文章

电脑如何安装react

电脑如何安装react

安装 Node.js 和 npm React 依赖于 Node.js 和 npm(Node Package Manager)。从 Node.js 官网下载并安装最新稳定版本,安装完成后会自动包含 np…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-containe…

react项目如何

react项目如何

创建React项目 使用create-react-app工具快速初始化项目。确保已安装Node.js(建议版本≥12.0.0),运行以下命令生成新项目: npx create-react-app m…

react如何通信

react如何通信

React 组件通信方式 React 组件间的通信方式多样,具体选择取决于组件关系和场景需求。以下是常见方法: 父子组件通信 父组件通过 props 向子组件传递数据,子组件通过回调函数通知父组件…

react如何启动

react如何启动

启动 React 项目的步骤 确保已安装 Node.js React 需要 Node.js 环境运行,建议安装最新稳定版(LTS)。可通过以下命令检查是否已安装: node -v npm -v…

react 如何获取dom

react 如何获取dom

获取 DOM 的方法 在 React 中,可以通过 useRef 或 createRef 来获取 DOM 节点。useRef 是函数组件中常用的方法,而 createRef 通常用于类组件。 使用…