A button to toggle css animation paused | running play state

Viewed 546

I have a working css animation. My graphic is SVG. I have been trying to add a button to play and pause the animation on click, but I just can't seem to get it work.

What I have managed so far is as below.

    .animation1 {
        --animation-delay: 0.1s;
        animation: ani_keyframes 1.8s linear infinite var(--animation-delay) paused;
    }
    
    .playani {
        animation-play-state: running;
    }
    
    .animation1:hover {
        animation: none;
        fill: #666;
    }

@keyframes ani_keyframes {
...
...

}


<script type="text/javascript">
    var button = document.getElementById('button-ani'),
        test_ani = document.getElementByClassName('animation1');
    
    button.onclick = function(){
      test_ani.classList.toggle('playani');
    }
  
</script>
  
<div id="animation1_wrapper">

<svg id="animation1" width="100%" height="100%" viewBox="0 0 418 255" fill="none" xmlns="http://www.w3.org/2000/svg">
    
    <g id="ani_graphic">
<path class="animation1" d="M263.46 25.3801C257.63 29.2001 252.88 33.7801 249.33 37.8001H263.46V25.3801Z" fill="#000"></path>
.....
.....
.....
</g>
</svg>
</div>

 <button id="button-ani">Toggle Animation Play State</button>

2 Answers

use

document.querySelector('.animation1');

instead of

document.getElementsByClassName('animation1');

I add a translate animation for the demo. Hope it works :).

var button = document.getElementById('button-ani');
var test_ani = document.querySelector('.animation1');
    
button.onclick = function(){
  test_ani.classList.toggle('playani');
}
.animation1 {
  --animation-delay: 0.1s;
  animation: ani_keyframes 1.8s linear infinite var(--animation-delay) paused;
}

.playani {
  animation-play-state: running;
}

.animation1:hover {
  animation: none;
  fill: #666;
}

@keyframes ani_keyframes {
  from{
    transform:translateX(0px);
  }
  to{
    transform:translateX(-100px);
  }

}
<button id="button-ani">Toggle Animation Play State</button>

<div id="animation1_wrapper">
  <svg id="animation1" width="100%" height="100%" viewBox="0 0 418 255" fill="none" xmlns="http://www.w3.org/2000/svg">
    <g id="ani_graphic">
      <path class="animation1" d="M263.46 25.3801C257.63 29.2001 252.88 33.7801 249.33 37.8001H263.46V25.3801Z" fill="#000"></path>
      .....
      .....
      .....
    </g>
  </svg>
</div>

Since using document.getElementsByClassName('animation1') gives you HTMLCollection while ClassList applies to element so you can select the element by using indexer. and then your code works perfectly.

var button = document.getElementById('button-ani');

var test_ani = document.getElementsByClassName('animation1');
//this will give you HTML collection object so use [0] to access the element.        
    
    button.onclick = function(){
      test_ani[0].classList.toggle('playani');
      }
  
  .animation1 {
        --animation-delay: 0.1s;
        animation: ani_keyframes 1.8s linear infinite var(--animation-delay) paused;
    }
    
    .playani {
        animation-play-state: running;
    }
    
    .animation1:hover {
        animation: none;
        fill: #666;
    }
    @keyframes ani_keyframes {
  from{
    transform:scale(0);
  }
  to{
    transform:scale(1.1);
  }

}
 <button id="button-ani">Toggle Animation Play State</button>
<div id="animation1_wrapper">

<svg id="animation1" width="100%" height="100%" viewBox="0 0 418 255" fill="none" xmlns="http://www.w3.org/2000/svg">
    
    <g id="ani_graphic">
<path class="animation1" d="M263.46 25.3801C257.63 29.2001 252.88 33.7801 249.33 37.8001H263.46V25.3801Z" fill="#000"></path>
.....
.....
.....
</g>
</svg>
</div>

Related