职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 763|回复: 5

js动画

[复制链接]
紫衿 发表于 2011-7-12 09:22 | 显示全部楼层 |阅读模式
最近写了一个动画效果。
例子中我只写了一个效果,增加高度,有兴趣的朋友可以自己在添加其他的效果。
  1. function Animation(obj){   
  2.     this.ele = obj.ele;   
  3.     this.maxheight = obj.maxheight||26;   
  4.     this.minheight = obj.minheight||0;   
  5.     this.delay = obj.delay || 0;   
  6.       
  7.     this.inter = null;   
  8.     this.time = obj.time||200;   
  9.     this.callback = obj.callback;   
  10.     this.type = obj.type || "addh";   
  11.     this.initialize.apply(this, arguments);   
  12. };   
  13.   
  14. Animation.prototype={   
  15.     constructor:Animation,   
  16.     initialize:function(obj){   
  17.         this.events = [];//动画状态数组   
  18.         this.inittime = this.time;   
  19.     },   
  20.     start:function(){   
  21.         this.events.push(this.type);   
  22.         /*  
  23.         判断主循环 是否在执行 在执行的话就 只保存需要执行的状态   
  24.         这样就可以保证一个dom只有一个 主循环 从而保证不会出现死循环  
  25.         */  
  26.         if(this.inter == null){   
  27.             this.startAnimate();   
  28.         }   
  29.     },   
  30.     startAnimate:function(){   
  31.         this.time = this.inittime;   
  32.         var t = this;   
  33.         var type = t.events.pop();//获取最后一个动画   
  34.         t.events = [];   
  35.         //主循环   
  36.         t.inter = setInterval(function(){   
  37.             switch(type){   
  38.                 case "addh":   
  39.                     t.addh();   
  40.                 break;   
  41.                 case "delh":   
  42.                     t.delh();   
  43.                 break;   
  44.             }   
  45.         },50);   
  46.     },   
  47.     callBack:function(){   
  48.         var len = this.events.length;   
  49.         clearInterval(this.inter);   
  50.         var t = this;   
  51.         if(len == 0){   
  52.             this.inter = null;   
  53.             if(typeof this.callback == "function"){   
  54.                 setTimeout(function(){   
  55.                     t.callback();   
  56.                 },this.delay);   
  57.             }   
  58.         }   
  59.         else{   
  60.             setTimeout(function(){   
  61.                 t.startAnimate();   
  62.             },this.delay);   
  63.         }   
  64.     },   
  65.     addh:function(){   
  66.         var h = this.ele.height();   
  67.         var nh = Math.min(h+((this.time*this.time)/1000000)*10,this.maxheight);   
  68.         if(nh >= this.maxheight){   
  69.             this.callBack();   
  70.         }   
  71.         this.ele.height(nh);   
  72.         this.time += 1000;   
  73.     },   
  74.     delh:function(){   
  75.         var h = this.ele.height();   
  76.         var nh = Math.max(h-((this.time*this.time)/1000000)*10, this.minheight);   
  77.         if(nh <= this.minheight){   
  78.             this.callBack();   
  79.         }   
  80.         this.ele.height(nh);   
  81.         this.time += 1000;   
  82.     }   
  83. };  

  84. function Animation(obj){
  85.         this.ele = obj.ele;
  86.         this.maxheight = obj.maxheight||26;
  87.         this.minheight = obj.minheight||0;
  88.         this.delay = obj.delay || 0;
  89.        
  90.         this.inter = null;
  91.         this.time = obj.time||200;
  92.         this.callback = obj.callback;
  93.         this.type = obj.type || "addh";
  94.         this.initialize.apply(this, arguments);
  95. };

  96. Animation.prototype={
  97.         constructor:Animation,
  98.         initialize:function(obj){
  99.                 this.events = [];//动画状态数组
  100.                 this.inittime = this.time;
  101.         },
  102.         start:function(){
  103.                 this.events.push(this.type);
  104.                 /*
  105.                 判断主循环 是否在执行 在执行的话就 只保存需要执行的状态  
  106.                 这样就可以保证一个dom只有一个 主循环 从而保证不会出现死循环
  107.                 */
  108.                 if(this.inter == null){
  109.                         this.startAnimate();
  110.                 }
  111.         },
  112.         startAnimate:function(){
  113.                 this.time = this.inittime;
  114.                 var t = this;
  115.                 var type = t.events.pop();//获取最后一个动画
  116.                 t.events = [];
  117.                 //主循环
  118.                 t.inter = setInterval(function(){
  119.                         switch(type){
  120.                                 case "addh":
  121.                                         t.addh();
  122.                                 break;
  123.                                 case "delh":
  124.                                         t.delh();
  125.                                 break;
  126.                         }
  127.                 },50);
  128.         },
  129.         callBack:function(){
  130.                 var len = this.events.length;
  131.                 clearInterval(this.inter);
  132.                 var t = this;
  133.                 if(len == 0){
  134.                         this.inter = null;
  135.                         if(typeof this.callback == "function"){
  136.                                 setTimeout(function(){
  137.                                         t.callback();
  138.                                 },this.delay);
  139.                         }
  140.                 }
  141.                 else{
  142.                         setTimeout(function(){
  143.                                 t.startAnimate();
  144.                         },this.delay);
  145.                 }
  146.         },
  147.         addh:function(){
  148.                 var h = this.ele.height();
  149.                 var nh = Math.min(h+((this.time*this.time)/1000000)*10,this.maxheight);
  150.                 if(nh >= this.maxheight){
  151.                         this.callBack();
  152.                 }
  153.                 this.ele.height(nh);
  154.                 this.time += 1000;
  155.         },
  156.         delh:function(){
  157.                 var h = this.ele.height();
  158.                 var nh = Math.max(h-((this.time*this.time)/1000000)*10, this.minheight);
  159.                 if(nh <= this.minheight){
  160.                         this.callBack();
  161.                 }
  162.                 this.ele.height(nh);
  163.                 this.time += 1000;
  164.         }
  165. };
