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 相关文章推荐
分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
Sep 21 Javascript
Javascript selection的兼容性写法介绍
Dec 20 Javascript
JavaScript cookie的设置获取删除详解
Feb 11 Javascript
jquery动态分页效果堪比时光网
Sep 25 Javascript
JQuery基础语法小结
Feb 27 Javascript
Javascript中的包装类型介绍
Apr 02 Javascript
设置jQueryUI DatePicker默认语言为中文
Jun 04 Javascript
AngularJS监听路由变化的方法
Mar 07 Javascript
AngularJS $http模块POST请求实现
Apr 08 Javascript
使用JS判断移动端手机横竖屏状态
Jul 30 Javascript
如何使用CocosCreator对象池
Apr 14 Javascript
vue实现拖拽交换位置
Apr 07 Vue.js
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银联网页支付实现方法
2015/03/04 PHP
PHP实现阳历到农历转换的类实例
2015/03/07 PHP
php操作MongoDB类实例
2015/06/17 PHP
yii框架搜索分页modle写法
2016/12/19 PHP
Javascript &amp; DHTML 实例编程(教程)基础知识
2007/06/02 Javascript
用js实现手把手教你月入万刀(转贴)
2007/11/07 Javascript
JS+CSS实现表格高亮的方法
2015/08/05 Javascript
JavaScript获取各大浏览器信息图示
2015/11/20 Javascript
基于MVC5和Bootstrap的jQuery TreeView树形控件(一)之数据支持json字符串、list集合
2016/08/11 Javascript
微信小程序模板之分页滑动栏
2017/02/10 Javascript
angular.js指令中transclude选项及ng-transclude指令详解
2017/05/24 Javascript
微信小程序开发之实现自定义Toast弹框
2017/06/08 Javascript
Vue2路由动画效果的实现代码
2017/07/10 Javascript
js实现1,2,3,5数字按照概率生成
2017/09/12 Javascript
nodejs socket服务端和客户端简单通信功能
2017/09/14 NodeJs
原生JS实现图片懒加载之页面性能优化
2019/04/26 Javascript
微信小程序批量上传图片到七牛(推荐)
2019/12/19 Javascript
再也不怕 JavaScript 报错了,怎么看怎么处理都在这儿
2020/12/09 Javascript
Python获取Windows或Linux主机名称通用函数分享
2014/11/22 Python
详细解读Python的web.py框架下的application.py模块
2015/05/02 Python
python Django模板的使用方法
2016/01/14 Python
利用Python进行数据可视化常见的9种方法!超实用!
2018/07/11 Python
Python实现的插入排序,冒泡排序,快速排序,选择排序算法示例
2019/05/04 Python
python图像和办公文档处理总结
2019/05/28 Python
python binascii 进制转换实例
2019/06/12 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
2020/05/15 Python
纯CSS3实现自定义Tooltip边框涂鸦风格的教程
2014/11/05 HTML / CSS
CSS3 @keyframes简单动画实现
2018/02/24 HTML / CSS
canvas实现二维码和图片合成的示例代码
2018/08/01 HTML / CSS
世界各地的当地人的食物体验:Eatwith
2019/07/26 全球购物
大三自我鉴定范文
2013/10/05 职场文书
最新的互联网创业计划书
2014/01/10 职场文书
《值日生》教学反思
2014/02/17 职场文书
反四风对照检查材料
2014/09/22 职场文书
2016年寒假学习心得体会
2015/10/09 职场文书
15个值得收藏的JavaScript函数
2021/09/15 Javascript