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}/>
相关推荐
福兮说2 小时前
设计稿是 #4A7C6F,页面量出来是 #4B7C6F:HEX、HSL、透明度、canvas 来回转的七个坑
前端·javascript·css·canvas
郑州光合科技余经理3 小时前
海外版外卖加盟:总站与分站配送规则怎么分开管
java·开发语言·前端·后端·uni-app·php·ai编程
小呆呆6664 小时前
副业搞起来,小说,漫画,漫剧的成本优化思路
前端·后端·面试
Dovis(誓平步青云)4 小时前
浇水提醒刚弹出又消失,植物状态别只存一个百分比
开发语言·前端·javascript·pdf·ecmascript·电脑
码艺-Alimjan4 小时前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_5 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒5 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端
广州华水科技5 小时前
大坝安全监测解决方案:单北斗GNSS形变监测系统应用与维护
前端
yivifu6 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub
默_笙7 小时前
🛴 从散件到整机:DeepAgents 与 Agent 身上预留的那些"插槽"(前置介绍)
前端·javascript