JavaScript 图像动画的小demo


Posted in Javascript onMay 23, 2012
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<title>图形动画</title> 
<style type="text/css"> 
.de{ font-size:30px; text-decoration:none; font-family:微软雅黑; color:#ccc;} 
.de:hover{ color:#933;} 
</style> 
<script type="text/javascript"> 
/** 
* ImageLoop.js: An ImageLoop class for performing image animations 
* 
* Constructor Arguments: 
* imageId: the id of the <img> tag which will be animated 
* fps: the number of frames to display per second 
* frameURLs: an array of URLs, one for each frame of the animation 
* 
* Public Methods: 
* start(): start the animation (but wait for all frames to load first) 
* stop(): stop the animation 
* 
* Public Properties: 
* loaded: true if all frames of the animation have loaded, 
* false otherwise 
*/ 
function ImageLoop(imageId, fps, frameURLs) { 
// Remember the image id. Don't look it up yet since this constructor 
// may be called before the document is loaded. 
this.imageId = imageId; 
// Compute the time to wait between frames of the animation 
this.frameInterval = 1000 / fps; 
// An array for holding Image objects for each frame 
this.frames = new Array(frameURLs.length); this.image = null; // The <img> element, looked up by id 
this.loaded = false; // Whether all frames have loaded 
this.loadedFrames = 0; // How many frames have loaded 
this.startOnLoad = false; // Start animating when done loading? 
this.frameNumber = -1; // What frame is currently displayed 
this.timer = null; // The return value of setInterval() 
// Initialize the frames[] array and preload the images 
for (var i = 0; i < frameURLs.length; i++) { 
this.frames[i] = new Image(); // Create Image object 
// Register an event handler so we know when the frame is loaded 
this.frames[i].onload = countLoadedFrames; // defined later 
this.frames[i].src = frameURLs[i]; // Preload the frame's image 
} 
// This nested function is an event handler that counts how many 
// frames have finished loading. When all are loaded, it sets a flag, 
// and starts the animation if it has been requested to do so. 
var loop = this; 
function countLoadedFrames() { 
loop.loadedFrames++; 
if (loop.loadedFrames == loop.frames.length) { 
loop.loaded = true; 
if (loop.startOnLoad) loop.start(); 
} 
} 
// Here we define a function that displays the next frame of the 
// animation. This function can't be an ordinary instance method because 
// setInterval() can only invoke functions, not methods. So we make 
// it a closure that includes a reference to the ImageLoop object 
this._displayNextFrame = function () { 
// First, increment the frame number. The modulo operator (%) means 
// that we loop from the last to the first frame 
loop.frameNumber = (loop.frameNumber + 1) % loop.frames.length; 
// Update the src property of the image to the URL of the new frame 
loop.image.src = loop.frames[loop.frameNumber].src; 
}; 
} 
/** 
* This method starts an ImageLoop animation. If the frame images have not 
* finished loading, it instead sets a flag so that the animation will 
* automatically be started when loading completes 
*/ 
ImageLoop.prototype.start = function () { 
if (this.timer != null) return; // Already started 
// If loading is not complete, set a flag to start when it is 
if (!this.loaded) this.startOnLoad = true; 
else { 
// If we haven't looked up the image by id yet, do so now 
if (!this.image) this.image = document.getElementById(this.imageId); 
// Display the first frame immediately 
this._displayNextFrame(); 
// And set a timer to display subsequent frames 
this.timer = setInterval(this._displayNextFrame, this.frameInterval); 
} 
}; 
/** Stop an ImageLoop animation */ 
ImageLoop.prototype.stop = function () { 
if (this.timer) clearInterval(this.timer); 
this.timer = null; 
}; 
</script> 
<script type="text/javascript"> 
function de() { 
var animation = new ImageLoop("loop", 1, ["img/img_01.jpg", "img/img_02.jpg",]); 
var sta = document.getElementById("sta"); 
var stp = document.getElementById("stp"); 
sta.onclick = function () { 
animation.start(); 
} 
stp.onclick = function () { 
animation.stop(); 
} 
} 
window.onload = function () { 
de(); 
} 
</script> 
</head> 
<body> 
<img src="img/img_01.jpg" id="loop" alt="" title="" /> 
<a href="#" class="de" id="sta">Start</a> 
<a href="#" class="de" id="stp">Stop</a> 
</body> 
</html>
Javascript 相关文章推荐
Extjs ajax同步请求时post方式参数发送方式
Aug 05 Javascript
JavaScript的public、private和privileged模式
Dec 28 Javascript
快速排序 php与javascript的不同之处
Feb 22 Javascript
jquery随机展示头像代码
Dec 21 Javascript
js Dialog 去掉右上角的X关闭功能
Apr 23 Javascript
js+html5获取用户地理位置信息并在Google地图上显示的方法
Jun 05 Javascript
js中window.open的参数及注意注意事项
Jul 06 Javascript
JS中Attr的用法详解
Oct 09 Javascript
PHP自动加载autoload和命名空间的应用小结
Dec 01 Javascript
微信小程序自定义可滑动日历界面
Dec 28 Javascript
详解小程序开发经验:多页面数据同步
May 18 Javascript
一些可能会用到的Node.js面试题
Jun 15 Javascript
JavaScript学习笔记记录我的旅程
May 23 #Javascript
JS中处理与当前时间间隔的函数代码
May 23 #Javascript
自己做的模拟模态对话框实现代码
May 23 #Javascript
解决jquery的datepicker的本地化以及Today问题
May 23 #Javascript
{}与function(){}选用空对象{}来存放keyValue
May 23 #Javascript
JavaScript基本编码模式小结
May 23 #Javascript
Javascript处理DOM元素事件实现代码
May 23 #Javascript
You might like
php实现网站插件机制的方法
2009/11/10 PHP
PHP实现在线阅读PDF文件的方法
2015/06/17 PHP
利用PHP生成静态html页面的原理
2016/09/30 PHP
php封装的smarty类完整实例
2016/10/19 PHP
PHP API接口必备之输出json格式数据示例代码
2017/06/27 PHP
js创建数据共享接口——简化框架之间相互传值
2011/10/23 Javascript
如何使用jQuery来处理图片坏链具体实现步骤
2013/05/02 Javascript
Javascript 按位取反运算符 (~)
2014/02/04 Javascript
javascript的alert box在java中如何显示多行
2014/05/18 Javascript
jquery 中的each()跳出循环的语句
2014/05/23 Javascript
javascript中的return和闭包函数浅析
2014/06/06 Javascript
jQuery前端框架easyui使用Dialog时bug处理
2014/12/05 Javascript
Javascript 拖拽的一些高级的应用(逐行分析代码,让你轻松了拖拽的原理)
2015/01/23 Javascript
vuex实现简易计数器
2016/10/27 Javascript
浅谈javascript中的 “ &amp;&amp; ” 和 “ || ”
2017/02/02 Javascript
javascript笔记之匿名函数和闭包
2017/02/06 Javascript
详解angular element()方法使用
2017/04/08 Javascript
vue跨域解决方法
2017/10/15 Javascript
vue集成openlayers加载geojson并实现点击弹窗教程
2020/09/24 Javascript
使用PyV8在Python爬虫中执行js代码
2017/02/16 Python
Python向MySQL批量插数据的实例讲解
2018/03/31 Python
使用pycharm生成代码模板的实例
2018/05/23 Python
python中将zip压缩包转为gz.tar的方法
2018/10/18 Python
Python 20行简单实现有道在线翻译的详解
2019/05/15 Python
在windows下使用python进行串口通讯的方法
2019/07/02 Python
Python爬虫使用代理IP的实现
2019/10/27 Python
在python下实现word2vec词向量训练与加载实例
2020/06/09 Python
python 如何停止一个死循环的线程
2020/11/24 Python
美国最大的团购网站:Groupon
2016/07/23 全球购物
Expedia马来西亚旅游网站:廉价酒店,度假村和航班预订
2016/07/26 全球购物
英国家居用品和家居装饰品购物网站:Cox & Cox
2019/08/25 全球购物
环保建议书
2014/03/12 职场文书
《北京的春节》教学反思
2014/04/07 职场文书
2014最新党员违纪检讨书
2014/10/12 职场文书
就业意向协议书
2015/01/29 职场文书
Java练习之潜艇小游戏的实现
2022/03/16 Java/Android