复制代码
上面是基本代码
具体例子
  1. <div style="border:1px solid red;width:100px;" id="div1"  >   
  2. <div style="border:1px solid green;width:50px;margin:auto;" id="div2" ></div>   
  3. </div>   
  4. <input type="button" value="同时动" id="btn1" />   
  5. <input type="button" value="分开动" id="btn2" />   
  6.   
  7. <script type="text/javascript" >   
  8. $(function(){   
  9.     var div1 = new Animation({   
  10.         ele:$("#div1"),   
  11.         maxheight:200,   
  12.         minheight:0,   
  13.         delay:0  
  14.     });   
  15.     var div2 = new Animation({   
  16.         ele:$("#div2"),   
  17.         maxheight:100,   
  18.         minheight:0,   
  19.         delay:5000  
  20.     });   
  21.       
  22.     $("#btn1").click(function(){   
  23.         div1.type="addh";   
  24.         div1.start();   
  25.         div2.type="addh";   
  26.         div2.start();   
  27.     });   
  28.     $("#btn2").click(function(){   
  29.         div1.type="addh";   
  30.         div1.start();   
  31.         div1.callback=function(){   
  32.             div2.type="addh";   
  33.             div2.start();   
  34.         };   
  35.     });   
  36. });   
  37. </script>  

  38. <div style="border:1px solid red;width:100px;" id="div1"  >
  39. <div style="border:1px solid green;width:50px;margin:auto;" id="div2" ></div>
  40. </div>
  41. <input type="button" value="同时动" id="btn1" />
  42. <input type="button" value="分开动" id="btn2" />

  43. <script type="text/javascript" >
  44. $(function(){
  45.         var div1 = new Animation({
  46.                 ele:$("#div1"),
  47.                 maxheight:200,
  48.                 minheight:0,
  49.                 delay:0
  50.         });
  51.         var div2 = new Animation({
  52.                 ele:$("#div2"),
  53.                 maxheight:100,
  54.                 minheight:0,
  55.                 delay:5000
  56.         });
  57.        
  58.         $("#btn1").click(function(){
  59.                 div1.type="addh";
  60.                 div1.start();
  61.                 div2.type="addh";
  62.                 div2.start();
  63.         });
  64.         $("#btn2").click(function(){
  65.                 div1.type="addh";
  66.                 div1.start();
  67.                 div1.callback=function(){
  68.                         div2.type="addh";
  69.                         div2.start();
  70.                 };
  71.         });
  72. });
  73. </script>
复制代码
为了方便我在代码中 使用了jquery 。大家需要 引入jquery框架


走失的猫咪 发表于 2011-7-12 09:22 | 显示全部楼层
推荐链接
年薪100万诚邀IT讲师
【推荐】java 新手是如何获得“8K月薪”的?

爱车车 发表于 2011-7-22 10:32 | 显示全部楼层
我顶啊。接着顶
fossil 发表于 2011-7-26 18:54 | 显示全部楼层
我的我的 忘记了 呵呵
天上智喜 发表于 2011-7-26 18:55 | 显示全部楼层
我在q上和你说话
找不到我 发表于 2011-7-27 09:57 | 显示全部楼层
原来还有这么多内幕啊,长见识了,呵呵
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-19 19:54 , Processed in 0.150848 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表