前回から今回までの間に、
- イベントバンドリングが103%高速化
- CSSセレクタの13%高速化
- offset() 21%高速化
- css() 25%高速化
それでは、
jQuery.param()
2834: // Serialize an array of form elements or a set of
2835: // key/values into a query string
2836: param: function( a ) {
2837: var s = [];
2838:
2839: // If an array was passed in, assume that it is an array
2840: // of form elements
2841: if ( a.constructor == Array || a.jquery )
2842: // Serialize the form elements
2843: jQuery.each( a, function(){
2844: s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
2845: });
2846:
2847: // Otherwise, assume that it's an object of key/value pairs
2848: else
2849: // Serialize the key/values
2850: for ( var j in a )
2851: // If the value is an array then the key names need to be repeated
2852: if ( a[j] && a[j].constructor == Array )
2853: jQuery.each( a[j], function(){
2854: s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
2855: });
2856: else
2857: s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
2858:
2859: // Return the resulting serialization
2860: return s.join("&").replace(/%20/g, "+");
2861: }
2862:
2863: });
2834行目からは、
2837行目は、
2841~2845行目は、
2848~2857行目は、
最後に2860行目で、
jQuery.fn.show()
2864: jQuery.fn.extend({
2865: show: function(speed,callback){
2866: return speed ?
2867: this.animate({
2868: height: "show", width: "show", opacity: "show"
2869: }, speed, callback) :
2870:
2871: this.filter(":hidden").each(function(){
2872: this.style.display = this.oldblock || "";
2873: if ( jQuery.css(this,"display") == "none" ) {
2874: var elem = jQuery(" + this.tagName + " />").appendTo("body");
2875: this.style.display = elem.css("display");
2876: // handle an edge condition where css is - div { display:none; } or similar
2877: if (this.style.display == "none")
2878: this.style.display = "block";
2879: elem.remove();
2880: }
2881: }).end();
2882: },
2883:
jQuery.
第1引数speedが指定されていれば、
speed引数が指定されていない場合は、
jQuery.fn.hide()
2884: hide: function(speed,callback){
2885: return speed ?
2886: this.animate({
2887: height: "hide", width: "hide", opacity: "hide"
2888: }, speed, callback) :
2889:
2890: this.filter(":visible").each(function(){
2891: this.oldblock = this.oldblock || jQuery.css(this,"display");
2892: this.style.display = "none";
2893: }).end();
2894: },
2895:
jQuery.
第1引数speedが指定されていれば、
speed引数が指定されていない場合は、
jQuery.fn.toggle()
2896: // Save the old toggle function
2897: _toggle: jQuery.fn.toggle,
2898:
2899: toggle: function( fn, fn2 ){
2900: return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
2901: this._toggle( fn, fn2 ) :
2902: fn ?
2903: this.animate({
2904: height: "toggle", width: "toggle", opacity: "toggle"
2905: }, fn, fn2) :
2906: this.each(function(){
2907: jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
2908: });
2909: },
2910:
jQuery.
まず、
fn引数が指定されていない場合には、
jQuery.fn.slideDown()
2911: slideDown: function(speed,callback){
2912: return this.animate({height: "show"}, speed, callback);
2913: },
2914:
jQuery.
実際の処理としては、
jQuery.fn.slideUp()
2915: slideUp: function(speed,callback){
2916: return this.animate({height: "hide"}, speed, callback);
2917: },
2918:
jQuery.
実際の処理としては、
jQuery.fn.slideToggle()
2919: slideToggle: function(speed, callback){
2920: return this.animate({height: "toggle"}, speed, callback);
2921: },
2922:
jQuery.
実際の処理としては、
jQuery.fn.fadeIn()
2923: fadeIn: function(speed, callback){
2924: return this.animate({opacity: "show"}, speed, callback);
2925: },
2926:
jQuery.
実際の処理としては、
jQuery.fn.fadeOut()
2927: fadeOut: function(speed, callback){
2928: return this.animate({opacity: "hide"}, speed, callback);
2929: },
2930:
jQuery.
実際の処理としては、
jQuery.fn.fadeTo()
2931: fadeTo: function(speed,to,callback){
2932: return this.animate({opacity: to}, speed, callback);
2933: },
2934:
jQuery.
実際の処理としては、
jQuery.fn.animate()
2935: animate: function( prop, speed, easing, callback ) {
2936: var optall = jQuery.speed(speed, easing, callback);
2937:
2938: return this[ optall.queue === false ? "each" : "queue" ](function(){
2939: if ( this.nodeType != 1)
2940: return false;
2941:
2942: var opt = jQuery.extend({}, optall);
2943: var hidden = jQuery(this).is(":hidden"), self = this;
2944:
2945: for ( var p in prop ) {
2946: if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
2947: return jQuery.isFunction(opt.complete) && opt.complete.apply(this);
2948:
2949: if ( p == "height" || p == "width" ) {
2950: // Store display property
2951: opt.display = jQuery.css(this, "display");
2952:
2953: // Make sure that nothing sneaks out
2954: opt.overflow = this.style.overflow;
2955: }
2956: }
2957:
2958: if ( opt.overflow != null )
2959: this.style.overflow = "hidden";
2960:
2961: opt.curAnim = jQuery.extend({}, prop);
2962:
2963: jQuery.each( prop, function(name, val){
2964: var e = new jQuery.fx( self, opt, name );
2965:
2966: if ( /toggle|show|hide/.test(val) )
2967: e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
2968: else {
2969: var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
2970: start = e.cur(true) || 0;
2971:
2972: if ( parts ) {
2973: var end = parseFloat(parts[2]),
2974: unit = parts[3] || "px";
2975:
2976: // We need to compute starting value
2977: if ( unit != "px" ) {
2978: self.style[ name ] = (end || 1) + unit;
2979: start = ((end || 1) / e.cur(true)) * start;
2980: self.style[ name ] = start + unit;
2981: }
2982:
2983: // If a +=/-= token was provided, we're doing a relative animation
2984: if ( parts[1] )
2985: end = ((parts[1] == "-=" ? -1 : 1) * end) + start;
2986:
2987: e.custom( start, end, unit );
2988: } else
2989: e.custom( start, val, "" );
2990: }
2991: });
2992:
2993: // For JS strict compliance
2994: return true;
2995: });
2996: },
2997:
jQuery.
2938行目は、
2939行目は、
2942行目は、
2943行目は、
2945~2956行目で、
2958行目は、
2961行目は、
2963行目からは、
2964行目では、
2966行目は、
2969行目は、
2972行目のif文により、
2977行目は、
2984行目は、
ここまでで計算した開始値と終了値を使って、
return文に値を返すために、
jQuery.fn.queue()
2998: queue: function(type, fn){
2999: if ( jQuery.isFunction(type) || ( type && type.constructor == Array )) {
3000: fn = type;
3001: type = "fx";
3002: }
3003:
3004: if ( !type || (typeof type == "string" && !fn) )
3005: return queue( this[0], type );
3006:
3007: return this.each(function(){
3008: if ( fn.constructor == Array )
3009: queue(this, type, fn);
3010: else {
3011: queue(this, type).push( fn );
3012:
3013: if ( queue(this, type).length == 1 )
3014: fn.apply(this);
3015: }
3016: });
3017: },
3018:
jQuery.
3004行目は、
jQuery.fn.stop()
3019: stop: function(clearQueue, gotoEnd){
3020: var timers = jQuery.timers;
3021:
3022: if (clearQueue)
3023: this.queue([]);
3024:
3025: this.each(function(){
3026: // go in reverse order so anything added to the queue during the loop is ignored
3027: for ( var i = timers.length - 1; i >= 0; i-- )
3028: if ( timers[i].elem == this ) {
3029: if (gotoEnd)
3030: // force the next step to be the last
3031: timers[i](true);
3032: timers.splice(i, 1);
3033: }
3034: });
3035:
3036: // start the next in the queue if the last step wasn't forced
3037: if (!gotoEnd)
3038: this.dequeue();
3039:
3040: return this;
3041: }
3042:
3043: });
3044:
jQuery.
引数clearQueueとgotoEndは、
3022行目は、
3025~3034行目のthis.
3026行目にて、
最後に3040行目にて、
queue()
3045: var queue = function( elem, type, array ) {
3046: if ( !elem )
3047: return undefined;
3048:
3049: type = type || "fx";
3050:
3051: var q = jQuery.data( elem, type + "queue" );
3052:
3053: if ( !q || array )
3054: q = jQuery.data( elem, type + "queue",
3055: array ? jQuery.makeArray(array) : [] );
3056:
3057: return q;
3058: };
3059:
queue()は内部的に利用される関数で、
引数elemが指定されていない場合は、
第3引数arrayが渡された場合は、
最後に3057行目にて、
jQuery.fn.dequeue()
3060: jQuery.fn.dequeue = function(type){
3061: type = type || "fx";
3062:
3063: return this.each(function(){
3064: var q = queue(this, type);
3065:
3066: q.shift();
3067:
3068: if ( q.length )
3069: q[0].apply( this );
3070: });
3071: };
3072:
jQuery.
まず3064行目にて、
jQuery.speed()
3073: jQuery.extend({
3074:
3075: speed: function(speed, easing, fn) {
3076: var opt = speed && speed.constructor == Object ? speed : {
3077: complete: fn || !fn && easing ||
3078: jQuery.isFunction( speed ) && speed,
3079: duration: speed,
3080: easing: fn && easing || easing && easing.constructor != Function && easing
3081: };
3082:
3083: opt.duration = (opt.duration && opt.duration.constructor == Number ?
3084: opt.duration :
3085: { slow: 600, fast: 200 }[opt.duration]) || 400;
3086:
3087: // Queueing
3088: opt.old = opt.complete;
3089: opt.complete = function(){
3090: if ( opt.queue !== false )
3091: jQuery(this).dequeue();
3092: if ( jQuery.isFunction( opt.old ) )
3093: opt.old.apply( this );
3094: };
3095:
3096: return opt;
3097: },
3098:
jQuery.
3076行目により、
3083行目は、
3088~3094行目は、
jQuery.easing()
3099: easing: {
3100: linear: function( p, n, firstNum, diff ) {
3101: return firstNum + diff * p;
3102: },
3103: swing: function( p, n, firstNum, diff ) {
3104: return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
3105: }
3106: },
3107:
easingは、
デフォルトでは"linear"と"swing"の2つしか定義されていませんが、