Jquery1.9.1源碼分析系列(十五)動畫處理之外篇_jquery
a.动画兼容Tween.propHooks
Tween.propHooks提供特殊情况下设置、获取css特征值的方法,结构如下
Tween.propHooks = { _default: { get: function(){...}, set: function(){...} }, scrollTop: { set: function(){...} } scrollLeft: { set: function(){...} } }
Tween.propHooks.scrollTop 和Tween.propHooks.scrollLeft两个主要是在ie8离线状态下会出现混乱而把css特征值保存到节点上
set: function( tween ) { if ( tween.elem.nodeType && tween.elem.parentNode ) { tween.elem[ tween.prop ] = tween.now; } }
Tween.propHooks._default的get方法会尝试直接从节点上取得css的tween.prop特征值,如果取不到则使用jQuery.css()方式来获取。该方法处理中,简单的值如“10px”会被解析为浮点数;复杂的值,如“旋转(1rad)”返回原样。并对返回结果再做处理:空字符串, null, undefined 和 "auto"都转化为0;其他情况不变。
get: function( tween ) { var result; if ( tween.elem[ tween.prop ] != null && (!tween.elem.style || tween.elem.style[ tween.prop ] == null) ) { return tween.elem[ tween.prop ]; } //传递一个空字符串作为第三个参数的.css会自动尝试parseFloat, //并返回到一个字符串,如果解析失败的话。 //所以,简单的值,如“10px”会被被解析为浮点数。复杂的值,如“旋转(1rad)”返回原样。 result = jQuery.css( tween.elem, tween.prop, "" ); // 空字符串, null, undefined 和 "auto"都转化为0 return !result || result === "auto" ? 0 : result; }
Tween.propHooks._default的set方法先会尝试jQuery.fx.step[ tween.prop ]来设置向下兼容;否则会使用jQuery.style来设置css特征值;最极端情况则会将特征值直接保存在节点上
set: function( tween ) { //使用step hook向下兼容 - 使用cssHook如果他存在 - 使用.style如果可用的话 //使用直接的特征值如果可用可用的话 if ( jQuery.fx.step[ tween.prop ] ) { jQuery.fx.step[ tween.prop ]( tween ); } else if ( tween.elem.style && ( tween.elem.style[ jQuery.cssProps[ tween.prop ] ] != null || jQuery.cssHooks[ tween.prop ] ) ) { jQuery.style( tween.elem, tween.prop, tween.now + tween.unit ); } else { tween.elem[ tween.prop ] = tween.now; } }
b. 动画专用对象jQuery.fx
jQuery.fx封装了一些用来执行动画动作的函数,结构如下
jQuery.fx = { tick = function () {...},//每个时间点都会执行的函数外壳,会取出jQuery.timers中的函数执行 timer = function ( timer ) {...},//执行参数中的函数并启动计时 interval = 13, //计时步长 start = function () {...},//启动计时 stop = function () {...},//停止计时 speeds = {slow: 600,fast: 200,_default: 400},//动画速度(完整动画执行时间) step = {}//向下兼容<1.8扩展点 }
详细的源码分析如下
jQuery.fx = Tween.prototype.init; //每个时间点都会执行的函数外壳,会取出jQuery.timers中的函数执行 jQuery.fx.tick = function() { var timer, timers = jQuery.timers, i = 0; fxNow = jQuery.now(); for ( ; i < timers.length; i++ ) { timer = timers[ i ]; // Checks the timer has not already been removed if ( !timer() && timers[ i ] === timer ) { timers.splice( i--, 1 ); } } if ( !timers.length ) { jQuery.fx.stop(); } fxNow = undefined; }; //执行参数中的函数并启动计时 jQuery.fx.timer = function( timer ) { if ( timer() && jQuery.timers.push( timer ) ) { jQuery.fx.start(); } }; //计时步长 jQuery.fx.interval = 13; //启动计时 jQuery.fx.start = function() { if ( !timerId ) { timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval ); } }; //停止计时 jQuery.fx.stop = function() { clearInterval( timerId ); timerId = null; }; //动画速度(完整动画执行时间) jQuery.fx.speeds = { slow: 600, fast: 200, // Default speed _default: 400 }; //向下兼容<1.8扩展点 jQuery.fx.step = {}; 这里其中执行动画的关键源码是 //动画入口函数function Animation( elem, properties, options ){ ... jQuery.fx.timer( jQuery.extend( tick, { elem: elem, anim: animation, queue: animation.opts.queue }) ); ... } //执行参数中的函数并启动计时 jQuery.fx.timer = function( timer ) { if ( timer() && jQuery.timers.push( timer ) ) { jQuery.fx.start(); } }; //计时步长 jQuery.fx.interval = 13; //启动计时 jQuery.fx.start = function() { if ( !timerId ) { timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval ); } };
The variable jQuery.timers = []; is used to save the list of functions that need to be executed for each tick. Generally speaking, there is only one function, which is the tick function defined in the Animation function. jQuery.fx.interval can be used to set the time interval between every two frames of the animation. The default is 13 milliseconds.
That’s it for the animation analysis. Below is a list of animation-related APIs
jQuery.fn.show([ duration ] [, easing ] [, complete ] | options ) (Displays all matching elements. In addition, you can also specify the transition animation effect of the element display. If the element itself is visible, If the element is hidden, make it visible. The opposite of this function is the hide() function, which is used to hide all matching elements)
jQuery.fn.hide([ duration ] [, easing ] [, complete ] | options) (Hide all matching elements. In addition, you can also specify the transition animation effect for element hiding. If the element itself is invisible , no changes are made to the element if it is visible )
.
jQuery.fn.toggle([ duration ] [, easing ] [, complete ] | options) (toggle all matching elements. In addition, you can also specify the transition animation effect of element switching. The so-called "toggle", that is, If the element is currently visible, hide it; if the element is currently hidden, make it visible (visible) )
.
The toggle() function introduced here is used to switch the display/hide of elements. jQuery also has an event function of the same name, toggle(), which is used to bind the click event and switch to execute different event processing functions in turn when triggered.
jQuery.fn.slideDown([ duration ] [, easing ] [, complete ] | options) (displays all matching elements with a sliding downward transition animation effect. The sliding downward animation effect, that is, the element The height of the visible area is gradually increased from 0 to its original height (gradually expanding downwards). If the element itself is visible, no changes are made to it. If the element is hidden, it is made visible.
.
jQuery.fn.stop([ queueName ] [, clearQueue [, jumpToEnd ] ]) (Stop the currently running animation on the matching element. By default, the stop() function will only stop the currently running animation. If You use the animate() function to set three animations A, B, and C for the current element. If the currently executing animation is A, it will only stop the execution of animation A and will not prevent the execution of animations B and C. Of course. , you can also stop all animations by specifying optional option parameters. Stopping the animation does not restore the state before the animation is executed, but stops it directly. The current animation execution state will stay in that state. For example: Execute a transition animation from 100px to 200px for an element's height. If the animation is stopped when the height is 150px, the current height will still remain at 150px. If the animation sets a callback function after execution, the callback function will not be executed. . )
jQuery.fn.finish([ queueName ]) (immediately completes all animations in the queue. finish() stops the currently running animation, deletes all animations in the queue, and completes all animations of matching elements)
jQuery.fn. fadeTo([speed,]opacity[,callback]) (gradually changes the opacity of the selected element to the specified value)
jQuery.fx.off (This property is used to set or return whether all animations are globally disabled. If this property is not set, a Boolean value indicating whether animation effects are globally disabled is returned. If this property is Set to true to disable all animations globally. Any animation queues that are not yet executed will be completed immediately without animation effects. false, will enable the animation effect globally
.
You can disable animation effects when you encounter the following situations: you are using jQuery on a computer with low configuration; some users may encounter accessibility issues due to animation effects. )
jQuery.fx.interval (This property is used to set or return the frame rate of the animation (millisecond value). The jQuery.fx.interval property is used to set how many milliseconds the jQuery animation draws a frame of image (triggering a style change, The browser may redraw the current page). The smaller the value, the more times the animation is triggered and the animation effect is more obvious and smoother. Of course, the animation queue being executed when changing the value of this property will consume more performance. will not be affected. Any animation queue that has not yet been executed will draw animation effects at the changed frame rate)
The above content is excluding animation processing of Jquery 1.9.1 source code analysis series (fifteenth) introduced by the editor of Script House. Animation processing of jQuery 1.9.1 source code analysis series (fifteen), click to learn more.

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。
