<div class="codetitle"> <span><a style="CURSOR: pointer" data="32700" class="copybut" id="copybut32700" onclick="doCopy('code32700')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code32700"> <br> <br> <br> <br><title>图形动画</title> <br><style type="text/css"> <BR>.de{ font-size:30px; text-decoration:none; font-family:微软雅黑; color:#ccc;} <BR>.de:hover{ color:#933;} <BR></style> <br><script type="text/javascript"> <BR>/** <BR>* ImageLoop.js: An ImageLoop class for performing image animations <BR>* <BR>* Constructor Arguments: <BR>* imageId: the id of the <img alt="JavaScript 图像动画的小demo_javascript技巧" > tag which will be animated <BR>* fps: the number of frames to display per second <BR>* frameURLs: an array of URLs, one for each frame of the animation <BR>* <BR>* Public Methods: <BR>* start(): start the animation (but wait for all frames to load first) <BR>* stop(): stop the animation <BR>* <BR>* Public Properties: <BR>* loaded: true if all frames of the animation have loaded, <BR>* false otherwise <BR>*/ <BR>function ImageLoop(imageId, fps, frameURLs) { <BR>// Remember the image id. Don't look it up yet since this constructor <BR>// may be called before the document is loaded. <BR>this.imageId = imageId; <BR>// Compute the time to wait between frames of the animation <BR>this.frameInterval = 1000 / fps; <BR>// An array for holding Image objects for each frame <BR>this.frames = new Array(frameURLs.length); <br><br>this.image = null; // The <img alt="JavaScript 图像动画的小demo_javascript技巧" > element, looked up by id <BR>this.loaded = false; // Whether all frames have loaded <BR>this.loadedFrames = 0; // How many frames have loaded <BR>this.startOnLoad = false; // Start animating when done loading? <BR>this.frameNumber = -1; // What frame is currently displayed <BR>this.timer = null; // The return value of setInterval() <br><br>// Initialize the frames[] array and preload the images <BR>for (var i = 0; i < frameURLs.length; i++) { <BR>this.frames[i] = new Image(); // Create Image object <BR>// Register an event handler so we know when the frame is loaded <BR>this.frames[i].onload = countLoadedFrames; // defined later <BR>this.frames[i].src = frameURLs[i]; // Preload the frame's image <BR>} <br><br>// This nested function is an event handler that counts how many <BR>// frames have finished loading. When all are loaded, it sets a flag, <BR>// and starts the animation if it has been requested to do so. <BR>var loop = this; <BR>function countLoadedFrames() { <BR>loop.loadedFrames++; <BR>if (loop.loadedFrames == loop.frames.length) { <BR>loop.loaded = true; <BR>if (loop.startOnLoad) loop.start(); <BR>} <BR>} <br><br>// Here we define a function that displays the next frame of the <BR>// animation. This function can't be an ordinary instance method because <BR>// setInterval() can only invoke functions, not methods. So we make <BR>// it a closure that includes a reference to the ImageLoop object <BR>this._displayNextFrame = function () { <BR>// First, increment the frame number. The modulo operator (%) means <BR>// that we loop from the last to the first frame <BR>loop.frameNumber = (loop.frameNumber + 1) % loop.frames.length; <BR>// Update the src property of the image to the URL of the new frame <BR>loop.image.src = loop.frames[loop.frameNumber].src; <BR>}; <BR>} <br><br>/** <BR>* This method starts an ImageLoop animation. If the frame images have not <BR>* finished loading, it instead sets a flag so that the animation will <BR>* automatically be started when loading completes <BR>*/ <BR>ImageLoop.prototype.start = function () { <BR>if (this.timer != null) return; // Already started <BR>// If loading is not complete, set a flag to start when it is <BR>if (!this.loaded) this.startOnLoad = true; <BR>else { <BR>// If we haven't looked up the image by id yet, do so now <BR>if (!this.image) this.image = document.getElementById(this.imageId); <BR>// Display the first frame immediately <BR>this._displayNextFrame(); <BR>// And set a timer to display subsequent frames <BR>this.timer = setInterval(this._displayNextFrame, this.frameInterval); <BR>} <BR>}; <br><br>/** Stop an ImageLoop animation */ <BR>ImageLoop.prototype.stop = function () { <BR>if (this.timer) clearInterval(this.timer); <BR>this.timer = null; <BR>}; <br><br></script> <br><script type="text/javascript"> <BR>function de() { <BR>var animation = new ImageLoop("loop", 1, ["img/img_01.jpg", "img/img_02.jpg",]); <BR>var sta = document.getElementById("sta"); <BR>var stp = document.getElementById("stp"); <BR>sta.onclick = function () { <BR>animation.start(); <BR>} <BR>stp.onclick = function () { <BR>animation.stop(); <BR>} <BR>} <BR>window.onload = function () { <BR>de(); <BR>} <BR></script> <br> <br> <br><img src="img/img_01.jpg" id="loop" alt="" title=""> <br><a href="#" class="de" id="sta">Start</a> <br><a href="#" class="de" id="stp">Stop</a> <br> <br> <br> </div>