MooTools 1.4 源碼分析 - Fx.Tween

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

 

  1. /* 
  2. --- 
  3.  
  4. name: Fx.Tween 
  5.  
  6. description: Formerly Fx.Style, effect to transition any CSS property for an element. 
  7.  
  8. license: MIT-style license. 
  9.  
  10. requires: Fx.CSS 
  11.  
  12. provides: [Fx.Tween, Element.fade, Element.highlight] 
  13.  
  14. 源碼分析: 苦苦的苦瓜(http://hmking.blog.51cto.com) 
  15.  
  16. ... 
  17. */ 
  18.  
  19. // #region - Fx.Tween - 
  20.  
  21. /** 
  22. * @Fx.Tween: 對元素單個樣式屬性執行一個特效 
  23. **/ 
  24. Fx.Tween = new Class({ 
  25.  
  26.     // 繼承自Fx.CSS 
  27.     Extends: Fx.CSS, 
  28.  
  29.     /** 
  30.     * @method: initialize 
  31.     * @param element - (mixed) 元素的id或引用 
  32.     * @param options - (object, 可選) 類可用的所有可選項, 以及如下的可選項: 
  33.     *       property - (string, 默認爲null) 變換的目標CSS屬性,例如:'width', 'color', 'font-size', 'border'等.  
  34.     *                               如果在此省略該可選項, 則在執行start或set方法時,需要在方法的第一個參數上指定一個CSS屬性. 
  35.     * @description: 構造函數,提供將元素的一個CSS屬性值從一個值向另一個值進行轉化的功能 
  36.     * @notes: 
  37.     *       任何可以用Element:setStyle設置的CSS屬性都可以用於Fx.Tween 
  38.     *       如果不是可計算型的CSS屬性, 如border-style 或 background-p_w_picpath等, 則只是簡單的設置它的值 
  39.     *       如果使用了property可選項, 則在調用start和set方法時就不用再指定CSS屬性 
  40.     **/ 
  41.     initialize: function (element, options) { 
  42.         // element屬性存儲特效所作用的元素對象 
  43.         this.element = this.subject = document.id(element); 
  44.         // 調用父類的同名方法 
  45.         this.parent(options); 
  46.     }, 
  47.  
  48.     /** 
  49.     * @method: set 
  50.     * @param property - (string) css屬性(如果在構造函數中設置了property可選項, 則該處可以省略) 
  51.     * @param now - (mixed) css屬性值 
  52.     * @description: 將元素的指定CSS屬性值立即設置爲指定的值 
  53.     * @returns: (object) 主調Fx.Tween實例 
  54.     * @notes: 
  55.     *       如果使用了property可選項, 或者在start方法中指定了CSS屬性參數,則在調用set方法時就不用指定CSS屬性參數 
  56.     **/ 
  57.     set: function (property, now) { 
  58.         // 如果只提供一個參數 
  59.         if (arguments.length == 1) { 
  60.             // 將此參數作爲目標值 
  61.             now = property; 
  62.             // 取CSS屬性,首先取Fx實例的property屬性存儲的CSS屬性名 
  63.             property = this.property || this.options.property; 
  64.         } 
  65.         // 最終渲染效果 
  66.         this.render(this.element, property, now, this.options.unit); 
  67.         return this
  68.     }, 
  69.  
  70.     /** 
  71.     * @method: start 
  72.     * @param property - (string)  要進行變換的css屬性(如果在構造函數中設置了property可選項, 則該處可以省略) 
  73.     * @param from - (mixed) CSS屬性起始值。如果整個方法只給出一個參數,則該值作爲CSS屬性的結束值 
  74.     * @param to - (mixed, 可選) CSS屬性結束值 
  75.     * @description: 將元素的CSS屬性值過度到指定值 
  76.     * @notes: 
  77.     *       如果整個方法只給出一個參數,則該值作爲CSS屬性的結束值, 起始值則從元素的當前狀態計算而來 
  78.     *       當變換顏色類的CSS屬性時, 既可使用RGB格式也可以使用十六進制格式 
  79.     *       如果在構造函數中設置了property可選項, 則在調用start方法時就不用指定CSS屬性參數 
  80.     **/ 
  81.     start: function (property, from, to) { 
  82.         // 檢查當前特效運行狀態,決定是否運行新特效 
  83.         if (!this.check(property, from, to)) { return this; } 
  84.         // 將參數降維 
  85.         var args = Array.flatten(arguments); 
  86.         // 取CSS屬性,首先判斷有沒有設置property可選項 
  87.         this.property = this.options.property || args.shift(); 
  88.         // 調用父類Fx.CSS的prepare方法解釋參數,得到from和to值 
  89.         var parsed = this.prepare(this.element, this.property, args); 
  90.         // 調用Fx基類的同名方法,開始執行特效 
  91.         return this.parent(parsed.from, parsed.to); 
  92.     } 
  93.  
  94. }); 
  95.  
  96. // #endregion 
  97.  
  98. // #region - Element.Properties.tween - 
  99.  
  100. /** 
  101. * @element property: tween 
  102. * @description: 用於設置或獲取元素上的Fx.Tween實例,實現單件模式 
  103. * @notes: 
  104. *       01、When initializing the Element's tween instance with Element:set, the property to tween SHOULD NOT be passed. 
  105. *               當使用Element:set方法來設置元素的tween時, 則要進行tween的css屬性<不需要>傳入 
  106. *       02、The property must be specified when using Element:get to retrieve the actual Fx.Tween instance, and in calls to Element:tween 
  107. *               當使用Element:get方法來獲取元素的Fx.Tween實例時, 則可選項中的property必須指定 
  108. *       03、When options are passed to the setter, the instance will be reset. 
  109. *               當使用setter方法設置可選參數時,Fx.Tween實例對象會被重置 
  110. *       04、As with the other Element shortcuts, the difference between a setter and a getter is that the getter returns the instance, while the setter returns the element (for chaining and initialization). 
  111. *               調用get方法獲取tween返回的是Fx.Tween的實例, 而調用set返回的是主調元素 
  112. *       05、當使用get方法時, 如果元素上還不存在tween, 則會根據給出的可選項新建一個實例設置到元素上 
  113. **/ 
  114. Element.Properties.tween = { 
  115.  
  116.     // setter設置Fx.Tween對象參數 
  117.     set: function (options) { 
  118.         // 取得Fx.Tween實例,取消執行中的特效,然後再設置可選參數 
  119.         this.get('tween').cancel().setOptions(options); 
  120.         return this
  121.     }, 
  122.  
  123.     get: function () { 
  124.         // 先從臨時對象讀取,看有沒緩存到Fx.Tween實例 
  125.         var tween = this.retrieve('tween'); 
  126.         if (!tween) { 
  127.             // 保存Fx.Tween實例 
  128.             tween = new Fx.Tween(this, { link: 'cancel' }); 
  129.             this.store('tween', tween); 
  130.         } 
  131.         return tween; 
  132.     } 
  133.  
  134. }; 
  135.  
  136. // #endregion 
  137.  
  138. // #region - Element Methods - 
  139.  
  140. // 元素進行特效變換的快捷方法 
  141. Element.implement({ 
  142.  
  143.     /** 
  144.     * @element method: tween 
  145.     * @returns: (element) 返回主調元素 
  146.     * @notes: 參照Fx.Tween.start方法 
  147.     **/ 
  148.     tween: function (property, from, to) { 
  149.         this.get('tween').start(property, from, to); 
  150.         return this
  151.     }, 
  152.  
  153.     /** 
  154.     * @element method: fade 
  155.     * @param how - (mixed, 可選: 默認爲'toggle') 代表不透明度的數值或字符串. 可爲如下值: 
  156.     *       'in' - opacity爲100% 
  157.     *       'out' - opacity爲0% 
  158.     *       'show' - opacity立即設置爲100% 
  159.     *       'hide' - opacity立即設置爲0% 
  160.     *       'toggle' - 如果元素當前爲可見狀態, 則將元素淡出; 相反, 則將元素淡入 
  161.     *       (number) - 0~1之間的浮點數. 將代入淡出到該值. 
  162.     * @returns: (element) 返回主調元素 
  163.     * @description: 對opacity樣式屬性進行tween特效變換的快捷方法,實現的深入淺出效果 
  164.     **/ 
  165.     fade: function (how) { 
  166.         // 取得主調元素的Fx.Tween實例 
  167.         var fade = this.get('tween'), 
  168.                 method, to, toggle; 
  169.         if (how == null) { how = 'toggle'; } 
  170.         // 幾種淡入淡出的方式 
  171.         switch (how) { 
  172.             case 'in'// 淡入 
  173.                 method = 'start'
  174.                 to = 1; 
  175.                 break
  176.  
  177.             case 'out'// 淡出 
  178.                 method = 'start'
  179.                 to = 0; 
  180.                 break
  181.  
  182.             case 'show'// 顯示 
  183.                 method = 'set'
  184.                 to = 1; 
  185.                 break
  186.  
  187.             case 'hide'// 隱藏 
  188.                 method = 'set'
  189.                 to = 0; 
  190.                 break
  191.  
  192.             case 'toggle'// 開關 
  193.                 // 獲取標記變量, 第二個參數用於默認值 
  194.                 var flag = this.retrieve('fade:flag'this.getStyle('opacity') == 1); 
  195.                 method = 'start'
  196.                 // 根據標記狀態控制淡入還是淡出 
  197.                 to = flag ? 0 : 1; 
  198.                 // 將標記取反保存 
  199.                 this.store('fade:flag', !flag); 
  200.                 toggle = true
  201.                 break
  202.  
  203.             default
  204.                 method = 'start'
  205.                 to = how; 
  206.         } 
  207.         // 如果沒有使用開關方式,刪除臨時標記,避免在使用toggle之後又使用其它方式,導致toggle響應錯誤 
  208.         if (!toggle) { this.eliminate('fade:flag'); } 
  209.         // 根據指定的淡入淡出方式執行特效實例相應的方法 
  210.         fade[method]('opacity', to); 
  211.         // 根據opacity樣式屬性結束值是否爲零來設置主調元素隱藏還是顯示 
  212.         if (method == 'set' || to != 0) { 
  213.             this.setStyle('visibility', to == 0 ? 'hidden' : 'visible'); 
  214.         } else fade.chain(function () { 
  215.             this.element.setStyle('visibility''hidden'); 
  216.         }); 
  217.         return this
  218.     }, 
  219.  
  220.     /** 
  221.     * @element method: highlight 
  222.     * @param start - (string, 可選: 默認爲'#ff8') 高亮色 
  223.     * @param end - (string, 可選: 默認爲元素初始的background-color值) 高亮效果結束後的元素背景顏色 
  224.     * @returns: (element) 返回主調元素 
  225.     * @description: 對background-color樣式屬性進行tween特效變換的快捷方法.(即背景高亮特效, 將背景顏色迅速設置爲指定顏色,隨後返回到初始的背景顏色) 
  226.     * @notes: 如果未給元素指定背景色, 或指定成了'transparent', 則end值默認爲白色 
  227.     **/ 
  228.     highlight: function (start, end) { 
  229.         // end爲動畫結束後使用的背景色,通常是原來恢復原來的顏色 
  230.         if (!end) { 
  231.             // 臨時對象取值 
  232.             end = this.retrieve('highlight:original'this.getStyle('background-color')); 
  233.             // 透明的話按白色處理 
  234.             end = (end == 'transparent') ? '#fff' : end; 
  235.         } 
  236.         // 獲取主調元素的Fx.Tween實例 
  237.         var tween = this.get('tween'); 
  238.         // 開始執行 
  239.         tween.start('background-color', start || '#ffff88', end).chain(function () { 
  240.             // 動畫結束恢復原來顏色 
  241.             this.setStyle('background-color'this.retrieve('highlight:original')); 
  242.             // 鏈式執行 
  243.             tween.callChain(); 
  244.         } .bind(this)); 
  245.         return this
  246.     } 
  247.  
  248. }); 
  249.  
  250. // #endregion 

 

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