我一直在重写我的网站ondeband.com-目前是一个wordpress网站,大约需要一个月的时间来撰写它。
我不会过多地更改主页-它的背景图像每隔几秒钟就会更改一次。您可以在上面的链接中看到它。
问题出现在网站的新反应版本上-在每次图像更改之间都会闪烁-特别是当我将其推送到heroku时。在我的本地开发服务器上,它可以工作99%的时间。
我感觉是由于图像的“预加载”?也许您可以为我指明正确的方向-这是我应用首页的代码。
className为'homePage'的div是将bg图像加载到嵌入式样式中的位置。变量bgImage处于状态存储。我使用useEffect挂钩启动函数“ bgTransition”,该函数随机更改每个图像。
import React, { useState, useEffect } from "react";
import NavBarA from "./components/NavBarA";
import { Router, Route, Switch } from "react-router-dom";
import BookABand2 from "./components/Profile/BookABand2";
import LiveProfile from './components/BookABand/LiveProfile'
import Account from './components/Account/Account'
import history from "./uitls/history";
import PrivateRoute from "./components/PrivateRoute";
import 'bootstrap/dist/css/bootstrap.min.css';
import AqgSetup3 from "./components/AqgSetup3";
import './App.css';
import { useAuth0 } from './react-auth0-spa'
import Banjo from './BackgroundImgs/Banjo.jpg'
import Hands from './BackgroundImgs/Hands.jpg'
import Mic from './BackgroundImgs/Mic.jpg'
import Sax from './BackgroundImgs/Sax.jpg'
import { Button } from 'reactstrap'
function App() {
const { user } = useAuth0()
const [bgImgArray] = useState([Banjo, Hands, Mic, Sax])
const [ bgImg, setBgImg ] = useState(Banjo)
const { loginWithRedirect, isAuthenticated } = useAuth0();
useEffect(() => {
bgTransition()
}, [] )
const bgTransition = () => {
let newNum = Math.floor(Math.random() * 4)
setBgImg(bgImgArray[newNum])
setTimeout(() => {
bgTransition()
}, 5000)
}
if(!bgImgArray){
return <div>loading...</div>
}
return (
<div className="App h-100" style={{
paddingTop: user ? '85px' : '0px'
}}>
<Router history={history}>
<header>
<NavBarA color={ user ? 'light' : ''} className={user ? 'navbar text-dark fixed-top shadow-lg' : "navbar text-light fixed-top"}/>
</header>
<Switch>
<Route path="/" exact >
<div className='homePage h-100' style={{
display: !isAuthenticated ? 'block' : 'none',
backgroundImage: `url(${bgImg})`,
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
webkitTransition: 'background-image 1s ease-in-out',
transition: 'background-image 1s ease-in-out',
}}>
<div className='h-100 w-100 position-absolute'style={{
background: 'linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) )',
zIndex: '10'
}}/>
<div className="d-flex flex-column w-100 h-100 align-items-center justify-content-center position-relative" style={{zIndex: '100'}}>
<h1 className='text-light'>On Demand</h1>
<h1 className='text-light'>On DeBand</h1>
<h6 className='text-light'>Find, Support, and Book Local Bands</h6>
<div className='d-flex flex-row'>
<Button outline color='light' size='lg' className='mx-2 my-2' onClick={() => {loginWithRedirect()}}>Find Bands</Button>
</div>
</div>
<div>
</div>
</div>
<div style={{
display: user ? 'block' : 'none',
}}>
<BookABand2 />
</div>
</Route>
<PrivateRoute path="/BookABand2" component={BookABand2} />
<PrivateRoute path="/AqgSetup3/:id" component={AqgSetup3} />
<PrivateRoute path="/band/:id" component={LiveProfile} />
<PrivateRoute path="/account" component={Account} />
</Switch>
</Router>
</div>
);
}
export default App;
感谢您的帮助!
您可以只用html来做,只需要对React代码进行一些细微调整。
要更早下载图像,您可以按照this answer。唯一的问题是,您不知道图片的网址,因为一旦您建立网站,图片的网址就会被随机化。
为了减轻这种情况,请将图像从src
文件夹移到public
文件夹。我假设您将使用/public/images/
来存储它们。这是修改后的代码:
index.html
<head>
..
..
<link rel="preload" href="%PUBLIC_URL%/images/Banjo.jpg" as="image">
<link rel="preload" href="%PUBLIC_URL%/images/Hands.jpg" as="image">
<link rel="preload" href="%PUBLIC_URL%/images/Mic.jpg" as="image">
<link rel="preload" href="%PUBLIC_URL%/images/Sax.jpg" as="image">
...
</head>
App.js
import React, { useState, useEffect } from "react";
import NavBarA from "./components/NavBarA";
import { Router, Route, Switch } from "react-router-dom";
import BookABand2 from "./components/Profile/BookABand2";
import LiveProfile from './components/BookABand/LiveProfile'
import Account from './components/Account/Account'
import history from "./uitls/history";
import PrivateRoute from "./components/PrivateRoute";
import 'bootstrap/dist/css/bootstrap.min.css';
import AqgSetup3 from "./components/AqgSetup3";
import './App.css';
import { useAuth0 } from './react-auth0-spa'
import { Button } from 'reactstrap'
function App() {
const [bgImgArray] = useState(['Banjo.jpg', 'Hands.jpg', 'Mic.jpg', 'Sax.jpg'])
...
return (
<div className="App h-100" style={{
paddingTop: user ? '85px' : '0px'
}}>
<Router history={history}>
...
<Switch>
<Route path="/" exact >
<div className='homePage h-100' style={{
display: !isAuthenticated ? 'block' : 'none',
backgroundImage: `url(/public/images/${bgImg})`,
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
webkitTransition: 'background-image 1s ease-in-out',
transition: 'background-image 1s ease-in-out',
}}>
...
</div>
</Route>
...
</Switch>
</Router>
</div>
);
}
export default App;