0

What am I doing wrong, that after pressing the button the animation does not repeat? Thanks for help.

var  abox = document.getElementsByClassName("box")[0];
function allmove(){
       abox.classList.toggle("move");
}
.vector img {
    width: 20%;
    height: 20%;
    position: relative;
    animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: 1;  
}
@-webkit-keyframes example{
    0%{left:0px; top:0px;}
    25%{left:200px; top:0px;}
    100%{left:0px; top:0px;}
}
<div class="box"></div>
<div class="vector">
  <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqYooppFTzO8AydhbeZtjnrWpeZS5b7Gbi9EnwEPuuPW_t6ycn" />
</div>
<button class="button" onclick="allmove()">Click Me</button>
Rob
  • 14,746
  • 28
  • 47
  • 65
vmahth1
  • 203
  • 2
  • 9

3 Answers3

0

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.

var  abox = document.getElementsByClassName("animation")[0];
function allmove(){
    abox.classList.remove("animation");
    setTimeout(function(){ abox.classList.add('animation') }, 100);
}
    .vector img {
        width: 20%;
        height: 20%;
        position: relative;
 
    }
    .animation img {
        animation-name: example;
        animation-duration: 4s;
        animation-iteration-count: 1; 
    }
    @-webkit-keyframes example{
        0%{left:0px; top:0px;}
        25%{left:200px; top:0px;}
        100%{left:0px; top:0px;}
    }
    <div class="box"></div>
    <div class="vector animation">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqYooppFTzO8AydhbeZtjnrWpeZS5b7Gbi9EnwEPuuPW_t6ycn" />
    </div>
    <button class="button" onclick="allmove()">Click Me</button>
Mehdi Ayari
  • 478
  • 5
  • 13
0

That's it, but I think addEventListener is better than onclick()

I modify a bit your code

You can find jsfiddle example Here

const bike = document.querySelector(".bike");
const button = document.querySelector(".button");

function allMove(){
       bike.classList.toggle("move");
}

button.addEventListener('click', allMove);
.vector img {
    width: 20%;
    height: 20%;
    position: relative;
    
}

.move{
  
     animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: 1;  
}

@-webkit-keyframes example{
    0%{left:0px; top:0px;}
    25%{left:200px; top:0px;}
    100%{left:0px; top:0px;}
}
<div class="box"></div>
<div class="vector">
  <img class="bike" src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqYooppFTzO8AydhbeZtjnrWpeZS5b7Gbi9EnwEPuuPW_t6ycn" />
</div>
<button class="button">Click Me</button>
0

Inspired by Mehdi Ayari's answer, but I think using requestAnimationFrame is better than using a timeout.

var abox = document.querySelector(".animation");
function allmove() {
  requestAnimationFrame(() => {
    abox.classList.remove("animation");
    requestAnimationFrame(() => abox.classList.add("animation"));
  });
}
.vector img {
        width: 20%;
        height: 20%;
        position: relative;
 
    }
    .animation img {
        animation-name: example;
        animation-duration: 4s;
        animation-iteration-count: 1; 
    }
    @-webkit-keyframes example{
        0%{left:0px; top:0px;}
        25%{left:200px; top:0px;}
        100%{left:0px; top:0px;}
    }
 <div class="box"></div>
    <div class="vector animation">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSqYooppFTzO8AydhbeZtjnrWpeZS5b7Gbi9EnwEPuuPW_t6ycn" />
    </div>
    <button class="button" onclick="allmove()">Click Me</button>
Abbas Hosseini
  • 1,545
  • 8
  • 21