I went through a couple of articles and stack questions for a solution, but I haven't been able to get this working. I'm trying to restart this animation upon button click so I can get a better idea of how to approach css animation in my web app. Unfortunately, it seems a lot harder than I initially thought it be with all the cross browser compatibility issues with some code.
HTML
<img src="http://s.cdpn.io/79/sprite-steps.png" />
<div class="hi"></div>
<input type = "button" id = "restart" value = "Restart" />
JavaScript
var $ = function(id){
return document.getElementById(id);
}
$("restart").addEventListener("click", function (e){
$("div").classList.remove("hi");
$("div").offsetWidth = $("div").offsetWidth;
$("div").classList.add("hi")
;}, false);
CSS
.hi {
width: 50px;
height: 72px;
background-image: url("http://s.cdpn.io/79/sprite-steps.png");
-webkit-animation: play .8s steps(10) 1;
-moz-animation: play .8s steps(10) 1;
-ms-animation: play .8s steps(10) 1;
-o-animation: play .8s steps(10) 1;
animation: play .8s steps(10) 1;
}
@-webkit-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@-moz-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@-ms-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@-o-keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
@keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
This is the JSFiddle link. I added and changed some stuff to what it originally had, but credits out to the original coder(s) always. JQuery answers are appreciated, but I would really like an answer in vanilla JavaScript. Thanks!