1

It's probably something really simple and i'm just being stupid. The button won't show the animation. When I run the code, there is no error showing.

HTML:

<button id='testBtn' class='test'>Test</button>

CSS:

.test{
  animation-name: changePos;
  animation-duration: 3s;
  animation-iteration-count: infinite;
}
@keyframes changePos {
  0%{left: 0px;}
  50%{left: 1000px;}
  100%{left: 0px;}
}

.test{
  animation-name: changePos;
  animation-duration: 3s;
  animation-iteration-count: infinite;
}
@keyframes changePos {
  0%{left: 0px;}
  50%{left: 1000px;}
  100%{left: 0px;}
}
<button id='testBtn' class='test'>Test</button>

3 Answers3

0

Check whether those styles work on the element before making an animation

@keyframes changePos {
    0% {
        margin-left: 0px;
    }
    50% {
        margin-left: 1000px;
    }
    100% {
        margin-left: 0px;
    }
}
Anuja Nimesh
  • 408
  • 3
  • 13
0

Add animation like this:

animation: changePos 5s infinite;

.test {
  width: 100px;
  height: 100px;
  position: relative;
  animation: changePos 3s infinite;
}
@keyframes changePos {
  0%{left: 0px;}
  50%{left: 1000px;}
  100%{left: 0px;}
}
<button id='testBtn' class='test'>Test</button>
Rakesh Gupta
  • 3,507
  • 3
  • 18
  • 24
0

Set position: relative; You can also use animation in short like animation: changePos 4s linear 0s infinite alternate;

@keyframes changePos {
  0% {left: 0px;}
  50% {left: 400px;}
  100% {left: 0px;}
}

.test{
  animation-name: changePos;
  animation-duration: 3s;
  animation-timing-function: linear;
  animation-delay: 0s;
  animation-iteration-count: infinite;
  position: relative;
  /*animation: changePos 4s linear 0s infinite alternate;*/
}
<button id='testBtn' class='test'>Test</button>
JorgeZ
  • 168
  • 8