基于时间的动画函数

2016-06-13  本文已影响93人  MakingChoice

基于时间的动画函数是适用于大多数情况,避免了由于页面切换造成的动画不连贯现象,同时jquery内部也使用的这种方法。下面是源码,结合源码看一下就明白了。

<script>
   var btn=document.getElementsByClassName("button-success")[0];
   var dv=document.getElementsByClassName("div")[0];
   btn.onclick= function () {
      getStyle(dv,"left");
      move(dv,2000,{
         left:"200",
         top:"300",
      },"elasticBoth", function () {
         console.log(1);
      })
   }
   var move= function (obj,t, JSON, type, fn) {
      var startValue={};//获取其实值
      var startTime=(new Date()).getTime();//获取起始时间
      for(var attr in JSON){
         startValue[attr]=0;//初始化一个style对象
         if(attr ==="opacity"){
            startValue[attr]=Math.round(getStyle(obj,attr)*100);//兼容IE style是opacity的情况
         }else{
            startValue[attr]=parseInt(getStyle(obj,attr))
         }
      }
      clearInterval(obj.time);//避免多次触发造成动画叠加
      obj.time=setInterval(function () {
         var nowTime=(new Date()).getTime();//获取现在时间
         var scale=1-Math.max(0,startTime+t-nowTime)/t;//通过tartTime+t-nowTime获取随着时间变化的变化率
         for(var attr in JSON){//对json形式的style进行挨个赋值
            var value=Tween[type](scale*t,startValue[attr],JSON[attr]-startValue[attr],t);//Tween用法,这里把时间t设置为变化量,这样就可以得到Tween处理后的变化值。
            if(attr==="opacity"){
               obj.style.filter='alpha(opacity'+value+')';
               obj.style.opacity=value/100;
            }else{
               obj.style[attr]=value+'px';
            }
         }
         if(scale===1){
            clearInterval(obj.time);
            if(fn){
               fn.call(obj);//触发回调
            }
         }
      },30);

   }
   var getStyle= function (obj, attr) {
      if(obj.currentStyle){
         return obj.currentStyle[attr];
      }else{
         return getComputedStyle(obj,false)[attr];
      }
   }
   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;
      }
   }
</script>

Linear:无缓动效果(匀速运动);
Quadratic:二次方的缓动;
Cubic:三次方的缓动
Quartic:四次方的缓动;
Quintic :五次方的缓动;
Sinusoidal:正弦曲线的缓动;
Exponential:指数曲线的缓动;
Circular:圆形曲线的缓动;
Elastic:指数衰减的正弦曲线缓动;
Back:超过范围的三次方缓动);
Bounce:指数衰减的反弹缓动。
每个效果都分三个缓动方式(方法),分别是:
easeIn:从0开始加速的运动;
easeOut:减速到0的运动;
easeInOut:前半段从0开始加速,后半段减速到0的运动。
函数的四个参数分别代表:
t--- current time(当前时间);
b--- beginning value(初始值);
c--- change in value(变化量);
d---duration(持续时间)
运算的结果就是当前的运动路程。可以看到,只有t-current-time是能变化的,这样就支持了根据时间来实现动画。

上一篇 下一篇

猜你喜欢

热点阅读