Play and Pause button in image animation using Javascript in HTML

Asked

Viewed 833 times

1

<style type='text/css'>
    #animation img{
        display: none;
    }
    #animation img:first-child{
        display: block;
    }
</style>
<script type='text/javascript'>
    //<![CDATA[
    onload = function startAnimation(){
        var frames = document.getElementById("animation").children;
        var frameCount = frames.length;
        var i = 0;
        setInterval(function(){
        frames[i % frameCount].style.display = "none";
            frames[++i % frameCount].style.display = "block";
        }, 100);
        stop;
    }
    //]]>
</script>
<div id="animation">
    <img src="cyanblue.png"/>
    <img src="canvas2.png"/>
</div>

How do I change this onload function to an onclick so that by clicking a button Play(input button #1), it starts animation, and by clicking another button Pause(input button #2) it stops the animation in such a way that when performing the function Pause, it stops exactly on the frame that was being displayed, and that while being clicked on the button Play, it continues exactly from the frame displayed after the Pause function has been run?

  • Related (and may be useful to you): http://answall.com/questions/6457/howto use pausar-e-dar-play-em-um-gif/6550#6550

1 answer

1

<style type='text/css'>
    #animation img{
        display: none;
    }
    #animation img:first-child{
        display: block;
    }
</style>
<script type='text/javascript'>
    //<![CDATA[
     function startAnimation(){
        var frames = document.getElementById("animation").children;
        var frameCount = frames.length;
        var i = 0;
        setInterval(function(){
        frames[i % frameCount].style.display = "none";
            frames[++i % frameCount].style.display = "block";
        }, 100); 
        stop;           
    }       
    //]]>
</script>
<div id="animation">
    <img src="cyanblue.png"/>
    <img src="canvas2.png"/>
</div>
<button onclick="startAnimation()">Clique aqui!</button>

That will solve.

  • Thanks, the code worked well! but how do I pause?

  • I put the code part to pause, I imagine it is so since I do not know the plugin vc ta using to run the video.

  • The Stopfunction part didn’t work here, and it’s not really a video, but two images.

  • In fact it is supposed to stop by itself. If you can reproduce in jsfiddle it is easier to help.

  • here’s an example, but the stop Function doesn’t seem to work on it. http://jsfiddle.net/k2Ljnrky/6/

  • 1

    @Rick, I took the liberty of making some changes to this jsFiddle, see if it fits. Here play and pause work perfectly. http://jsfiddle.net/k2Ljnrky/11/

  • perfectly what I wanted, but I’m sorry to bother you, but there’s only one question left. those javascript var src should remain there or have some way to work the same result as this fiddle with the img images html tags?

  • in fact it is different the way q he did and his, in his case it is an img tag with a "src" source, and javascript changes this source passing image by array in the variable on top of the code. in your case, it loads all the images inside the div each with its "src" and all of them with "display None" and the javascript will give display block in 1 by 1, I prefer his approach because it gives the possibility to change the source easily by adding new images and it can rotate infinitely. its it will rotate 1 time and the images will 1 overlapping the other and when it ends up rs. abç

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.