I'd like the current image to fade out at the same time that the next image fades in (like the second image above), with no white flash in between. I feel like it has something to do with the javascript, but I can't detect what's wrong. Any advice?
Here's the component code:
const ImageSlider = ({ slides }) => {
const [current, setCurrent] = useState(0);
const length = slides.length;
const nextSlide = () => {
setCurrent(current === length - 1 ? 0 : current + 1);
};
const prevSlide = () => {
setCurrent(current === 0 ? length - 1 : current - 1);
};
if (!Array.isArray(slides) || slides.length <= 0) {
return null;
}
return (
<section className="slider">
<FaArrowAltCircleLeft className="left-arrow" onClick={prevSlide} />
{SliderData.map((slide, index) => {
return (
<div
className={index === current ? "slide active" : "slide"}
key={slide.id}
>
{index === current && (
<img src={slide.img} alt="" className="image" />
)}
</div>
);
})}
<FaArrowAltCircleRight className="right-arrow" onClick={nextSlide} />
</section>
);
};
export default ImageSlider;
And the css:
.slider {
position: relative;
height: 90vh;
}
.image {
width: 100vw;
height: 100vh;
object-fit: cover;
object-position: center;
}
.slide {
position: absolute;
inset: 0;
opacity: 0;
transition: 200ms opacity ease-in-out;
transition-delay: 200ms;
}
.slide.active {
opacity: 1;
z-index: 1;
transition-delay: 0ms;
}
.slide > img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;

