本文帶大家一起走進 jQuery 1.5.1 中 animate 方法的源碼,逐段拆解它的執行邏輯。以下為源碼及註釋:
/*7536-7646*/
animate: function( prop, speed, easing, callback ) {
if ( jQuery.isEmptyObject( prop ) ) {
return this.each( optall.complete );
}
//#7864行 this.options.complete.call( this.elem ) 使得其可以不斷地連續執行動畫,比如 $('selector').animate({prop1},speed1).animate({prop2},speed2) 這樣的動畫隊列;
return this[ optall.queue === false ? "each" : "queue" ](function() {
// XXX 'this' does not always have a nodeName when running the
// test suite
var opt = jQuery.extend({}, optall), p,
isElement = this.nodeType === 1,
hidden = isElement && jQuery(this).is(":hidden"),
self = this;
//要執行動畫的prop,prop一般是一個plainObj,形如{key1:value1,key2:value2};
for ( p in prop ) {
//駝峰改寫,有些比如 margin-top 需要變成駝峰的屬性即變成 marginTop;見 camelCase 方法;
var name = jQuery.camelCase( p );
//fix屬性;主要是前面 camelCase 的屬性;
if ( p !== name ) {
prop[ name ] = prop[ p ];
delete prop[ p ];
p = name;
}
//如果執行 $(..).show||$(..).hide;如果這個元素本身是 hidden,而動畫裡面又寫 hide,直接運行 callbacks 就可以了;
if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) {
return opt.complete.call(this);
}
//如果 prop[key]==(height||width)並且是一個 dom 元素;需要有些特殊的處理;
if ( isElement && ( p === "height" || p === "width" ) ) {
// Make sure that nothing sneaks out
// Record all 3 overflow attributes because IE does not
// change the overflow attribute when overflowX and
// overflowY are set to the same value
opt.overflow = [ this.style.overflow, this.style.overflowX, this.style.overflowY ];
// Set display property to inline-block for height/width
// animations on inline elements that are having width/height
// animated
if ( jQuery.css( this, "display" ) === "inline" &&
jQuery.css( this, "float" ) === "none" ) {
if ( !jQuery.support.inlineBlockNeedsLayout ) {
this.style.display = "inline-block";
} else {
var display = defaultDisplay(this.nodeName);
// inline-level elements accept inline-block;
// block-level elements need to be inline with layout
if ( display === "inline" ) {
this.style.display = "inline-block";
} else {
this.style.display = "inline";
this.style.zoom = 1;
}
}
}
}
//如果 prop[key] 是一個數組;只用第一個值 prop[p][0];
if ( jQuery.isArray( prop[p] ) ) {
// Create (if needed) and add to specialEasing
(opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1];
prop[p] = prop[p][0];
}
}
if ( opt.overflow != null ) {
//如果動畫元素的 overflow 已經被設置的情況下,把它暫時為 hidden;
this.style.overflow = "hidden";
}
//當前動畫鍵值對,其實就是 prop;
opt.curAnim = jQuery.extend({}, prop);
//這裡便是動畫的核心了,對每一個 prop[key] 進行處理;
jQuery.each( prop, function( name, val ) {
//獲取一個 Fx 對象;傳入的每一個參數都被設置成為這個對象的屬性;其中 self 是指動畫元素自身;opt 是前面產生的對象;
var e = new jQuery.fx( self, opt, name );
//當執行 show||hide 操作的時候 prop==fxAttrs(參見 show||hide 方法)
if ( rfxtypes.test(val) ) {
e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop );
} else {
var parts = rfxnum.exec(val),
//start 保存了初始值,它可能在 style,也可能在 css 中,如果該值==null,undefined,auto,0 等將被設置為 0;
start = e.cur();
if ( parts ) {
//end 是指變化量的大小,比如:{left: -=66px},那麼 end=66;
var end = parseFloat( parts[2] ),
//單元運算符,就是 px,%;如果是一些不能帶單位的,比如 z-index,設置為空,否則就設置為 px;
unit = parts[3] || ( jQuery.cssNumber[ name ] ? "" : "px" );
// We need to compute starting value
//如果不是 px,比如 %,em 等等;
if ( unit !== "px" ) {
//設置該屬性值 name 為 (end || 1) + unit,如果 end=0;設置為 1;開始值被設置為 start = ((end || 1) / e.cur()) * start;
jQuery.style( self, name, (end || 1) + unit);
//這裡 e.cur() 和前面的 start = e.cur(); 是不一樣的,因為 jQuery.style( self, name, (end || 1) + unit) 的執行使得 start 被改變;用於處理 end=0 的情況;因為 e.cur() 作為除數,不能為 0;
start = ((end || 1) / e.cur()) * start;
jQuery.style( self, name, start + unit);
}
// If a +=/-= token was provided, we're doing a relative animation
if ( parts[1] ) {
//end 相應的被設置為運算後的變量值;
end = ((parts[1] === "-=" ? -1 : 1) * end) + start;
}
e.custom( start, end, unit );
//如果沒有數字化的運算;那麼沒傳入的只能是 '';
} else {
e.custom( start, val, "" );
}
}
});
// For JS strict compliance
return true;
});
},
整段源碼的核心思路是:先遍歷傳入的屬性對象 prop,對每個屬性做駝峰化與特殊處理,再為每個屬性創建一個 jQuery.fx 對象,通過 e.custom() 驅動動畫從起始值過渡到目標值。理解這條主線,再看細節分支就會清晰很多。