search
HomeWeb Front-endJS TutorialJquery1.9.1 source code analysis series (15) beyond animation processing_jquery

a. Animation compatible with Tween.propHooks

Tween.propHooks provides methods for setting and obtaining css feature values ​​under special circumstances. The structure is as follows

Tween.propHooks = {
  _default: {
    get: function(){...},
    set: function(){...}
  },
  scrollTop: {
    set: function(){...}
  }
  scrollLeft: {
    set: function(){...}
  }
} 

Tween.propHooks.scrollTop and Tween.propHooks.scrollLeft are mainly caused by confusion when IE8 is offline and the css feature value is saved to the node

set: function( tween ) {
  if ( tween.elem.nodeType && tween.elem.parentNode ) {
    tween.elem[ tween.prop ] = tween.now;
  }
} 

The get method of Tween.propHooks._default will try to obtain the tween.prop feature value of css directly from the node. If it cannot be obtained, use jQuery.css() method to obtain it. During this method processing, simple values ​​such as "10px" will be parsed as floating point numbers; complex values ​​such as "rotation (1rad)" will be returned as they are. And then process the returned results: empty strings, null, undefined and "auto" are converted to 0; other conditions remain unchanged.

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;
} 

The set method of Tween.propHooks._default will first try jQuery.fx.step[ tween.prop ] to set the backward compatibility; otherwise, jQuery.style will be used to set the css feature value; in the most extreme case, the feature value will be set Save directly on the node

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. Animation-specific object jQuery.fx

jQuery.fx encapsulates some functions used to perform animation actions. The structure is as follows

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扩展点
} 

Detailed source code analysis is as follows

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 );
  }
}; 

  變數jQuery.timers = [];用來保存每次tick需要執行的函數清單。一般來說就只有一個函數,就是Animation函數中定義的tick函數。 jQuery.fx.interval可以用來設定動畫每兩個畫面之間的時間間隔,預設為13毫秒。

  動畫的分析就到這裡。下面把動畫相關的api列一下

jQuery.fn.show([ duration ] [, easing ] [, complete ] | options )(顯示所有符合的元素。此外,你也可以指定元素顯示的過渡動畫效果。如果元素本身是可見的,則不對其作任何變更。

jQuery.fn.hide([ duration ] [, easing ] [, complete ] | options)(隱藏所有符合的元素。此外,你也可以指定元素隱藏的過渡動畫效果。如果元素本身是不可見的,則不對其作任何更改。


jQuery.fn.toggle([ duration ] [, easing ] [, complete ] | options)(切換所有符合的元素。此外,你還可以指定元素切換的過渡動畫效果。所謂"切換",也就是如果元素目前是可見的,則將其隱藏;如果元素目前是隱藏的,則使其顯示(可見)。

這裡介紹的toggle()函數用來切換元素的顯示/隱藏。 jQuery還有一個同名的事件函數toggle(),用於綁定click事件並在觸發時輪流切換執行不同的事件處理函數。

jQuery.fn.slideDown([ duration ] [, easing ] [, complete ] | options)(顯示所有匹配的元素,並帶有向下滑動的過渡動畫效果。向下滑動的動畫效果,即元素可見區域的高度從0逐漸增大到其原有高度(向下逐漸展開)。

與該函數相對的是slideUp()函數,用於隱藏所有匹配的元素,並帶有向上滑動的過渡動畫效果)


jQuery.fn.slideUp([ duration ] [, easing ] [, complete ] | options)(隱藏所有匹配的元素,並帶有向上滑動的過渡動畫效果。向上滑動的動畫效果,即元素可見區域的高度從原有高度逐漸減小到0(向上逐漸收起)。


jQuery.fn.slideToggle([ duration ] [, easing ] [, complete ] | options)(切換所有匹配的元素,並帶有滑動的過渡動畫效果。所謂"切換",也就是如果元素當前是可見的,則將其隱藏(向上滑動);如果元素目前是隱藏的,則使其顯示(向下滑動))


jQuery.fn.fadeIn([ duration ] [, easing ] [, complete ] | options)(顯示所有匹配的元素,並帶有淡入的過渡動畫效果。淡入的動畫效果,即元素的不透明度的比例從0%逐漸增加到100%。匹配的元素,並帶有淡出的過渡動畫效果)


jQuery.fn.fadeOut([ duration ] [, easing ] [, complete ] | options)(隱藏所有匹配的元素,並帶有淡出的過渡動畫效果。所謂"淡出"的動畫效果,即元素的不透明度的比例從100%逐漸減小到0%。

jQuery.fn.fadeToggle([ duration ] [, easing ] [, complete ] | options)(切換所有匹配的元素,並帶有淡入/淡出的過渡動畫效果。所謂"切換",即如果元素當前是可見的,則將其隱藏(淡出);如果元素目前是隱藏的,則使其顯示(淡入))

jQuery.fn.animate(cssProperties [, duration ] [, easing ] [, complete ] | cssProperties, options)(執行一個基於css屬性的自訂動畫。你可以為匹配的元素設定css樣式,animate( )函數將會執行一個從目前樣式到指定的css樣式的一個過渡動畫。的高度從100px逐漸增加到200px的過渡動畫)

jQuery.fn.delay(duration [, queueName ])(延遲佇列中下一項的執行。delay()可以將佇列中等待執行的下一個動畫延遲指定的時間後才執行。它常用在佇列中的兩個jQuery效果函數之間,從而在上一個動畫效果執行後延遲下一個動畫效果的執行時間。對它進行延遲呼叫)

jQuery.fn.stop([ queueName ] [, clearQueue [, jumpToEnd ] ])(停止目前在匹配元素上正在運行的動畫。預設情況下,stop()函數只會停止目前正在運行的動畫。如果你使用animate()函數為目前元素設定了A、B、C這3段動畫,如果目前正在執行的動畫是A,則只會停止動畫A的執行,不會阻止動畫B和C的執行。 ,你也可以透過指定可選的選項參數來停止所有的動畫。 停止動畫並不是恢復到該動畫執行前的狀況,而是直接停止,當前動畫執行到什麼狀態,就停留在什麼狀態。執行一個元素高度從100px到200px的過渡動畫,當高度為150px時停止了該動畫,則當前高度仍然保持150px的現狀。 。

jQuery.fn.finish([ queueName ])(立即完成佇列中的所有動畫。finish()會停止目前正在執行的動畫,刪除所有佇列中的動畫,並完成所有符合元素的動畫)


jQuery.fn. fadeTo([speed,]opacity[,callback])(將被選元素的不透明度逐漸地改變為指定的值)


jQuery.fx.off(該屬性用於設定或傳回是否全域性地停用所有動畫。如果不對該屬性設定值,則傳回表示是否全域性停用了動畫效果的布林值。如果將該屬性設為true,將全域性停用所有動畫。 false,將全域性地啟用動畫效果。


你可以在遇到以下情況時,需要停用動畫效果:你在配置比較低的電腦上使用jQuery;某些使用者可能因為動畫效果而遇到了可訪問性問題。 )


jQuery.fx.interval(此屬性用於設定或返回動畫的幀速(毫秒值)。jQuery.fx.interval屬性用於設定jQuery動畫每隔多少毫秒繪製一幀圖像(觸發一次樣式更改,瀏覽器可能會重新繪製目前頁面)。將不受影響。

以上內容是腳本之家小編給大家介紹的Jquery1.9.1源碼分析系列(十五)動畫處理之外篇,
jQuery 1.9.1源碼分析系列(十五)之動畫處理

,點選了解詳情。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment