JavaScript Tween 動畫效果

   Tween JavaScript & CSS 源碼效果網址: javascript

    jquery.easing.js:

   網址:https://github.com/danro/jquery-easing/blob/master/jquery.easing.js java

    jQuery Easing Plugin(有Demo):

   網址:http://gsgd.co.uk/sandbox/jquery/easing/ jquery

    CSS EASING ANIMATION TOOL:
    網址:http://matthewlein.com/ceaser/


    

    Jquery 1.8的API文檔只提供了少些JS動畫效果。 git

    在平時簡單的開發效果中是夠用了,若是項目比較大,效果又比較絢麗,那挑選的餘地就綽綽有餘。最近在開發項目中就遇到了瓶頸,效果種類太少,看都看膩了就別提使用了。 github

    若是公司規模夠大,那會有Flash部門,通過和他們聊天發現Flash有個包叫:com.tweenman裏面有個類庫:Easing 函數

     javascript的tween函數的因爲計算方法不一樣,基本能夠分紅flash流派和prototype流派,動畫的效果都差很少。flash流派的緩動函數一般都有4個參數,分別是: 學習

  • t: timestamp,指緩動效果開始執行到當前幀開始執行時通過的時間段,單位ms
  • b: beginning position,起始位置
  • c: change,要移動的距離,就是終點位置減去起始位置
  • d: duration ,緩和效果持續的時間

    言歸正傳,進入主題: 動畫

    JavaScript的Tween類庫,見下面。 ui

    JavaScript其實調用方法比較簡單,就是傳入4個參數而已 spa

    例子:

/*拿到要執行的Dom節點:
方便講解我把該元素的CSS樣式粘貼過來。
div{
    width:100px;
    height:100px;
    background:#000;
    position:absolute;
    left:100px;
    top:100px;
}
*/
var div = document.getElementById('div');

/*
b,c,d,t參數我用比較白開水的話描述吧,方便你們學習

b:表明該被動畫元素的起始座標位置。
  這裏b=100就是div Dom節點的left默認值:100px;

c:表明要移動到位置和自己位置的差值。
  好比要將div Dom元素移動到left:400px;那麼c = 400 - 100;

d:表示總步數,也就是說須要多少步完成本次動畫。

t:至關於時間的變化,通常是均勻變化的,每次變化都增長一個步長,
  當t從0遞增(或遞減)到d時,緩動就結束了。
*/
var b=100,c=300,d=100,t=0;

function Run(){

div.style.left = Math.ceil(Tween.Cubic.easeInOut(t,b,c,d)) + "px";

if(t<d){ 
    t++; 
    /*
    注:t是從0開始的,設置步長時必須肯定t確實能到達d,
        若是上面的步長是3,那麼t就永遠都到不了d了。

        這裏t++是確定等到100的,若是t += 3的話,是到不了100的。也就是d值。
    */
    setTimeout(Run, 10); 
}
}
Run();

    附JavaScript 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;
        }
    }
}
    

    歡迎提出疑問或糾正。

相關文章
相關標籤/搜索