score:3
Accepted answer
The problem is that your .image
class set the same z-index and animation properties for all images.
Try to play with set delay like this:
{images.map((image, index) => (
<img
id={index}
className="image"
src={image}
style={{
animationDelay: `${index * 4}s`, // delay animation for next images
zIndex: `-${index + 1}` // make images as layers, not same layer -1
}}
/>
))}
But if you are using React, you could also try to make it in React by interval and hooks, and animate slides in css by applied class to active slide:
import React, { useRef, useState, useEffect } from "react";
import "./Hero.css";
const images = [
" https://images.unsplash.com/photo-1470165451736-166cb1cc909d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2552&q=80",
"https://images.unsplash.com/photo-1537367075546-0529d021d198?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80",
"https://images.unsplash.com/photo-1590064589331-f19edc8bed34?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"
]
function Hero(props) {
const slidePresentationTime = 3000 // after how many ms slide will change - now 3s / 3000ms
const [currentSlide, setCurrentSlide] = useState(0) // value and function to set currrent slide index
const sliderInterval = useRef() // interval ref
useEffect(() => {
sliderInterval = setInterval(() => {
setCurrentSlide((currentSlide + 1) % images.length); // change current slide to next slide after 'slidePresentationTime'
}, slidePresentationTime);
// cleanup interval when your component will unmount
return () => {
clearInterval(sliderInterval)
}
})
return (
<div className="root">
<div className="content">
<h1 className="title">Title</h1>
<div>
{images.map((image, index) => (
<img
id={index}
key={index}
className={index === currentSlide ? 'image active' : 'image'}
src={image}
style={{
zIndex: `-${index + 1}`
}}
/>
))}
</div>
</div>
</div>
);
}
export default Hero;
And here it would be much easier to use transition
instead of animation
and just change the transition property (opacity) by class.
//styles
.image {
position: absolute;
left: 0;
top: 0;
overflow: hidden;
display: block;
width: 100vw;
height: 100vh;
object-fit: cover;
transition: opacity 1s ease;
opacity: 0;
}
.active {
opacity: 1;
}
Here is demo https://stackblitz.com/edit/react-2dd6iu
Source: stackoverflow.com
Related Query
- How to create an image background fade when mapping an array of images with React
- How to target single item in list with onClick when mapping JSON array in React
- How do I create a loader that will load in between images when they load in an image slider in react js
- How can I repeat a pattern image to create a background in React Native?
- How to specify a key for React children when mapping over an array
- react, css) transition css for fade in and out, when background image is changed with className
- How to create new elements with React mapping props
- React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
- How do I create an array of unique field values in React with data from Firestore?
- How to re-render react component when mapping over state that is array of objects
- How to handle recursive array mapping with React components?
- React background image style not working with local images
- How to display an image from an array of images in react
- How can I fill a konva rect with fillPatternImage in React or create an infinite grid with the same svg image in React
- Setting dynamic background image in React with create react-app
- Header background image disappear and fade away on scroll with bottom gradient in React app
- REACT I want to create an ARRAY with the names of the files of the images in the PUBLIC FOLDER
- React - Dealing with undefined state when mapping over an array of objects from API
- Toggling between an image grid and image slider with one array of images in react hooks
- How to make an svg image as a background image with react and Tailwind css?
- how to convert an array to a array of objects with additional information in react using map when doing a POST api request
- How to keep previous values when updating array in React with useState
- React too many re-renders when create an array with UseState
- How can I create an array of imported images in a React Component?
- React : how to display images from array into carousel with its thumbnail on using react-image-gallery
- How to create a signup form with React and Why won't a function be executed when called insight a form after input field?
- How to setState in React when async mapping through array and API call
- Create fade in/out animation on background images change css in react
- How to create an array in react with typescript?
- How to display the image and background image from the array in react
More Query from same tag
- How can we run jest unit tests along with npm start or npm build?
- React Router: Route with nested params not hit
- React - state for each variable vs as little state necessary
- Javascript Reactjs button component not showing
- Wrap withRouter in an ES7 function
- React state is undefined when setting it with a hook
- Stuck with webpack HMR configuration
- API receiving undefined data
- Switch to login returning element type is invalid in React
- what happens to http request when route changes in SPA
- getting undefined on form submit reactjs
- how to upload image or pdf file with form-data in react-native?
- How to enlarge the clickable of CSS button?
- React: fetch updated data from server without refresh
- Multiple layout are not working in reactjs
- Exclude results from array that contains ONLY the sent query. MongoDB
- Proper way to two .save() in mongoose
- Axios post is not working inside react useeffect
- ReactJS: Why shouldn't I mutate nested state?
- fetch data from multiple apis with async await
- ReactJs. the function is called before the state is updated
- useState with value that may be null
- Component it's not being display in react app
- make first column sticky in react table with only CSS
- Nothing was returned from render in react functional component
- Error sending data back to apollo server by (bad request error)
- Uploading file via Google Drive API with simple upload (uploadType=media)
- Parse date/time value from react-datetime
- Open new page when clicking on a custom react component
- Disable / workaround React key requirement?