Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering CSS3 Keyframes with javascript multiple times

I am triggering CSS3 Keyframes with javascript but its working for with first call after that any call to that function doesn't animate my div.

Here the Javascript code

function animateShare (imgSrc){            
    var share = document.getElementById("shareTools");
    share.style.animation = "testAnimate 1s ease-in-out 0s"
    //shareTools.style.animationPlayState = "running";
}

Sample of the issue (Click red box to preview)

var box = document.getElementById("box");
function animateBox(){
    box.style.animation = "box 1s ease-in-out 0s";
  }
#box{
  background:red;
  width:100px;
  height:100px;
  }

@keyframes box {
    50%{width:300px;}
  }
<div id='box' onclick='animateBox()'><div>

JSFIDDLE

I want it to animate everytime i call this function.

like image 345
Skyyy Avatar asked Nov 24 '25 08:11

Skyyy


1 Answers

You can use well known hack: destroy and create element to reset animation.

var box = document.getElementById("box");
function animateBox(){

    //destroy and create hack
    document.body.removeChild(box);
    document.body.appendChild(box);

    box.style.animation = "box 1s ease-in-out 0s";
  }
#box{
  background:red;
  width:100px;
  height:100px;
  }

@keyframes box {
    50%{width:300px;}
  }
<div id='box' onclick='animateBox()'><div>
like image 82
Everettss Avatar answered Nov 26 '25 21:11

Everettss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!