0

My animation is currently moving in a square motion. However, I want it to move from the left side of the screen to the right side and back again on an infinite loop. Can anyone help with this?

.animation {
  width: 10px;
  height: 10px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
}

@keyframes example {
  0% {
    background-color: red;
    left: 0px;
    top: 0px;
  }
  25% {
    background-color: red;
    left: 200px;
    top: 0px;
  }
  50% {
    background-color: red;
    left: 200px;
    top: 200px;
  }
  75% {
    background-color: red;
    left: 0px;
    top: 200px;
  }
  100% {
    background-color: red;
    left: 0px;
    top: 0px;
  }
}
<div class="animation"></div>
Jon Uleis
  • 17,693
  • 2
  • 33
  • 42
Marcus
  • 11
  • 1
  • 3

2 Answers2

5

You can set an animation's iteration count to infinite, and create a seamless loop by having the starting and ending keyframes (0% and 100%) share the same position, like so:

.animation {
  width: 10px;
  height: 10px;
  background-color: red;
  position: relative;
  animation: example 2s infinite;
}

@keyframes example {
  0%,
  100% {
    left: 0;
  }

  50% {
    left: 200px;
  }
}
<div class="animation"></div>
Jon Uleis
  • 17,693
  • 2
  • 33
  • 42
0

Try adding animation-iteration-count and setting it to infinite:

    animation-iteration-count: infinite