简体   繁体   English

javaScript动画图像onmouseover / onmouseout

[英]javaScript Animating Images onmouseover/onmouseout

I have an animation (41 frames) of a door opening, I want to use javaScript to make it open onmouseover, and I want it to go back to frame 1 onmouseout. 我有一个开门的动画(41帧),我想使用javaScript在onmouseover上使其打开,我希望它回到onmouseout的第1帧。 I don't think I'm doing it quite right for the onmouseout part. 对于onmouseout部分,我认为我做的并不正确。 Thanks in advance for the help! 先谢谢您的帮助!

HTML:
    <div onmouseover="startAnimation()" onmouseout="stopAnimation()" 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...(41 frames)

css:
    #door img{
    display: none;
    }

    #door img:first-child {
    display: block;
    }

javaScript:
    function startAnimation() { 
    var frames = document.getElementById("door").children;
    var frameCount = frames.length;

    for (i=0; i<41; i++) {
    setTimeout(function(){
    frames[i % frameCount].style.display = "none";
    frames[++i % frameCount].style.display = "block";
    }, 50*i);
    }
    }

    function stopAnimation() {
    var frames = document.getElementById("door").children;
    var frameCount = frames.length;

    for (i=0; i<1; i++){
    setTimeout(function(){
    frames[++i % frameCount].style.display = "none";
    frames[i % frameCount].style.display = "block";
    }, 50*i);
    }
    }

here's a link to it: http://www.reveriesrefined.com/test 这是它的链接: http : //www.reveriesrefined.com/test

I think this is probably more like what you want: 我认为这可能更像您想要的:

var ti, frame = 0,
frames = document.getElementById('door').children;

function resetAnimation() {
    frame = 0;
    frames[0].style.display = 'block';
    for (var i = 1; i < frames.length; i++) {
        frames[i].style.display = 'none';
    }
}
function startAnimation() {
    console.log('start animation');
    resetAnimation();
    ti = setInterval(function() {
        frames[frame].style.display = 'none';
        frame ++;
        if (frame >= frames.length) frame = 0;
        frames[frame].style.display = 'block';
    }, 50);
}
function stopAnimation() {
    if (ti) {
        clearInterval(ti);
        ti = undefined;
    }
    resetAnimation();
}​

Note that it's usually more appropriate to use setInterval than setTimeout when you want a continuous animation. 请注意,如果要连续播放动画,通常使用setInterval比setTimeout更合适。 A global variable stores which frame we are currently viewing. 全局变量存储我们当前正在查看的帧。 resetAnimation sets the frame to zero and sets the display accordingly. resetAnimation将框架设置为零,并相应地设置显示。 startAnimation sets an interval timer that hides the old frame, increases the frame by one, and shows the new frame, every 50ms. startAnimation设置一个间隔计时器,该计时器每50ms隐藏一次旧帧,将帧增加一帧,并显示新帧。 Stop animation simply clears the interval timer and resets the animation. 停止动画只是清除间隔计时器并重置动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM