What I'm trying to do:
-A javaScript animation (114 frames in total) that swaps out one image for the next in fast succession.
-onmouseover an image of a door opens. (plays 72 frames and stays on frame 72)
-onmouseout the door closes. (plays 42 frames and stays on the last frame)
-if the mouse is moved away from the element before the animation is completed, it will finish the 72 frames and then play the 42 frames.
-if the mouse is moved back onto the element before the 42 frames have finished, it will finish playing the 42 frames and then play the 72 frames.
The problems:
-I'm a noob at javaScript and don't fully understand it yet.
-Even though it sort of works, it's very buggy, you can't move your mouse away from the element without messing up the animation.
-Also, I can't figure out how to make it do all of the things listed above.
Here's the code I have right now:
HTML:
<div onmouseover="openDoor()" onmouseout="closeDoor()" id="door2"></div>
<div id="door">
<img src="images/Animation_Door/0001.png">
<img src="images/Animation_Door/0002.png">
<img src="images/Animation_Door/0003.png">
...etc... (114 frames)
</div>
CSS:
#door {
background-color:transparent;
...etc...
}
.door img{
display: none;
}
.door img:first-child {
display: block;
}
javaScript:
function openDoor() {
var ti, frame = 0;
var frames = document.getElementById("door").children;
var frameCount = frames.length;
for (i=0; i<72; i++) {
ti = setTimeout(function(){
frames[i % frameCount].style.display = "none";
frames[++i % frameCount].style.display = "block";
}, 50*i);
}
}
function closeDoor() {
var ti, frame = 0;
var frames = document.getElementById("door_close").children;
var frameCount = frames.length;
for (i=0; i<42; i++) {
setTimeout(function(){
frames[i % frameCount].style.display = "none";
frames[++i % frameCount].style.display = "block";
}, 50*i);
}
}