React页面使用ant design Spin加载遮罩指示符自定义成进度条的形式

React页面使用ant design Spin加载遮罩指示符自定义成进度条的形式具体实现:

javascript 复制代码
import React, { useState, useEffect, } from 'react';
import { Spin, Progress, } from 'antd';
import styles from './style.less';

const App = () => {
  // 全局加载状态
  const [globalLoading, setgGlobalLoading] = useState(false);
  // 进度条状态
  const [loadingProgress, setLoadingProgress] = useState(0);

  useEffect(() => {
    // API调用开始
    setgGlobalLoading(true);

    // 执行过程

    // API调用结束
    setgGlobalLoading(false);
  }, [])

  useEffect(() => {
    let timer;
    let progressInterval;

    if (globalLoading) {
      // 立即设置进度为 0,避免延迟显示时的跳跃
      setLoadingProgress(0);

      // 延迟显示进度条
      timer = setTimeout(() => {
        // 开始进度条动画
        progressInterval = setInterval(() => {
          setLoadingProgress(prev => {
            // 非线性增长,模拟真实加载
            const increment = Math.random() * 10;
            const newProgress = prev + increment;

            // 当接近完成时,放缓增长速度
            if (newProgress >= 95) {
              return 95; // 保持在 95%,等待实际加载完成
            }
            return newProgress;
          });
        }, 200); // 每 200ms 更新一次
      }, 100); // 延迟 100ms 后开始动画

    } else {
      // 加载完成时,快速填充到 100%
      setLoadingProgress(100);
      // 500ms 后隐藏进度条
      const completeTimer = setTimeout(() => {
        setLoadingProgress(0);
      }, 500);

      return () => clearTimeout(completeTimer);
    }

    return () => {
      clearTimeout(timer);
      clearInterval(progressInterval);
    };
  }, [globalLoading]);

  const customIndicator = (
    <Progress
      percent={loadingProgress}
      showInfo={false}
      strokeColor={{
        '0%': '#108ee9',
        '100%': '#87d068',
      }}
      strokeWidth={8}
      trailColor="#f5f5f5"
    />
  );

  return (
    <Spin
      spinning= { globalLoading }
      tip = "数据加载中,请稍候..."
      delay = { 100}
      indicator = { customIndicator }
      wrapperClassName = { styles.spinWrapper }
    >
      <div>......</div>
    </Spin>
  );
};

export default App;

css样式:

css 复制代码
.spinWrapper {
  width: 100%;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;

  :global {
    .ant-spin-container {
      width: 100%;
    }
    
    .ant-spin .ant-spin-text {
      font-size: 20px !important;
    }

    .ant-progress {
      width: 550px;
      margin-bottom: 12px;
    }

    .ant-spin.ant-spin-show-text .ant-spin-dot {
      transform: translateX(-48%);
    }
  }
}
相关推荐
嘟嘟07178 小时前
用单例模式管理弹窗:从一段原生 JS 理解 Singlet
前端·javascript·代码规范
一心只读圣贤书8 小时前
AI 辅助前端图片资源治理:从懒加载到多端适配
前端·人工智能
何时梦醒8 小时前
第三篇:React 组件化开发 — 函数即组件的哲学与实践
react.js·面试·typescript
一心只读圣贤书8 小时前
AI 辅助前端 Feature Flag 治理:从灰度开关到实验复盘
前端·人工智能
胡萝卜术8 小时前
编译期与运行期的双重防线:从 TypeScript 类型之争到 LLM 输出的自动化择优
前端·设计模式·面试
Goodbye8 小时前
前端路由深度解析:从原理到 React Router 实战
前端
何时梦醒8 小时前
🤖 Harness 工程:用 LLM as Judge + Best of N 打造自优化的 AI 代码生成流水线
前端·人工智能
AI_paid_community8 小时前
如何使用 Claude 在 AI 时代快速入局新的行业?(经验贴)
前端·javascript·后端
程序员韩星8 小时前
从模型直连到统一 AI 网关:多模型 API、Codex 与 Claude Code 接入实践
前端·程序员·ai编程
想要成为糕糕手8 小时前
🐎 从“幻觉”到“可控”:手把手构建一个 LLM 自优化流水线 Harness
前端·llm·agent