每个重新加载的背景变化

发布于 2025-02-13 04:15:39 字数 460 浏览 3 评论 0原文

在这里,出于测试目的,我仅添加了一个背景图像,但是我实际需要做的是更改每个重新加载的背景。这意味着我需要通过一个阵列,其中所有图像的路径都将在那里,并且每个重新加载的背景都应改变。 代码附加在下面。整个项目基于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音栖息无 2025-02-20 04:15:39

只需将其背景代码与此这样的代码:

   backgroundImage: `url('${backgroundUrlList[Math.floor((Math.random() * backgroundUrlList.length))]}')`

顺便说一句,您应该定义自己的背景图书馆。

Just wrtie the background code like this:

   backgroundImage: `url('${backgroundUrlList[Math.floor((Math.random() * backgroundUrlList.length))]}')`

You should define your own backgroundUrlList by the way.

缪败 2025-02-20 04:15:39

您可以制作一系列图像,并设置第一个图像

很重要的是,这将每5秒更改图像!您可以更改使用效果的间隔。

const imgArray = [image1, image2, image3, image4, image5];
const [state, setState] = useState({ img: 0 });

!!

import image1 from "../assets/im1.jpeg";
import image2 from "../assets/im2.jpeg";
import image3 from "../assets/im3.jpeg";
import image4 from "../assets/im4.jpeg";
import image5 from "../assets/im5.jpeg";

useEffect(() => {
    const interval = setInterval(() => {
      if (state.img === 4) {
        setState((prev) => ({
          ...prev,
          img: 0,
        }));
      } else {
        setState((prev) => ({
          ...prev,
          img: state.img + 1,
        }));
      }
    }, 5000);
    return () => clearInterval(interval);
  }, [state.img]);

​DIV设置背景图像

    <section
            class="hero-wrap hero-wrap-2 js-fullheight ftco-degree-bg"
            style={{
              backgroundImage: `url(${imgArray[state.img]})`,
              // backgroundSize: "cover",
              backgroundPosition: "center",
              // overflow: "hidden",
            
            }}
           
          >

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

const imgArray = [image1, image2, image3, image4, image5];
const [state, setState] = useState({ img: 0 });

Don't forget to import the images

import image1 from "../assets/im1.jpeg";
import image2 from "../assets/im2.jpeg";
import image3 from "../assets/im3.jpeg";
import image4 from "../assets/im4.jpeg";
import image5 from "../assets/im5.jpeg";

and in a useEffect change the state of image

useEffect(() => {
    const interval = setInterval(() => {
      if (state.img === 4) {
        setState((prev) => ({
          ...prev,
          img: 0,
        }));
      } else {
        setState((prev) => ({
          ...prev,
          img: state.img + 1,
        }));
      }
    }, 5000);
    return () => clearInterval(interval);
  }, [state.img]);

In your div set the backgroundImage

    <section
            class="hero-wrap hero-wrap-2 js-fullheight ftco-degree-bg"
            style={{
              backgroundImage: `url(${imgArray[state.img]})`,
              // backgroundSize: "cover",
              backgroundPosition: "center",
              // overflow: "hidden",
            
            }}
           
          >

合久必婚 2025-02-20 04:15:39

只需将图像URL存储到一个数组中,然后生成随机数即可。
然后使用生成的编号获取数组的随机索引。

let imageArr = ["back.jpeg", "front.jpeg", "forward.jpeg", "backward.jpeg", "other.jpeg"];
let randomNum = = Math.floor(imageArr.length * Math.random());
let randomImage = imageArr[randomNum];
console.log(randomImage);

添加随机图像变量如下所示。

<div className="App" style={
  { 
  backgroundImage: `url(${randomImage})`
  }

}&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.

let imageArr = ["back.jpeg", "front.jpeg", "forward.jpeg", "backward.jpeg", "other.jpeg"];
let randomNum = = Math.floor(imageArr.length * Math.random());
let randomImage = imageArr[randomNum];
console.log(randomImage);

Add the randomImage variable into your div's backgroundImage propery as follows.

<div className="App" style={
  { 
  backgroundImage: `url(${randomImage})`
  }

}>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文