MooTools 1.4 源碼分析 - Fx.Morph

 Mootools1.4 - Fx.Morph類的源碼分析,如果理解有誤歡迎指正:

 

  1. /* 
  2. --- 
  3.  
  4. name: Fx.Morph 
  5.  
  6. description: Formerly Fx.Styles, effect to transition any number of CSS properties for an element using an object of rules, or CSS based selector rules. 
  7.  
  8. license: MIT-style license. 
  9.  
  10. requires: Fx.CSS 
  11.  
  12. provides: Fx.Morph 
  13.  
  14. 源碼分析: 苦苦的苦瓜(http://hmking.blog.51cto.com) 
  15.  
  16. ... 
  17. */ 
  18.  
  19. // #region - Fx.Morph - 
  20.  
  21. /** 
  22. * @Fx.Morph: 提供一次對多個CSS屬性進行動畫特效變換的功能,所以本類的屬性爲多值集合 
  23. **/ 
  24. Fx.Morph = new Class({ 
  25.  
  26.     // 繼承自Fx.CSS 
  27.     Extends: Fx.CSS, 
  28.  
  29.     /** 
  30.     * @method: initialize 
  31.     * @param element - (mixed) 元素的id或引用 
  32.     * @param options - options - (object, 可選) Fx類中提供的可選項 
  33.     * @description: 構造函數,提供將多個元素的CSS屬性值從一個值向另一個值進行轉化的功能 
  34.     **/ 
  35.     initialize: function (element, options) { 
  36.         // element屬性存儲特效所作用的元素對象 
  37.         this.element = this.subject = document.id(element); 
  38.         // 調用父類的同名方法 
  39.         this.parent(options); 
  40.     }, 
  41.  
  42.     /** 
  43.     * @method: set 
  44.     * @param now - (mixed) 可以是包含CSS屬性鍵值對的對象, 或是一個CSS選擇器(必須在頁面中已定義).如果對CSS屬性只給出一個值,則變換的時候將把元素當前的屬性值作爲起始值. 
  45.     *       { 
  46.     *           'height': 200, 
  47.     *           'width': 200, 
  48.     *           'background-color': '#f00', 
  49.     *           'opacity': 0 
  50.     *       } 
  51.     * @returns: (object) 主調Fx.Morph實例 
  52.     * @description: 將元素的指定CSS屬性值立即設爲指定值 
  53.     **/ 
  54.     set: function (now) { 
  55.         // 如果參數是字符串類型,表示爲CSS選擇符,從當頁頁面的樣式規則中查找指定的規則 
  56.         if (typeof now == 'string') { 
  57.             now = this.search(now); 
  58.         } 
  59.         // 分別設置每一項樣式值 
  60.         for (var p in now) { 
  61.             this.render(this.element, p, now[p], this.options.unit); 
  62.         } 
  63.         return this
  64.     }, 
  65.  
  66.     /** 
  67.     * @method: compute 
  68.     * @param from - (object) 解釋過的各項樣式屬性的起始值的對象 
  69.     * @param to - (object) 解釋過的各項樣式屬性的結束值的對象 
  70.     * @param delta - (mixed) 特效變化所需要的比例因子 
  71.     * @returns: (array) 包含計算過的各項樣式屬性當前值信息的一個數組 
  72.     * @description: 根據各項樣式屬性初始值,結束值和特效比例因子計算各項樣式屬性的當前值 
  73.     **/ 
  74.     compute: function (from, to, delta) { 
  75.         var now = {}; 
  76.         for (var p in from) { 
  77.             // 對每一項樣式屬性,調用Fx.CSS類的同名方法計算 
  78.             now[p] = this.parent(from[p], to[p], delta); 
  79.         } 
  80.         return now; 
  81.     }, 
  82.  
  83.     /** 
  84.     * @method: start 
  85.     * @param roperties - (mixed) 可以是包含CSS屬性鍵值對的對象, 或是一個CSS選擇器(必須在頁面中已定義).如果對CSS屬性只給出一個值,則變換的時候將把元素當前的屬性值作爲起始值. 
  86.     *       { 
  87.     *           'height': [10, 100], 
  88.     *           'width': [900, 300], 
  89.     *           'opacity': 0, 
  90.     *           'background-color': '#00f' 
  91.     *       } 
  92.     * @returns: (object) - 主調Fx.Morph實例 
  93.     * @description: 串聯執行多個CSS屬性的變換(並觸發'start'事件) 
  94.     * @notes: 
  95.     *       如果傳入一個CSS選擇器, 則該選擇器必須在頁面存在相應匹配的樣式 
  96.     *       不支持多選擇器(逗號分隔的多個選擇器) 
  97.     *       @import'ed CSS rules will not be available for Morph calls. All CSS selectors must be present in CSS directly loaded into the page. 
  98.     **/ 
  99.     start: function (properties) { 
  100.         // 檢查當前特效運行狀態,決定是否運行新特效 
  101.         if (!this.check(properties)) { return this; } 
  102.         // 如果提供properties參數類型爲字符串,表明指定的是CSS選擇符名,需要從當前頁的樣式規則中查找各項屬性 
  103.         if (typeof properties == 'string') { 
  104.             properties = this.search(properties); 
  105.         } 
  106.         var from = {}, 
  107.                 to = {}; 
  108.         // 對每項CSS屬性值計算解釋後的值,此時的from和to對象每個鍵值皆爲一個數組 
  109.         for (var p in properties) { 
  110.             var parsed = this.prepare(this.element, p, properties[p]); 
  111.             from[p] = parsed.from; 
  112.             to[p] = parsed.to; 
  113.         } 
  114.         // 調用Fx類的構造函數 
  115.         return this.parent(from, to); 
  116.     } 
  117.  
  118. }); 
  119.  
  120. // #endregion 
  121.  
  122. // #region - Element.Properties.morph - 
  123.  
  124. /** 
  125. * @element property: morph 
  126. * @description: 用於設置或獲取元素上的Fx.Morph實例,實現單件模式 
  127. **/ 
  128. Element.Properties.morph = { 
  129.  
  130.     // setter設置Fx.Morph對象參數 
  131.     set: function (options) { 
  132.         // 獲取元素上的Fx.Morph實例後先執行cancel方法,取消特效的執行,然後再設置可選參數 
  133.         this.get('morph').cancel().setOptions(options); 
  134.         return this
  135.     }, 
  136.  
  137.     // getter獲取Fx.Morph對象 
  138.     get: function () { 
  139.         // 先從臨時對象讀取,看有沒緩存到Fx.Morph實例 
  140.         var morph = this.retrieve('morph'); 
  141.         if (!morph) { 
  142.             // 如果沒有緩存,則保存一個新的Fx.Morph實例 
  143.             morph = new Fx.Morph(this, { link: 'cancel' }); 
  144.             this.store('morph', morph); 
  145.         } 
  146.         return morph; 
  147.     } 
  148.  
  149. }; 
  150.  
  151. // #endregion 
  152.  
  153. // #region - Element Method - 
  154.  
  155. // 元素調用Fx.Morph的快捷方式 
  156. Element.implement({ 
  157.  
  158.     /** 
  159.     * @element method: morph 
  160.     * @param properties - (mixed) 可以是包含CSS屬性鍵值對的對象, 或是一個CSS選擇器(必須在頁面中已定義).如果對CSS屬性只給出一個值,則變換的時候將把元素當前的屬性值作爲起始值. 
  161.     * @returns: (element) 返回主調元素 
  162.     * @description: 對元素執行指定屬性值的動畫特效變換 
  163.     **/ 
  164.     morph: function (props) { 
  165.         // 這是使用上面的getter取Fx.Morph實例,再start,props可以爲多個樣式屬性數組或CSS選擇符 
  166.         this.get('morph').start(props); 
  167.         return this
  168.     } 
  169.  
  170. }); 
  171.  
  172. // #endregion 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章