cecily.yu

          ~~~~~~~

          2009年9月22日 #

          js Tween算法 水果棟特效

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <head>
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
          <title>無標題文檔</title>
          <script type="text/javascript">
           var Tween = {
            Bounce: {
                  easeIn: function(t,b,c,d){
                      return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;
                  },
                  easeOut: function(t,b,c,d){
                      if ((t/=d) < (1/2.75)) {
                          return c*(7.5625*t*t) + b;
                      } else if (t < (2/2.75)) {
                          return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
                      } else if (t < (2.5/2.75)) {
                          return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
                      } else {
                          return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
                      }
                  },
                  easeInOut: function(t,b,c,d){
                      if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b;
                      else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b;
                  }
              }

           }
           b為初始值 c為變化值 d為持續(xù)時間 t為開始時間

          d要不配合與步長一起使用  例如

          這里的步長為3

          d×3 =c-b
           var b=-300,c=600,d=300,t=0;
           function run(){
            var div = document.getElementById("div1");
            div.style.left =  Math.ceil(Tween.Bounce.easeInOut(t,b,c,d))+"px";
            if(t<d){
             t+=3;
             setTimeout(run,10);
           
            }
           }

          </script>
          </head>

          <body onload="run();">
          <div style="width:1000px; height:1000px; position:relative; overflow:hidden; background-color:#9966CC">
            <div id="div1" style="width:300px; height:200px; background-color:#0099FF; position:absolute; left:-300px; top:300px;">
            </div>
           </div>
          </body>
          </html>

           

           

          完整Tween算法

          var Tween = {

              Linear: function(t,b,c,d){ return c*t/d + b; },

              Quad: {

                  easeIn: function(t,b,c,d){

                      return c*(t/=d)*t + b;

                  },

                  easeOut: function(t,b,c,d){

                      return -c *(t/=d)*(t-2) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      if ((t/=d/2) < 1) return c/2*t*t + b;

                      return -c/2 * ((--t)*(t-2) - 1) + b;

                  }

              },

              Cubic: {

                  easeIn: function(t,b,c,d){

                      return c*(t/=d)*t*t + b;

                  },

                  easeOut: function(t,b,c,d){

                      return c*((t=t/d-1)*t*t + 1) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      if ((t/=d/2) < 1) return c/2*t*t*t + b;

                      return c/2*((t-=2)*t*t + 2) + b;

                  }

              },

              Quart: {

                  easeIn: function(t,b,c,d){

                      return c*(t/=d)*t*t*t + b;

                  },

                  easeOut: function(t,b,c,d){

                      return -c * ((t=t/d-1)*t*t*t - 1) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      if ((t/=d/2) < 1) return c/2*t*t*t*t + b;

                      return -c/2 * ((t-=2)*t*t*t - 2) + b;

                  }

              },

              Quint: {

                  easeIn: function(t,b,c,d){

                      return c*(t/=d)*t*t*t*t + b;

                  },

                  easeOut: function(t,b,c,d){

                      return c*((t=t/d-1)*t*t*t*t + 1) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;

                      return c/2*((t-=2)*t*t*t*t + 2) + b;

                  }

              },

              Sine: {

                  easeIn: function(t,b,c,d){

                      return -c * Math.cos(t/d * (Math.PI/2)) + c + b;

                  },

                  easeOut: function(t,b,c,d){

                      return c * Math.sin(t/d * (Math.PI/2)) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;

                  }

              },

              Expo: {

                  easeIn: function(t,b,c,d){

                      return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;

                  },

                  easeOut: function(t,b,c,d){

                      return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      if (t==0) return b;

                      if (t==d) return b+c;

                      if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;

                      return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;

                  }

              },

              Circ: {

                  easeIn: function(t,b,c,d){

                      return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;

                  },

                  easeOut: function(t,b,c,d){

                      return c * Math.sqrt(1 - (t=t/d-1)*t) + b;

                  },

                  easeInOut: function(t,b,c,d){

                      if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;

                      return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;

                  }

              },

              Elastic: {

                  easeIn: function(t,b,c,d,a,p){

                      if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;

                      if (!a || a < Math.abs(c)) { a=c; var s=p/4; }

                      else var s = p/(2*Math.PI) * Math.asin (c/a);

                      return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;

                  },

                  easeOut: function(t,b,c,d,a,p){

                      if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;

                      if (!a || a < Math.abs(c)) { a=c; var s=p/4; }

                      else var s = p/(2*Math.PI) * Math.asin (c/a);

                      return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);

                  },

                  easeInOut: function(t,b,c,d,a,p){

                      if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);

                      if (!a || a < Math.abs(c)) { a=c; var s=p/4; }

                      else var s = p/(2*Math.PI) * Math.asin (c/a);

                      if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;

                      return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;

                  }

              },

              Back: {

                  easeIn: function(t,b,c,d,s){

                      if (s == undefined) s = 1.70158;

                      return c*(t/=d)*t*((s+1)*t - s) + b;

                  },

                  easeOut: function(t,b,c,d,s){

                      if (s == undefined) s = 1.70158;

                      return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;

                  },

                  easeInOut: function(t,b,c,d,s){

                      if (s == undefined) s = 1.70158; 

                      if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;

                      return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;

                  }

              },

              Bounce: {

                  easeIn: function(t,b,c,d){

                      return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;

                  },

                  easeOut: function(t,b,c,d){

                      if ((t/=d) < (1/2.75)) {

                          return c*(7.5625*t*t) + b;

                      } else if (t < (2/2.75)) {

                          return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;

                      } else if (t < (2.5/2.75)) {

                          return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;

                      } else {

                          return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;

                      }

                  },

                  easeInOut: function(t,b,c,d){

                      if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b;

                      else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b;

                  }

              }

          }

           

          參考網址http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html

          posted @ 2009-09-22 09:48 郁 閱讀(764) | 評論 (0)編輯 收藏

          僅列出標題  

          我的郵箱地址:cecily.yu@163.com

          我的QQ主頁
          主站蜘蛛池模板: 分宜县| 府谷县| 晋宁县| 青铜峡市| 大埔区| 汉川市| 汽车| 荥经县| 巴彦县| 当阳市| 海原县| 屯留县| 陆河县| 六盘水市| 庄浪县| 卢氏县| 安宁市| 凌云县| 韩城市| 永川市| 邢台市| 和龙市| 和林格尔县| 灵山县| 镇平县| 台州市| 龙江县| 略阳县| 十堰市| 阿瓦提县| 福贡县| 松阳县| 峨边| 济宁市| 皋兰县| 东辽县| 灌阳县| 阿坝县| 巨鹿县| 灵石县| 靖安县|