What am I doing wrong, that after pressing the button the animation does not repeat? Thanks for help.
JavaScript
x
4
1
var abox = document.getElementsByClassName("box")[0];
2
function allmove(){
3
abox.classList.toggle("move");
4
}
JavaScript
1
13
13
1
.vector img {
2
width: 20%;
3
height: 20%;
4
position: relative;
5
animation-name: example;
6
animation-duration: 4s;
7
animation-iteration-count: 1;
8
}
9
@-webkit-keyframes example{
10
0%{left:0px; top:0px;}
11
25%{left:200px; top:0px;}
12
100%{left:0px; top:0px;}
13
}
JavaScript
1
5
1
<div class="box"></div>
2
<div class="vector">
3
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqYooppFTzO8AydhbeZtjnrWpeZS5b7Gbi9EnwEPuuPW_t6ycn" />
4
</div>
5
<button class="button" onclick="allmove()">Click Me</button>
Advertisement
Answer
You can use something like this to refresh animation on click.
I added .animation class to separate it from vector class.This way you can easly toggle it.
the setTimeout is to wait a moment before adding the class after removing it.
JavaScript
1
5
1
var abox = document.getElementsByClassName("animation")[0];
2
function allmove(){
3
abox.classList.remove("animation");
4
setTimeout(function(){ abox.classList.add('animation') }, 100);
5
}
JavaScript
1
16
16
1
.vector img {
2
width: 20%;
3
height: 20%;
4
position: relative;
5
6
}
7
.animation img {
8
animation-name: example;
9
animation-duration: 4s;
10
animation-iteration-count: 1;
11
}
12
@-webkit-keyframes example{
13
0%{left:0px; top:0px;}
14
25%{left:200px; top:0px;}
15
100%{left:0px; top:0px;}
16
}
JavaScript
1
5
1
<div class="box"></div>
2
<div class="vector animation">
3
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqYooppFTzO8AydhbeZtjnrWpeZS5b7Gbi9EnwEPuuPW_t6ycn" />
4
</div>
5
<button class="button" onclick="allmove()">Click Me</button>