I have a list of 5 images in my react app which I would like to cycle through in an infinite loop. I basically want to animate these 5 frames so that a light bar looks like a light that is constantly moving.
So the dot which shifts in each image will look as though it is moving.
I am currently importing each image and rendering it in react-bootstraps Image
ponent. I know my approach is probably off below. How would I go about doing this accurately?
My attempts
//images
import bar1 from "../assets/bar1.png";
import bar2 from "../assets/bar2.png";
import bar3 from "../assets/bar3.png";
import bar4 from "../assets/bar4.png";
import bar5 from "../assets/bar5.png";
//my state
state = {
bars:[bar1,bar2,bar3,bar4,bar5]
};
//function to cycle (this needs to run infinitely)
cycleBars =()=> {
let finalBar = this.state.bars0];
//return this.state.bars[0];
this.state.bars.map(e=>{
finalBar = e;
})
return finalBar;
}
//return from my ponent
<Image src={this.cycleBars()} />
I have a list of 5 images in my react app which I would like to cycle through in an infinite loop. I basically want to animate these 5 frames so that a light bar looks like a light that is constantly moving.
So the dot which shifts in each image will look as though it is moving.
I am currently importing each image and rendering it in react-bootstraps Image
ponent. I know my approach is probably off below. How would I go about doing this accurately?
My attempts
//images
import bar1 from "../assets/bar1.png";
import bar2 from "../assets/bar2.png";
import bar3 from "../assets/bar3.png";
import bar4 from "../assets/bar4.png";
import bar5 from "../assets/bar5.png";
//my state
state = {
bars:[bar1,bar2,bar3,bar4,bar5]
};
//function to cycle (this needs to run infinitely)
cycleBars =()=> {
let finalBar = this.state.bars0];
//return this.state.bars[0];
this.state.bars.map(e=>{
finalBar = e;
})
return finalBar;
}
//return from my ponent
<Image src={this.cycleBars()} />
Share
Improve this question
edited Dec 1, 2019 at 10:48
Renaldo Balaj
2,44013 silver badges24 bronze badges
asked Nov 30, 2019 at 15:48
LoF10LoF10
2,1273 gold badges30 silver badges80 bronze badges
2 Answers
Reset to default 6I will have gone with CSS animation. Here is the solution in React:
state = {
bars:[bar1,bar2,bar3,bar4,bar5],
activeImageIndex: 0
};
ponentDidMount(){
setInterval(()=>{
let newActiveIndex = this.state.activeImageIndex===4 ? 0 : this.state.activeImageIndex+1
this.setState({
activeImageIndex: newActiveIndex
})
}, 1000);
}
<Image src={this.state.bars[activeImageIndex]} />
A simpler way might be to do this with CSS:
.lightbox {
border: solid 3px black;
display: inline-flex;
padding: 10px 20px;
justify-content: space-between;
width: 200px;
position: relative;
margin-left: 24px;
align-items: center;
}
.light {
border: solid 3px black;
border-radius: 50%;
height: 15px;
width: 15px;
animation: blink 5s linear infinite;
}
.light:nth-child(2) {
animation-delay: 1s
}
.light:nth-child(3) {
animation-delay: 2s
}
.light:nth-child(4) {
animation-delay: 3s
}
.light:nth-child(5) {
animation-delay: 4s
}
@keyframes blink {
0% {
background-color: orangered;
}
19% {
background-color: orangered;
}
20% {
background-color: transparent;
}
100% {
background-color: transparent;
}
}
.lightbox::before,
.lightbox::after {
content: "";
border: solid 1.5px black;
width: 20px;
height: 0;
position: absolute;
}
.lightbox::before {
left: -24px;
}
.lightbox::after {
right: -24px;
}
<div class="lightbox">
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
</div>