Animation on several images : Animation « GUI Components « JavaScript DHTML

Animation on several images


Examples From
JavaScript: The Definitive Guide, Fourth Edition

Legal matters: these files were created by David Flanagan, and are
Copyright (c) 2001 by David Flanagan.  You may use, study, modify, and
distribute them for any purpose.  Please note that these examples are
provided "as-is" and come with no warranty of any kind.

David Flanagan
<!-- The image that will be animated. Give it a name for convenience. -->
<img name="animation" src="images/0.gif">

// Create a bunch of off-screen images, and pre-fetch the "frames"
// of the animation into them so that they're cached when we need them.
var aniframes = new Array(10);
for(var i = 0; i < 10; i++) {
    aniframes[i] = new Image();                 // Create an off-screen image
    aniframes[i].src = "images/" + i + ".gif";  // Tell it what URL to load.

var frame = 0;         // The frame counter: keeps track of current frame.
var timeout_id = null; // Allows us to stop the animation with clearTimeout()

// This function performs the animation.  Call it once to start.
// Note that we refer to the on-screen image using its name attribute.
function animate() {
    document.animation.src = aniframes[frame].src; // Display the current frame
    frame = (frame + 1)%10;                        // Update the frame counter
    timeout_id = setTimeout("animate()", 250);     // Display next frame later.

<form>  <!-- This form contains buttons to control the animation. -->
  <input type="button" value="Start"
   onclick="if (timeout_id == null) animate();">
  <input type="button" value="Stop" 
   onclick="if (timeout_id) clearTimeout(timeout_id); timeout_id=null;">


Related examples in the same category

1.Attack animation
2.Circle Animation
3.Right to left animation
4.Flash animation in Javascript
5.Flash animation in JavaScript: Changing style Properties
6.Animation along Straight Line
7.Animation along a Circle
8.Dancing Text (IE)
9.Type Writer effect (IE)
10.Type Writer Effect 1.1 (IE)
11.JavaScript Ticker 1.2 (IE)
12.Animation: Lottery Number Picker and Statistics
13.Animation: wriggly
14.Animation: welcome message
15.Animation: trio
16.Auto lotto dip
17.Animation: snow
18.Animation: star
19.Animation: mouse doodle
20.Animation: fireworks
21.Animation: pretty
22.Animation: Random Movement
23.Lotto number draw
24.Following eyes
25.Animation: three eyes
26.Animation: eyes
27.Spot light
28.Big static eyes
29.Animation based on DIV with color flash
30.Framework for creating CSS-based animations
31.Animate dynamic element h1 tag
32.Popup window animation (fly across screen)
33.Using the onFilterChange Event Handler
34.JavaScript Animation
35.Periodically Updating the Text Displayed by an HTML Element
36.Moving an Airplane Across a Web Page
37.Link Hint Scroller 2.0 (IE)
38.Snow animation
39.Animation with JSTween