標籤:
當你不再依賴JQuery時,當你已經厭倦了引入js類庫實現一些動畫效果的方式,當你想實現一個簡單而實用的動畫架構......下面介紹下愚人設計的動畫架構:支援動畫緩動演算法函數,如Linear、Cubic、Back、Bounce,支援改變高度,寬度,透明度,邊框,外邊距的基本動畫,支援動畫的回呼函數,如開始、暫停、完成的callback等。
Section One遊戲動畫,Flash動畫裡一個比較重要的概念是幀頻,即每秒播放多少幀動畫,一般動畫是30幀/秒,單位為fps(frames per second)。 對於勻速運動來說:如果一個動畫的期間duration為500ms,幀頻frequence為30fps,則總幀數frames為(500/1000)*30 = 15,即該動畫過程有15個“畫面”,每走一幀,都計算出一個畫面:畫面當前位置 = 開始位置 + (當前幀/總幀數)(結束位置-開始位置),如果當前幀是最後一幀,則動畫結束。其中setTimeout或setInterval每隔(500/15)ms時間段調用一次函數,即計算一個畫面。 來看下線性運動Linear緩動演算法函數,t表示當前幀,b表示開始位置,c表示發生位移的距離值,即當前位置-開始位置,d表示總幀數,符合上面的推理解釋,對於其他的演算法函數,道理其實都是一樣,只不過在運動過程中的曲線不同,有些呈現拋物線,有些呈現線性指數,對於數學感興趣的可以研究下這些演算法函數,我也是略知皮毛: Linear: function (t, b, c, d) {return c * t / d + b;}清楚了以上問題後,對於js的動畫架構設計,就迎刃而解了,廢話不多說,來個 demo 先。 Section Two代碼總體結構,具體說明看注釋,需注意的問題:1)在私人範圍裡定義的變數,要在外部能訪問到,需掛在window全域對象下,如window.Anim = Anim;2)動畫元素需要設定定位position屬性;3)傳入的外邊距參數需要駝峰式命名,並且當同時設定targetPos(元素目標位置)和外邊距時,外邊距的值會覆蓋targetPos的值,如marginLeft的值會覆蓋targetPos.left的值,因為外邊距實現動畫的原理也是利用元素的left、top值: (function(window) { /* * 工具對象 * 包含基本的dom操作,event操作 */ var util = {}; util.dom = { // 擷取元素計算樣式 getPropValue: function(element, propName) { }, // 設定透明度 setOpacity: function(obj, num) { document.all ? obj.filters.alpha.opacity = num : obj.style.opacity = num / 100; } // ...... }; util.event = { // 擷取事件對象 getEvent: function(event) { }, // 擷取事件來源目標 getTarget: function(event) { }, // 註冊事件 addEvent: function(element, event, handler) { }, // 刪除事件 removeEvent: function(element, event, handler) { }, // 阻止預設行為 preventDefault: function(event) { }, // 阻止事件冒泡 stopPropagation: function(event) { } // ...... }; /* * 動畫easing 函式 */ 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; } }, // ... }; /* * 核心動畫 * @elem 要執行動畫的元素 * @options left、top、opacity、width、height、border、marginLeft、marginRight、marginTop、marginBottom */ function Anim(elem, options) { this.elem = elem; this.options = options; // 預設樣式屬性 this.defaults = { }; } Anim.prototype = { constructor: Anim, // 初始化動畫 init: function() { this.isStart = false; this.isStop = false; this.isComplete = false; this.isBack = false; this.start(); }, // 初始化資料 before: function() { }, // 開始動畫 start: function() { }, // 動畫過程 run: function() { this.before(); // 動畫參數匹配 this.match(); // 原路返回 if (this.isBack) { // ... } if (this.isStart) { this.onStart.call(this.elem); // 計算動畫 this.count(); } }, // 匹配動畫的參數 match: function() { }, // 計算動畫 count: function() { }, // 原路返回 back: function() { }, // 停止動畫 stop: function() { }, // 重設元素 reset: function() { } }; // 全域使用 if (!window.util) { window.util = util; } if (!window.Anim) { window.Anim = Anim; } if (!window.Tween) { window.Tween = Tween; } })(window); Last使用非常簡單,初始化參數對象,然後調用建構函式Anim即可: // 動畫參數設定var options = { duration: 2000, // 動畫期間 frequence: 30, // 幀頻 tweenFunc: Tween.Linear, // 動畫easing 函式 targetPos: {left: 400, top: 300}, // 元素目標位置 opacity: 40, // 透明度(可選) width: 80, // 寬度(可選) height: 80, // 高度(可選) // marginTop: 100, // 上邊距(可選) border: ‘2px solid red‘, // 邊框(可選) // 動畫開始callback(可選) onStart: function() { // this指向當前動畫元素 }, // 動畫停止callback(可選) onStop: function() { // this指向當前動畫元素 }, // 動畫完成callback(可選) onComplete: function() { // this指向當前動畫元素 }};var anim = new Anim(animElem, options); 最後附上 原始碼 以及easing演算法函數的 demo。
js動畫架構設計