1

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.

enter image description here

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 component. 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 component

<Image src={this.cycleBars()} />

2 Answers 2

6

I will have gone with CSS animation. Here is the solution in React:

 state = {
    bars:[bar1,bar2,bar3,bar4,bar5],
    activeImageIndex: 0
 };

 componentDidMount(){
   setInterval(()=>{
     let newActiveIndex = this.state.activeImageIndex===4 ? 0 : this.state.activeImageIndex+1     
     this.setState({
        activeImageIndex: newActiveIndex
     })
   }, 1000);


 }

 <Image src={this.state.bars[activeImageIndex]} />
Sign up to request clarification or add additional context in comments.

4 Comments

this is how you do it in the "react" way
css would have been better but I wanted a react way for this. Worked like a charm, thanks guys!
btw does looping through an image like this have any negative effects as far as bloating a website? Was curious if you knew.
i would suggest to create a gif and, let it roll its way
2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.