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 相关文章推荐
onsubmit阻止form表单提交与onclick的相关操作
Sep 03 Javascript
StringTemplate遇见jQuery冲突的解决方法
Sep 22 Javascript
javascript解析json实例详解
Nov 05 Javascript
JavaScript高级教程5.6之基本包装类型(详细)
Nov 23 Javascript
JavaScript模拟鼠标右键菜单效果
Dec 08 Javascript
jquery简单插件制作(fn.extend)完整实例
May 24 Javascript
Bootstrap缩略图与警告框学习使用
Feb 08 Javascript
Vue.2.0.5过渡效果使用技巧
Mar 16 Javascript
JS实现简单的选择题测评系统代码思路详解(demo)
Sep 03 Javascript
node thread.sleep实现示例
Jun 20 Javascript
vue两组件间值传递 $router.push实现方法
May 15 Javascript
js模拟F11页面全屏显示
Sep 17 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
预告映像公开!第1章续篇剧场版动画《Princess Principal Crown Handler》4月10日上映!
2020/03/06 日漫
浅谈apache和nginx的rewrite的区别
2013/02/22 PHP
PHP中使用xmlreader读取xml数据示例
2014/12/29 PHP
PHP如何将log信息写入服务器中的log文件
2015/07/29 PHP
微信获取用户地理位置信息的原理与步骤
2015/11/12 PHP
PHP使用Pear发送邮件(Windows环境)
2016/01/05 PHP
PHP实现批量重命名某个文件夹下所有文件的方法
2017/09/04 PHP
Laravel使用RabbitMQ的方法示例
2019/06/18 PHP
Laravel中如何轻松容易的输出完整的SQL语句
2020/07/26 PHP
在 IE 中调用 javascript 打开 Excel 表
2006/12/21 Javascript
javascript 多种搜索引擎集成的页面实现代码
2010/01/02 Javascript
推荐 21 款优秀的高性能 Node.js 开发框架
2014/08/18 Javascript
JavaScript图片轮播代码分享
2015/07/31 Javascript
浅谈js的html元素的父节点,子节点
2016/08/06 Javascript
微信小程序购物商城系统开发系列-工具篇的介绍
2016/11/21 Javascript
vue2组件实现懒加载浅析
2017/03/29 Javascript
Angular directive递归实现目录树结构代码实例
2017/05/05 Javascript
layui框架与SSM前后台交互的方法
2019/09/12 Javascript
在Python的Django框架中使用通用视图的方法
2015/07/21 Python
python使用super()出现错误解决办法
2017/08/14 Python
解决Django中修改js css文件但浏览器无法及时与之改变的问题
2019/08/31 Python
GDAL 矢量属性数据修改方式(python)
2020/03/10 Python
Pygame框架实现飞机大战
2020/08/07 Python
专门出售各种儿童读物的网站:Put Me In The Story
2016/08/07 全球购物
JD Sports意大利:英国篮球和运动时尚的领导者
2017/10/29 全球购物
Wedgwood美国官网:英国骨瓷,精美礼品及家居装饰
2018/02/17 全球购物
编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串
2014/01/07 面试题
出纳岗位职责模板
2013/11/27 职场文书
食堂员工工作职责
2013/12/18 职场文书
优秀毕业生求职信范文
2014/01/02 职场文书
大学生志愿者感言
2014/01/15 职场文书
党旗在我心中演讲稿
2014/09/15 职场文书
2014医学院领导干部四风对照检查材料思想汇报
2014/09/16 职场文书
2014年妇产科工作总结
2014/12/08 职场文书
2016应届毕业生实习评语
2015/12/01 职场文书
感谢信
2019/04/11 职场文书