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 相关文章推荐
Js+XML 操作
Sep 20 Javascript
解决jquery .ajax 在IE下卡死问题的解决方法
Oct 26 Javascript
基于JQuery框架的AJAX实例代码
Nov 03 Javascript
js打印纸函数代码(递归)
Jun 18 Javascript
js showModalDialog参数的使用详解
Jan 07 Javascript
超简单JS二级、多级联动的简单实例
Feb 18 Javascript
jquery 显示*天*时*分*秒实现时间计时器
May 07 Javascript
详解Matlab中 sort 函数用法
Mar 20 Javascript
JQuery异步提交表单与文件上传功能示例
Jan 12 Javascript
Vue 实现手动刷新组件的方法
Feb 19 Javascript
Javascript实现鼠标移入方向感知
Jun 24 Javascript
vue.js实现点击图标放大离开时缩小的代码
Jan 27 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侧拉菜单 漂亮,可以向右或者向左展开,支持FF,IE
2009/10/15 PHP
PHP实现多条件查询实例代码
2010/07/17 PHP
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
2012/01/16 PHP
PHP中将网页导出为Word文档的代码
2012/05/25 PHP
php日期转时间戳,指定日期转换成时间戳
2012/07/17 PHP
浅析51个PHP处理字符串的函数
2013/08/02 PHP
PHP将二维数组某一个字段相同的数组合并起来的方法
2016/02/26 PHP
php中static 静态变量和普通变量的区别
2016/12/01 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
PHP实现微信提现功能(微信商城)
2019/11/21 PHP
使用node.js半年来总结的 10 条经验
2014/08/18 Javascript
简介AngularJS中使用factory和service的方法
2015/06/17 Javascript
jQuery实现横向带缓冲的水平运动效果(附demo源码下载)
2016/01/29 Javascript
JavaScript中如何判断一个值的类型
2017/09/15 Javascript
vue中遇到的坑之变化检测问题(数组相关)
2017/10/13 Javascript
vue解决弹出蒙层滑动穿透问题的方法
2018/09/22 Javascript
vue 移动端适配方案详解
2018/11/15 Javascript
vue+SSM实现验证码功能
2018/12/07 Javascript
今天,小程序正式支持 SVG
2019/04/20 Javascript
AntV F2和vue-cli构建移动端可视化视图过程详解
2019/10/08 Javascript
原生js实现随机点餐效果
2019/12/10 Javascript
vue实现整屏滚动切换
2020/06/29 Javascript
浅谈Python中的私有变量
2018/02/28 Python
Python 按字典dict的键排序,并取出相应的键值放于list中的实例
2019/02/12 Python
python使用 zip 同时迭代多个序列示例
2019/07/06 Python
pytorch 数据处理:定义自己的数据集合实例
2019/12/31 Python
keras用auc做metrics以及早停实例
2020/07/02 Python
Django-Scrapy生成后端json接口的方法示例
2020/10/06 Python
Python制作简单的剪刀石头布游戏
2020/12/10 Python
8款使用 CSS3 实现超炫的 Loading(加载)的动画效果
2015/03/17 HTML / CSS
斯洛伐克香水和化妆品购物网站:Parfemy-Elnino.sk
2020/01/28 全球购物
Set里的元素是不能重复的,那么用什么方法来区分重复与否呢?
2016/08/18 面试题
2014年社区居委会主任重阳节讲话稿
2014/09/25 职场文书
新郎父母婚礼答谢词
2015/09/29 职场文书
继续教育心得体会(共6篇)
2016/01/19 职场文书
VW、VH适配移动端的解决方案与常见问题
2023/05/21 HTML / CSS