如何自定义React应用的启动加载页(Splash Screen)
自定义Create React App PWA启动屏(Splash Screen)方案
这个启动加载页的正式名称是 Splash Screen(启动屏/闪屏),默认由浏览器根据manifest.json生成,配置有限。以下是两种自定义实现方案,覆盖静态快速显示和React组件化控制两种场景:
方案一:静态HTML/CSS快速启动屏(推荐)
适合需要在React初始化前就显示的场景,避免白屏,体验更流畅。
代码编写位置
所有代码都放在项目根目录的public/index.html中:
- 在
<head>里添加自定义启动屏样式:
<style> #splash-screen { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; /* 自定义背景色/渐变 */ background: #2c3e50; display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 9999; } #splash-screen .logo { width: 120px; height: 120px; /* 自定义动画 */ animation: spin 2s linear infinite; } #splash-screen .title { color: white; margin-top: 20px; font-size: 1.8rem; } @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } </style>
- 在
<body>的最顶部添加启动屏DOM元素(放在React根节点之前):
<div id="splash-screen"> <img class="logo" src="%PUBLIC_URL%/logo512.png" alt="App Logo"> <h1 class="title">My Custom PWA</h1> </div> <div id="root"></div>
- 在
<body>末尾添加JS代码,当React加载完成后移除启动屏:
<script> // 监听React根节点加载完成,或直接等待页面加载完成 window.addEventListener('load', () => { setTimeout(() => { const splashScreen = document.getElementById('splash-screen'); if (splashScreen) { // 添加淡出动画过渡 splashScreen.style.opacity = '0'; splashScreen.style.transition = 'opacity 0.5s ease-out'; setTimeout(() => splashScreen.remove(), 500); } }, 1000); // 可根据实际加载时间调整延迟 }); </script>
方案二:React组件化启动屏
适合需要和应用状态联动的场景(比如根据加载进度显示不同内容)。
代码编写位置
- 创建启动屏组件:
src/components/SplashScreen.jsx
import './SplashScreen.css'; export default function SplashScreen() { return ( <div className="splash-container"> <div className="splash-content"> <img src="/logo512.png" alt="App Logo" className="splash-logo" /> <h2 className="splash-text">加载中...</h2> <div className="splash-loader"></div> </div> </div> ); }
- 对应的样式文件:
src/components/SplashScreen.css
.splash-container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(120deg, #f6d365 0%, #fda085 100%); display: flex; align-items: center; justify-content: center; z-index: 9999; } .splash-content { text-align: center; color: white; } .splash-logo { width: 100px; height: 100px; margin-bottom: 20px; } .splash-text { margin-bottom: 15px; font-size: 1.5rem; } .splash-loader { width: 40px; height: 40px; border: 4px solid rgba(255,255,255,0.3); border-top-color: white; border-radius: 50%; animation: spin 1s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } }
- 在根组件
src/App.jsx中控制启动屏显示:
import { useState, useEffect } from 'react'; import SplashScreen from './components/SplashScreen'; function App() { const [isLoading, setIsLoading] = useState(true); useEffect(() => { // 模拟加载过程,实际替换为真实的资源加载/API请求 const loadApp = async () => { // 示例:等待2秒后结束加载 await new Promise(resolve => setTimeout(resolve, 2000)); setIsLoading(false); }; loadApp(); }, []); if (isLoading) { return <SplashScreen />; } return ( <div className="app"> {/* 你的应用核心内容 */} <h1>我的PWA应用</h1> </div> ); } export default App;
注意事项
- 调整
manifest.json配置:确保display设为standalone,start_url设为./,这样PWA从快捷方式打开时会以独立窗口运行,启动屏效果更完整。 - 避免启动屏停留过久:根据实际应用加载时间调整延迟,不要让用户等待太长。
- 适配不同设备:启动屏样式要考虑移动端的各种屏幕尺寸,使用相对单位(vw/vh、rem)。
内容的提问来源于stack exchange,提问作者Islam Y-
相关产品推荐
相关产品推荐

