I am creating a small storytelling website using css and sprite animations.
The problem I am facing is that I do not know how to change keyframes as soon as I set them in css files, especially when I try to make it compatible with all browsers.
Here is what I have
main part of css:
#ManSprite{
overflow:hidden;
width:200px;
height:200px;
position:absolute;
top:280px;
left:140px;
background-image: url("http://imageshack.com/a/img571/1710/2r1h.png");
z-index:99;
animation: play .4s steps(2) infinite;
-webkit-animation: play .4s steps(2) infinite;
-moz-animation: play .4s steps(2) infinite;
-ms-animation: play .4s steps(2) infinite;
-o-animation: play .4s steps(2) infinite;
}
@keyframes play {
from { background-position: -200px; }
to { background-position: -600px; }
}
@-webkit-keyframes play {
from { background-position: -200px; }
to { background-position: -600px; }
}
@-moz-keyframes play {
from { background-position: -200px; }
to { background-position: -600px; }
}
@-ms-keyframes play {
from { background-position: -200px; }
to { background-position: -600px; }
}
@-o-keyframes play {
from { background-position: -200px; }
to { background-position: -600px; }
}
Then in my javascript, I would like to change the background position for each keyframe, but I'm not sure how to change it. I tried the following, but it does not work.
var StoryPart = 0;
var fromArray = new Array("0px", "-200px", "-800px", "-1400px", "-2200px", "-3200px");
var toArray = new Array("0px", "-600px", "-1400px", "-2200px", "-3230px", "-4000px");
var stepsArray = new Array(0, 2, 3, 4, 5, 4);
function nextBtn() {
StoryPart++;
startChange();
}
function startChange() {
document.getElementById('ManSprite').style.webkitAnimationName = "none";
document.getElementById('ManSprite').style.animationName = "none";
document.getElementById('ManSprite').style.msAnimationName = "none";
document.getElementById('ManSprite').style.oAnimationName = "none";
document.getElementById('ManSprite').style.mozAnimationName ="none";
setTimeout(function () { change("play", fromArray[StoryPart], toArray[StoryPart]); }, 0);
}
function findKeyframesRule(rule) {
var ss = document.styleSheets;
for (var i = 0; i < ss.length; ++i) {
for (var j = 0; j < ss[i].cssRules.length; ++j) {
if (ss[i].cssRules[j].type == window.CSSRule.WEBKIT_KEYFRAMES_RULE && ss[i].cssRules[j].name == rule
|| ss[i].cssRules[j].type == window.CSSRule.KEYFRAMES_RULE && ss[i].cssRules[j].name == rule
)
return ss[i].cssRules[j];
}
}
return null;
}
function change(anim, fromValue, toValue) {
var keyframes = findKeyframesRule(anim);
keyframes.deleteRule("0%");
keyframes.deleteRule("100%");
keyframes.insertRule("0% {background-position: " + fromValue + ";}");
keyframes.insertRule("100% {background-position: " + toValue + ";}");
document.getElementById('ManSprite').style.webkitAnimationName = anim;
document.getElementById('ManSprite').style.animationName = anim;
document.getElementById('ManSprite').style.msAnimationName = anim;
document.getElementById('ManSprite').style.oAnimationName = anim;
document.getElementById('ManSprite').style.mozAnimationName = anim;
}
For some reason in Chrome it does not find any cssRule inside the findKeyframesRule method, but in firefox it does. However, Firefox crashes on this line.
keyframes.insertRule("0% {background-position: " + fromValue + ";}");
which bothers me because it used deleteRule well.
Does anyone ever work with them?