乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      javascript中的時間版運動

       quasiceo 2017-08-21

      前面的話

        速度版JS運動是指以速度為參照,隨著路程的變化,時間隨之變化;而時間版JS運動是指以時間為參照,隨著路程的變化,速度隨著變化。相較而言,時間版JS運動更為常用。JQ的animate就是時間版運動。本文將詳細(xì)介紹時間版JS運動

       

      速度版運動

        為何速度版JS更容易理解呢?這要歸功于定時器setInterval了。最容易想到的運行形式如下所示

      setInterval(function(){
          s = s + step
      },30)

        每30ms,路程增加step,實際上就決定了以速度為參照。而step的值如何變化就決定了何種運動形式。以最簡單的勻速運動為例

      復(fù)制代碼
      <button id="btn">開始運動</button>
      <button id="reset">還原</button>
      <div id="test" style="height: 100px;width: 100px;position:absolute;left:0;"></div>
      <script>
      var timer;
      reset.onclick = function(){history.go();}
      btn.onclick = function(){
          timer = setInterval(function(){
              if(test.offsetLeft < 500){
                  test.style.left = test.offsetLeft + 10 + 'px';
              }else{
                  test.style.left = '500px';
                  clearInterval(timer);
              }    
          },30);
      }
      </script>
      復(fù)制代碼

        總路程s為500,每30ms,向前移動10,相當(dāng)于速度為1000/3=333.3。最終計算得出花費時間t=s/v=500/333.3=1.5s

        當(dāng)路程發(fā)生變化時,仍然以333.3的速度運動,時間也發(fā)生相應(yīng)的變化。這就是速度版運動

       

      公式推導(dǎo)

        下面來介紹時間版運動,以勻速運動為例,先假設(shè)幾個變量

      復(fù)制代碼
      距離 c(change position)
      初始位置 b(beginning position)
      最終位置 p(position)
      持續(xù)時間 d(duration)
      時間 t(time)
      速度 v(velocity)
      復(fù)制代碼

        上面幾個變量有如下等式

        1、最終運動距離 = 最終位置 - 初始位置

      c = p - b

        2、最終運動距離 = 速度 * 持續(xù)時間

      c = v * d

        3、當(dāng)前運動距離 = 當(dāng)前位置 - 初始位置

      c(當(dāng)前) =  p(當(dāng)前) - b

        4、當(dāng)前運動距離 = 速度 * 時間

      c(當(dāng)前) = v * t

        最終要表示為如下函數(shù)

      p(當(dāng)前) = ?(t)

        因此,經(jīng)過整理得出公式如下

      p(當(dāng)前) = b + c(當(dāng)前) = b + v*t = b + c*t/d

        最終結(jié)果為

      p = t * c / d + b

       

      勻速函數(shù)

        下面將時間版勻速運動封裝為一個名稱為linearMove.js的文件

      復(fù)制代碼
      if (!window.requestAnimationFrame) {
          requestAnimationFrame = function(fn) {
              setTimeout(fn, 17);
          };    
      }
      if (!window.cancelAnimationFrame) {
          window.cancelAnimationFrame = function(id) {
              clearTimeout(id);
          };
      }
      function getCSS(obj,style){
          if(window.getComputedStyle){
              return getComputedStyle(obj)[style];
          }
          return obj.currentStyle[style];
      } 
      
      function linearMove(obj,json,times,fn){
        //獲取當(dāng)前毫秒數(shù)
        var startTime = +new Date();  
        /*獲取初始值*/
        var iCur = {};
        for(var attr in json){
          if(attr == 'opacity'){
            //對當(dāng)前值的取值進(jìn)行四舍五入,去除由于javascript小數(shù)計數(shù)中的bug存在的小尾巴
            iCur[attr] = Math.round(getCSS(obj,attr)*100);
          }else{
            //去掉單位
            iCur[attr] = parseInt(getCSS(obj,attr));
          }
        }
        //清除定時器
        cancelAnimationFrame(obj.timer);
        obj.timer = requestAnimationFrame(function func(){
          //獲取t、d參數(shù)
          var d = times;
          var t = d - Math.max(0,startTime - (+new Date()) + d);
          for(var attr in json){
            /*獲取b、c、p這三個參數(shù)*/
            var b = iCur[attr];
            var c = json[attr]-iCur[attr];
            var p = t * ( c / d ) + b;
            /*賦值操作*/
            if(attr == 'opacity'){
              obj.style.opacity = p / 100;
              obj.style.filter = 'alpha(opacity=' + p + ')';
            }else{
              obj.style[attr] = p + 'px';
            }  
          }
            obj.timer = requestAnimationFrame(func);
            /*運行指定時間后*/
            if(t == d){
              //清除定時器
              cancelAnimationFrame(obj.timer);
              //設(shè)置回調(diào)函數(shù)
              fn && fn.call(obj);        
            }    
        });
      } 
      復(fù)制代碼

        下面調(diào)用自己封裝的linearMove.js來制作一個實例

      復(fù)制代碼
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>Document</title>
      </head>
      <body>
      <button id="btn">開始運動</button>
      <button id="reset">還原</button>
      <div id="test1" style="height: 100px;width: 100px;background: pink;position:absolute;left:0;"></div>
      <div id="test2" style="height: 100px;width: 100px;background: blue;position:absolute;top:250px;left:0;"></div>
      <script src="http://files.cnblogs.com/files/xiaohuochai/linearMove.js"></script>
      <script>
      reset.onclick = function(){history.go();}
      btn.onclick = function(){
         linearMove(test1,{width:400,height:200,left:100},500,function(){
            linearMove(test2,{width:300},500);
         });
         linearMove(test2,{left:200},500)
      }
      </script>
      </body>
      </html>
      復(fù)制代碼

       

      Tween算法

        Tween是一個來自flash的運動算法,包含各種經(jīng)典的動畫運動公式,詳細(xì)列表如下

      復(fù)制代碼
      Linear:線性勻速運動效果;
      Quadratic(Quad):二次方的緩動(t^2);
      Cubic:三次方的緩動(t^3);
      Quartic(Quart):四次方的緩動(t^4);
      Quintic(Quint):五次方的緩動(t^5);
      Sinusoidal(Sine):正弦曲線的緩動(sin(t));
      Exponential(Expo):指數(shù)曲線的緩動(2^t);
      Circular(Circ):圓形曲線的緩動(sqrt(1-t^2));
      Elastic:指數(shù)衰減的正弦曲線緩動;
      Back:超過范圍的三次方緩動((s+1)*t^3 – s*t^2);
      Bounce:指數(shù)衰減的反彈緩動。
      復(fù)制代碼

        每個效果都分三個緩動方式,分別是

      easeIn:從0開始加速的緩動,也就是先慢后快;
      easeOut:減速到0的緩動,也就是先快后慢;
      easeInOut:前半段從0開始加速,后半段減速到0的緩動。

        所有的這些緩動算法都離不開下面4個參數(shù),tbcd,含義如下:

      t  當(dāng)前時間 (time)
      b  初始位置 (beginning position)
      c  距離 (change position)
      d  持續(xù)時間 (duration)

        tween的詳細(xì)算法如下

      復(fù)制代碼
      // 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;
              }
          }
      };
      復(fù)制代碼

        tween算法的精簡版本如下所示

      復(fù)制代碼
      var Tween = {
          linear: function (t, b, c, d){  //勻速
              return c*t/d + b;
          },
          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;
          },
          easeBoth: function(t, b, c, d){  //加速減速曲線
              if ((t/=d/2) < 1) {
                  return c/2*t*t + b;
              }
              return -c/2 * ((--t)*(t-2) - 1) + b;
          },
          easeInStrong: function(t, b, c, d){  //加加速曲線
              return c*(t/=d)*t*t*t + b;
          },
          easeOutStrong: function(t, b, c, d){  //減減速曲線
              return -c * ((t=t/d-1)*t*t*t - 1) + b;
          },
          easeBothStrong: 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;
          },
          elasticIn: function(t, b, c, d, a, p){  //正弦衰減曲線(彈動漸入)
              if (t === 0) { 
                  return b; 
              }
              if ( (t /= d) == 1 ) {
                  return b+c; 
              }
              if (!p) {
                  p=d*0.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;
          },
          elasticOut: function(t, b, c, d, a, p){    //正弦增強曲線(彈動漸出)
              if (t === 0) {
                  return b;
              }
              if ( (t /= d) == 1 ) {
                  return b+c;
              }
              if (!p) {
                  p=d*0.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;
          },    
          elasticBoth: function(t, b, c, d, a, p){
              if (t === 0) {
                  return b;
              }
              if ( (t /= d/2) == 2 ) {
                  return b+c;
              }
              if (!p) {
                  p = d*(0.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 - 0.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 )*0.5 + c + b;
          },
          backIn: function(t, b, c, d, s){     //回退加速(回退漸入)
              if (typeof s == 'undefined') {
                 s = 1.70158;
              }
              return c*(t/=d)*t*((s+1)*t - s) + b;
          },
          backOut: function(t, b, c, d, s){
              if (typeof s == 'undefined') {
                  s = 3.70158;  //回縮的距離
              }
              return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
          }, 
          backBoth: function(t, b, c, d, s){
              if (typeof 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;
          },
          bounceIn: function(t, b, c, d){    //彈球減振(彈球漸出)
              return c - Tween['bounceOut'](d-t, 0, c, d) + b;
          },       
          bounceOut: 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 + 0.75) + b;
              } else if (t < (2.5/2.75)) {
                  return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
              }
              return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
          },      
          bounceBoth: function(t, b, c, d){
              if (t < d/2) {
                  return Tween['bounceIn'](t*2, 0, c, d) * 0.5 + b;
              }
              return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
          }
      }
      復(fù)制代碼

       

      JQ擴展

        默認(rèn)地, JQ只有兩種運動形式,包括linear和swing

      復(fù)制代碼
      jQuery.easing = {
          linear: function( p ) {
              return p;
          },
          swing: function( p ) {
              return 0.5 - Math.cos( p * Math.PI ) / 2;
          },
          _default: "swing"
      };
      復(fù)制代碼

        可以利用tween算法來擴展JQ的運動形式,JQ源碼中關(guān)于運動形式的函數(shù)如下所示

      this.pos = eased = jQuery.easing[ this.easing ](
                      percent, this.options.duration * percent, 0, 1, this.options.duration
                  );

        可以看到,它有5個參數(shù),分別對應(yīng)tween算法中的p、t、d、c、d,因此需要對tween算法中的參數(shù)進(jìn)行修改

      復(fù)制代碼
      <script src="http://cdn./jquery/1.12.4/jquery.min.js"></script>
      <script>
      ;(function($){
         $.extend(jQuery.easing,{
          linear: function (p,t, b, c, d){  //勻速
            return c*t/d + b;
          },
          easeIn: function(p,t, b, c, d){  //加速曲線
            return c*(t/=d)*t + b;
          },
          easeOut: function(p,t, b, c, d){  //減速曲線
            return -c *(t/=d)*(t-2) + b;
          },
          easeBoth: function(p,t, b, c, d){  //加速減速曲線
            if ((t/=d/2) < 1) {
              return c/2*t*t + b;
            }
            return -c/2 * ((--t)*(t-2) - 1) + b;
          },
          easeInStrong: function(p,t, b, c, d){  //加加速曲線
            return c*(t/=d)*t*t*t + b;
          },
          easeOutStrong: function(p,t, b, c, d){  //減減速曲線
            return -c * ((t=t/d-1)*t*t*t - 1) + b;
          },
          easeBothStrong: function(p,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;
          },
          elasticIn: function(p,t, b, c, d, a, p){  //正弦衰減曲線(彈動漸入)
            if (t === 0) { 
              return b; 
            }
            if ( (t /= d) == 1 ) {
              return b+c; 
            }
            if (!p) {
              p=d*0.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;
          },
          elasticOut: function(p,t, b, c, d, a, p){    //正弦增強曲線(彈動漸出)
            if (t === 0) {
              return b;
            }
            if ( (t /= d) == 1 ) {
              return b+c;
            }
            if (!p) {
              p=d*0.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;
          },    
          elasticBoth: function(p,t, b, c, d, a, p){
            if (t === 0) {
              return b;
            }
            if ( (t /= d/2) == 2 ) {
              return b+c;
            }
            if (!p) {
              p = d*(0.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 - 0.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 )*0.5 + c + b;
          },
          backIn: function(p,t, b, c, d, s){     //回退加速(回退漸入)
            if (typeof s == 'undefined') {
               s = 1.70158;
            }
            return c*(t/=d)*t*((s+1)*t - s) + b;
          },
          backOut: function(p,t, b, c, d, s){
            if (typeof s == 'undefined') {
              s = 3.70158;  //回縮的距離
            }
            return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
          }, 
          backBoth: function(p,t, b, c, d, s){
            if (typeof 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;
          },
          bounceIn: function(p,t, b, c, d){    //彈球減振(彈球漸出)
            return c - this['bounceOut'](p,d-t, 0, c, d) + b;
          },       
          bounceOut: function(p,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 + 0.75) + b;
            } else if (t < (2.5/2.75)) {
              return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
            }
            return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
          },      
          bounceBoth: function(p,t, b, c, d){
            if (t < d/2) {
              return this['bounceIn'](p,t*2, 0, c, d) * 0.5 + b;
            }
            return this['bounceOut'](p,t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
          }
        }); 
      })(jQuery);
      </script>
      復(fù)制代碼

        下面利用擴展后的JQ插件來進(jìn)行JQ的自定義運動

      <button id="btn">開始運動</button>
      <button id="reset">還原</button>
      <div id="test" style="height: 100px;width: 100px;background-color: pink;position:absolute;left:0;"></div>
      復(fù)制代碼
      <script>
      reset.onclick = function(){history.go();}
      btn.onclick = function(){
         $(test).animate({width:300},1000,'bounceBoth')
      } 
      </script>
      復(fù)制代碼

       

      Tween函數(shù)

        下面基于上面的tween算法,對tween里面的所有運動形式進(jìn)行封裝,名稱為tweenMove.js

      復(fù)制代碼
      if (!window.requestAnimationFrame) {
          requestAnimationFrame = function(fn) {
              setTimeout(fn, 17);
          };    
      }
      if (!window.cancelAnimationFrame) {
          window.cancelAnimationFrame = function(id) {
              clearTimeout(id);
          };
      }
      function getCSS(obj,style){
          if(window.getComputedStyle){
              return getComputedStyle(obj)[style];
          }
          return obj.currentStyle[style];
      } 
      // 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;
              }
          }
      };  
      function tweenMove(obj,json,times,fx,fn){
        //獲取當(dāng)前毫秒數(shù)
        var startTime = +new Date();  
        /*獲取初始值*/
        var iCur = {};
        for(var attr in json){
          if(attr == 'opacity'){
            //對當(dāng)前值的取值進(jìn)行四舍五入,去除由于javascript小數(shù)計數(shù)中的bug存在的小尾巴
            iCur[attr] = Math.round(getCSS(obj,attr)*100);
          }else{
            //去掉單位
            iCur[attr] = parseInt(getCSS(obj,attr));
          }
        }
        //清除定時器
        cancelAnimationFrame(obj.timer);
        obj.timer = requestAnimationFrame(function func(){
          //獲取t、d參數(shù)
          var d = times;
          var t = d - Math.max(0,startTime - (+new Date()) + d);
          for(var attr in json){
            /*獲取b、c、p這三個參數(shù)*/
            var b = iCur[attr];
            var c = json[attr]-iCur[attr];
            var fxArr = fx.split('-');
            if(fxArr.length == 2){
              var p = Tween[fxArr[0]][fxArr[1]](t,b,c,d);
            }else{
              var p = Tween[fx](t,b,c,d);
            }     
            /*賦值操作*/
            if(attr == 'opacity'){
              obj.style.opacity = p / 100;
              obj.style.filter = 'alpha(opacity=' + p + ')';
            }else{
              obj.style[attr] = p + 'px';
            }  
          }
            obj.timer = requestAnimationFrame(func);
            /*運行指定時間后*/
            if(t == d){
              //清除定時器
              cancelAnimationFrame(obj.timer);
              //設(shè)置回調(diào)函數(shù)
              fn && fn.call(obj);        
            }    
        });
      } 
      復(fù)制代碼

        下面是一個實例演示

      復(fù)制代碼
      <button id="btn">開始運動</button>
      <button id="reset">還原</button>
      <div id="test1" style="height: 100px;width: 100px;background-color: pink;position:absolute;left:0;"></div>
      <div id="test2" style="height: 100px;width: 100px;background-color: blue;position:absolute;left:300px;"></div>
      <script src="http://files.cnblogs.com/files/xiaohuochai/tweenMove.js"></script>
      <script>
      reset.onclick = function(){history.go();}
      btn.onclick = function(){
         tweenMove(test1,{width:300,height:200},500,'Bounce-easeInOut',function(){
          tweenMove(test2,{width:300},500,'Linear');
         });
         tweenMove(test2,{left:400},500,'Linear');
         
      }
      </script>
      復(fù)制代碼

       

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多