React 实现自定义进度条(类似于ant design中的progress)

一 说明

antd中的进度条不太符合项目需求,因此需要自己实现一款进度条。具体功能有:**颜色的自定义、最大百分比可以超过100%,自定义百分比文字标签的位置或者不显示标签。**其他功能可以自定义拓展。

二 实现源码

1.react 代码

javascript 复制代码
import React, {useEffect, useState} from 'react';
import styles from './index.module.less';

interface IProgressBarProps {
  percent: number; // 当前进度值
  maxPercent?: number; // 最大百分比(进度)
  color?: string; // 进度值颜色
  showText?: "right" | "center"  // 标签显示在哪里:右边、中间

}

const ProgressBar = (props: IProgressBarProps) => {
  const {percent, color, maxPercent, showText} = props;
  const [textStyle, setTextStyle] = useState<string>("progressTextCenter");
  const newMaxPercent = maxPercent ? maxPercent : 100; // 判断最大百分比
  const progressPercentage = (percent / newMaxPercent) * 100; // 计算百分比
  const newColor = color ? color : "#1677ff"; // 判断颜色(默认为#1677ff)

  useEffect(() => {
    switch (showText) {
      case "right":
        setTextStyle("progressTextRight");
        break;
      case "center":
        setTextStyle("progressTextCenter");
        break;
      default:
        setTextStyle("");
        break;
    }
  }, [showText])

  return (
    <div className={styles.outerContainer}>
      <div className={styles.progressContainer}>
        <div className={styles.progressBar} style={
  
  {width: `${progressPercentage}%`, backgroundColor: newColor}}></div>
      </div>
      {textStyle ? <div className={styles[textStyle]}>{percent}%</div> : ""}
    </div>
  );
};

export default ProgressBar;
  1. less样式 代码
css 复制代码
.outerContainer {
  display: flex;
  justify-content: space-between;
}

.progressContainer {
  width: 100%;
  height: 8px;
  background-color: #E9E9E9;
  border-radius: 10px;
  overflow: hidden;
  position: relative;
}


.progressBar {
  height: 100%;
  transition: width 0.3s ease;
  border-radius: 10px;
}

.progressTextCenter {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #000;
  font-size: 12px;
}

.progressTextRight {
  margin-left: 10px;
  margin-top: -3px;
  font-size: 12px;
}

三 代码的使用和效果

  1. 最常规样式:总长度100%,占比80%,文字标签在进度条中间(占比颜色可自定义)。

使用代码:

javascript 复制代码
<MyProgress color={"#1677ff"} maxPercent={100} percent={80} showText={"center"}/>
  1. 扩展样式:总长度150%,占比135%,文字标签在进度条右边(占比颜色可自定义)。

使用代码:

javascript 复制代码
 <MyProgress color={"gold"} maxPercent={150} percent={135} showText={"right"}/>
  1. 扩展样式:不显示文字标签

使用代码

javascript 复制代码
   <MyProgress color={"red"} maxPercent={150} percent={135}/>
相关推荐
北辰alk9 小时前
Next.js 全栈开发指南:从项目初始化到生产环境部署
react.js
梦帮科技10 小时前
GRAVIS v4.0:基于Web的极速套利架构设计与实时数据流实现
前端·人工智能·rust·自动化·区块链·智能合约·数字货币
爱勇宝10 小时前
办公资料反复修改、补传、交接混乱,我做了个桌面工具来解决这件事
前端·后端·程序员
微信开发api-视频号协议11 小时前
企业微信外部群开发自动化实践过程
java·前端·微信·自动化·企业微信·ipad
甲维斯11 小时前
Fable5:20美金的顶级设计师!
前端·人工智能
kyriewen11 小时前
我同时用了 Claude Code、Cursor 和 Codex,说说三个工具的真实差距——2026 年选错工具比不用 AI 更浪费时间
前端·ai编程·claude
Jackson__12 小时前
为了拯救我的腰椎和颈椎,我做了款浏览器插件!
前端·javascript·开源
闲猫12 小时前
Python FastAPI + SQLAlchemy 入门教程:从零搭建你的第一个 Web 应用
前端·python·fastapi
林小帅12 小时前
NestJS v11 + Prisma v7 ESM 迁移配置解析
前端·javascript·后端
IT_陈寒13 小时前
React的setState竟然不是立刻生效的,害我调试半天
前端·人工智能·后端