当前位置: 首页 > article >正文

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

一 说明

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

二 实现源码

1.react 代码

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;

2. less样式 代码

.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%,文字标签在进度条中间(占比颜色可自定义)。

使用代码:

<MyProgress color={"#1677ff"} maxPercent={100} percent={80} showText={"center"}/>

   2.  扩展样式:总长度150%,占比135%,文字标签在进度条右边(占比颜色可自定义)。

 

使用代码:

 <MyProgress color={"gold"} maxPercent={150} percent={135} showText={"right"}/>

3. 扩展样式:不显示文字标签

 

使用代码

   <MyProgress color={"red"} maxPercent={150} percent={135}/>

 

 


http://www.kler.cn/a/539246.html

相关文章:

  • 力扣hot100刷题第一天
  • 在 Java 中使用数据库的存储过程有什么好处?如何在 JDBC 中调用存储过程?
  • 使用 Three.js 实现热力渐变效果
  • Photoshop自定义键盘快捷键
  • LIMO:上海交大的工作 “少即是多” LLM 推理
  • RuoYi-Vue-Oracle的oracle driver驱动配置问题ojdbc8-12.2.0.1.jar的解决
  • Log4j2在Spring项目中的集成与应用
  • 深度求索(DeepSeek)的AI革命:NLP、CV与智能应用的技术跃迁
  • 论文阅读:MGMAE : Motion Guided Masking for Video Masked Autoencoding
  • Deepseek的MLA技术原理介绍
  • C++实现黑白棋小游戏
  • Python和JavaScript在字符串比较上的差异
  • 高性能分布式全局ID生成器-雪花算法实现
  • 【设计模式】【行为型模式】模板方法模式(Template Method)
  • DeepSeek-R1 智能知识库系统使用指南
  • 上拉触底案例
  • 使用docker搭建FastDFS文件服务
  • 探头特征点创建
  • 数据库5(MySQL版)
  • Spring Boot单元测试实战指南
  • 蓝桥与力扣刷题(94 二叉树的中序遍历)
  • 【CubeMX-HAL库】STM32F407—无刷电机开环驱动
  • 从算法到落地:DeepSeek如何突破AI工具的同质化竞争困局
  • 【Rust中级教程】1.1. 指针概览(上):什么是指针、指针和引用的区别
  • [高等数学]不定积分的概念与性质
  • python笔记2--组合数据类型