react, css) transition css 用于淡入和淡出,当背景图像使用 className 更改时

react, css) transition css for fade in and out, when background image is changed with className

我每 3 秒更改一次 class一个 div 的名称(使用 setInterval 更改状态)。 每个 classes 都有不同的背景图像。

我想在背景图像发生变化时淡入淡出,并带有过渡 css。我看到了一些更简单的案例的例子,但是我有两个以上的元素可以改变/改变状态改变图片。

我该怎么做?

我上传了我的代码:https://stackblitz.com/edit/react-ubxffz 但我无法在此页面上传图片,因此暂时将其替换为此页面中的背景色。

这是图像幻灯片组件。

const imgUrls = [
    1,2,3
];

class ImageSlide extends Component {

render() {
    const { url } = this.props;
    const Text=...
    return (          
        <div>
            <div className={`pic${url}`}>
                <p className="p1_1">{Text.p1_1}</p>
            </div>
        </div>      
    );
}

这是调用ImageSlide的App组件。

class App extends Component {
constructor (props) {
    super(props);
        currentImageIndex: 0,
    };
}

// ...

componentDidMount() {
    this.interval = setInterval(() => {
        this.nextSlide(); //this function change the index state. 
        }, 3000);   
}

componentWillUnmount() {
clearInterval(this.interval);
}

// ...

<ImageSlide url={imgUrls[this.state.currentImageIndex]} />

这是 css 每个 class,设置背景图片。

.pic1 {
  background-image: url('images/img_01.png');
}

.pic2 {
  background-image: url('images/img_02.png');
}

.pic3 {
  background-image: url('images/img_03.png');
}

对每个 class 使用 animation,如下所示:

See working code

操!!

.pic1 {
  background-image: url('images/img_01.jpg');
  animation: fade 3s infinite;
}

.pic2 {
  background-image: url('images/img_02.jpg');
  animation: fade 3s infinite;
}

.pic3 {
  background-image: url('images/img_03.jpg');
   animation: fade 3s infinite;
}

@keyframes fade {
  0%,100% { opacity: 0 }
  50% { opacity: 1 }
}

它是这样工作的:淡化背景:你需要有两个具有不同背景图像的元素,它们相互堆叠,然后交叉淡化

stackblitz 中的工作代码。

没有框架的工作代码:

const imgUrls = [
    1,2,3
];
let currentIndex = 0;
const lastIndex = imgUrls.length - 1;

const nextSlide = () => {
  currentIndex++;
  currentIndex = currentIndex % (lastIndex + 1)
  
  // @See https://css-tricks.com/restart-css-animation/
  const elm = document.getElementById('root')
    .querySelector('[class^="pic"],[class*=" pix"]');
  elm.className = `pic${currentIndex+1}`
  const newone = elm.cloneNode(true);
  elm.parentNode.replaceChild(newone, elm);
}

interval = setInterval(() => {
  console.log()
  nextSlide(); //this function change the index state. 
}, 3000);
#root {
  position: relative;
  width: 640px;
  height: 480px;
}
#root .front,
#root .back {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
#root .front {
  z-index: 2;
  opacity: 0;
}
#root .back {
  z-index: 1;
  opacity: 1;
}
#root [class^="pic"] .front,
#root [class*=" pic"] .front {
  -webkit-animation: in 3s 0s;
          animation: in 3s 0s;
}
#root .pic1 .front,
#root .pic2 .back {
  background-image: url("https://picsum.photos/640/480?image=1");
}
#root .pic1.init .back {
  background-image: none;
}
#root .pic2 .front,
#root .pic3 .back {
  background-image: url("https://picsum.photos/640/480?image=2");
}
#root .pic3 .front,
#root .pic1 .back {
  background-image: url("https://picsum.photos/640/480?image=3");
}

@-webkit-keyframes in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div id="root">
  <div class="pic1 init">
    <div class="front"></div>
    <div class="back"></div>
  </div>
</div>

我找到了一个带有 React Hooks 的解决方案

import React, { useState, useEffect } from "react";

const heroImage = ["hero1.svg", "hero2.svg"];

export default function Home() {
  const [activeIndex, setactiveIndex] = useState(0);
  useEffect(() => {
    setInterval(() => {
      currentImageIndex === 0 ? setactiveIndex(1) : setactiveIndex(0);
    }, 3000);
  });

  return (
    <div>
      <img
        className={styles.featureImage}
        src={"img/" + heroImage[activeIndex]}
        alt={"imageTitle"}
      />
    </div>
  );
}

CSS 看起来像这样

.featureImage {
  height: 600px;
  width: 600px;
  animation: fade 3s infinite;
}