每个重新加载的背景变化
在这里,出于测试目的,我仅添加了一个背景图像,但是我实际需要做的是更改每个重新加载的背景。这意味着我需要通过一个阵列,其中所有图像的路径都将在那里,并且每个重新加载的背景都应改变。 代码附加在下面。整个项目基于React
function App() {
return (
<div className="App" style={
{
backgroundImage: `url("back.jpg")`
}
}>
<div className='container'>
<div className='container'>
<Header/>
<Footer/>
</div>
</div>
</div>
);
}
here for the testing purpose I have added only one background image, but what I actually need to do is to change the background on each reload. That means I need to pass an array where the path of all the images will be there and the background should change on each reload.
The code is attached below. The whole project is based on react
function App() {
return (
<div className="App" style={
{
backgroundImage: `url("back.jpg")`
}
}>
<div className='container'>
<div className='container'>
<Header/>
<Footer/>
</div>
</div>
</div>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将其背景代码与此这样的代码:
顺便说一句,您应该定义自己的背景图书馆。
Just wrtie the background code like this:
You should define your own backgroundUrlList by the way.
您可以制作一系列图像,并设置第一个图像
很重要的是,这将每5秒更改图像!您可以更改使用效果的间隔。
!!
DIV设置背景图像
You can make an array of images , and set the state of the first one
Important this will change image every 5 sec ! you can change the interval in the useEffect .
!If you want to change just every time when render , make a Math.random from the lenght of arrayOfImages.length
Don't forget to import the images
and in a useEffect change the state of image
In your div set the backgroundImage
只需将图像URL存储到一个数组中,然后生成随机数即可。
然后使用生成的编号获取数组的随机索引。
添加随机图像变量如下所示。
}&gt;
Just store the image URLs into one array and then generate random number .
And then get the random index of an array using the generated number.
Add the randomImage variable into your div's backgroundImage propery as follows.
}>