您好,登錄后才能下訂單哦!
具體代碼如下:
~function(){ var myEffect = { Linear:function(t,b,c,d){ return c*t/d+b }, Quad: {//二次方的緩動(t^2); 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: {//三次方的緩動(t^3) 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: {//四次方的緩動(t^4); 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: {//5次方的緩動(t^5); 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: {//正弦曲線的緩動(sin(t)) 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: {//指數曲線的緩動(2^t); 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: {//圓形曲線的緩動(sqrt(1-t^2)); 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: {//超過范圍的三次方緩動((s+1)*t^3 - s*t^2); 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; } }, zfBounce: {//指數衰減的反彈緩動。 easeIn: function(t,b,c,d){ return c - zhufengEffect.zfBounce.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 zhufengEffect.zfBounce.easeIn(t*2, 0, c, d) * .5 + b; else return zhufengEffect.zfBounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b; } } }; //move:實現多方向的運動動畫 /* curEle:當前要運動的元素 target:當前動畫的目標位置,存儲的是每一個方向的目標位置{left:xxx,top:xxx...} duration:當前動畫的總時間 */ //effect支持以下的情況 /* */ function move(curEle,target,duration,effect,callback){ //處理我們需要的動畫效果 var tempEffect = myEffect.Linear; if(typeof effect === "number"){ switch(effect){ case 0: tempEffect = myEffect.Linear; break; case 1: tempEffect = myEffect.Circ.easeInOut; break; case 2: tempEffect = myEffect.Elastic.easeOut; break; case 3: tempEffect = myEffect.Back.easeOut; break; case 4: tempEffect = myEffect.Bounce.easeOut; break; case 5: tempEffect = myEffect.Expo.easeIn; } }else if(effect instanceof Array){ tempEffect = effect.length>=2 ? myEffect[effect[0]][effect[1]] : myEffect[effect[0]] }else if(typeof effect === "function"){ //我們的實際意義應該是:effect是不傳遞值的,傳遞進來的函數應該是回調函數的值 callback = effect; } //在每一次執行方法之前,首先把當前元素之前正在運行的動畫結束掉 window.clearInterval(curEle.timer); //根據target獲取每一個方向的起始值begin和總距離change var begin = {},change = {}; for(var key in target){ if(target.hasOwnProperty(key)){ begin[key] = utils.css(curEle,key) change[key] = target[key] - begin[key]; } } //實現多方向的運動動畫 var time = 0; curEle.timer = window.setInterval(function(){ time+=10; //到達目標:結束動畫,讓當前元素的樣式值等于目標樣式值 if(time>=duration){ utils.css(curEle,target); window.clearInterval(curEle.timer); //在動畫結束的時候,如果用戶把回調函數傳遞給我了,我就把用戶傳遞的回調函數執行,不僅執行還把this的指向改為當前操作的元素 typeof callback === "function" ? callback.call(curEle) : null; //或者callback && callback() return; } //沒到達目標:分別獲取每一個方向的當前位置,給當前位置設置樣式即可。 for(var key in target){ if(target.hasOwnProperty(key)){ var curPos = tempEffect(time,begin[key],change[key],duration); utils.css(curEle,key,curPos); } } },10) } window.myAnimate = move; }()
以上這篇js學習心得_一個簡單的動畫庫封裝tween.js就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。