e4893d6fb938a8bd51564a028c4c43172e1a20fa
[jquery.git] / src / effects.js
1 (function( jQuery ) {
2
3 var elemdisplay = {},
4         rfxtypes = /^(?:toggle|show|hide)$/,
5         rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/,
6         timerId,
7         fxAttrs = [
8                 // height animations
9                 [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
10                 // width animations
11                 [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
12                 // opacity animations
13                 [ "opacity" ]
14         ];
15
16 function defaultDisplay(nodeName) {
17         if ( !elemdisplay[ nodeName ] ) {
18                 var elem = jQuery("<" + nodeName + ">").appendTo("body"),
19                         display = elem.css("display");
20
21                 elem.remove();
22
23                 if ( display === "none" || display === "" ) {
24                         display = "block";
25                 }
26
27                 elemdisplay[ nodeName ] = display;
28         }
29
30         return elemdisplay[ nodeName ];
31 }
32
33 jQuery.fn.extend({
34         show: function( speed, easing, callback ) {
35                 if ( speed || speed === 0 ) {
36                         return this.animate( genFx("show", 3), speed, easing, callback);
37                 } else {
38                         for ( var i = 0, j = this.length; i < j; i++ ) {
39                                 // Set elements which have been overridden with display: none
40                                 // in a stylesheet to whatever the default browser style is
41                                 // for such an element
42                                 if ( jQuery.css( this[i], "display" ) === "none" && this[i].style.display !== "none" ) {
43                                         jQuery.data(this[i], "olddisplay", defaultDisplay(this[i].nodeName));
44                                 }
45                         }
46
47                         // Set the display of the elements in a second loop
48                         // to avoid the constant reflow
49                         for ( i = 0, j = this.length; i < j; i++ ) {
50                                 this[i].style.display = jQuery.data(this[i], "olddisplay") || "";
51                         }
52
53                         return this;
54                 }
55         },
56
57         hide: function( speed, easing, callback ) {
58                 if ( speed || speed === 0 ) {
59                         return this.animate( genFx("hide", 3), speed, easing, callback);
60
61                 } else {
62                         for ( var i = 0, l = this.length; i < l; i++ ) {
63                                 var old = jQuery.data(this[i], "olddisplay");
64                                 if ( !old && old !== "none" ) {
65                                         jQuery.data( this[i], "olddisplay", jQuery.css( this[i], "display" ) );
66                                 }
67                         }
68
69                         // Set the display of the elements in a second loop
70                         // to avoid the constant reflow
71                         for ( var j = 0, k = this.length; j < k; j++ ) {
72                                 this[j].style.display = "none";
73                         }
74
75                         return this;
76                 }
77         },
78
79         // Save the old toggle function
80         _toggle: jQuery.fn.toggle,
81
82         toggle: function( fn, fn2, callback ) {
83                 var bool = typeof fn === "boolean";
84
85                 if ( jQuery.isFunction(fn) && jQuery.isFunction(fn2) ) {
86                         this._toggle.apply( this, arguments );
87
88                 } else if ( fn == null || bool ) {
89                         this.each(function() {
90                                 var state = bool ? fn : jQuery(this).is(":hidden");
91                                 jQuery(this)[ state ? "show" : "hide" ]();
92                         });
93
94                 } else {
95                         this.animate(genFx("toggle", 3), fn, fn2, callback);
96                 }
97
98                 return this;
99         },
100
101         fadeTo: function( speed, to, easing, callback ) {
102                 return this.filter(":hidden").css("opacity", 0).show().end()
103                                         .animate({opacity: to}, speed, easing, callback);
104         },
105
106         animate: function( prop, speed, easing, callback ) {
107                 var optall = jQuery.speed(speed, easing, callback);
108
109                 if ( jQuery.isEmptyObject( prop ) ) {
110                         return this.each( optall.complete );
111                 }
112
113                 return this[ optall.queue === false ? "each" : "queue" ](function() {
114                         // XXX ‘this’ does not always have a nodeName when running the
115                         // test suite
116
117                         var opt = jQuery.extend({}, optall), p,
118                                 hidden = this.nodeType === 1 && jQuery(this).is(":hidden"),
119                                 self = this;
120
121                         for ( p in prop ) {
122                                 var name = jQuery.camelCase( p );
123
124                                 if ( p !== name ) {
125                                         prop[ name ] = prop[ p ];
126                                         delete prop[ p ];
127                                         p = name;
128                                 }
129
130                                 if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) {
131                                         return opt.complete.call(this);
132                                 }
133
134                                 if ( ( p === "height" || p === "width" ) ) {
135                                         // Make sure that nothing sneaks out
136                                         // Record all 3 overflow attributes because IE does not
137                                         // change the overflow attribute when overflowX and
138                                         // overflowY are set to the same value
139                                         opt.overflow = [ this.style.overflow, this.style.overflowX, this.style.overflowY ];
140
141                                         // Set display property to inline-block for height/width
142                                         // animations on inline elements that are having width/height
143                                         // animated
144                                         if ( jQuery.curCSS( this, "display" ) === "inline" &&
145                                         jQuery.curCSS( this, "float" ) === "none" ) {
146                                                 if ( !jQuery.support.inlineBlockNeedsLayout ) {
147                                                         this.style.display = "inline-block";
148                                                 } else {
149                                                         var display = defaultDisplay(this.nodeName);
150
151                                                         // inline-level elements accept inline-block;
152                                                         // block-level elements need to be inline with layout
153                                                         if ( display === "inline" ) {
154                                                                 this.style.display = "inline-block";
155                                                         }
156                                                         else {
157                                                                 this.style.display = "inline";
158                                                                 jQuery.data( this, "oldzoom", this.style.zoom );
159                                                                 this.style.zoom = 1;
160                                                         }
161                                                 }
162                                         }
163                                 }
164
165                                 if ( jQuery.isArray( prop[p] ) ) {
166                                         // Create (if needed) and add to specialEasing
167                                         (opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1];
168                                         prop[p] = prop[p][0];
169                                 }
170                         }
171
172                         if ( opt.overflow != null ) {
173                                 this.style.overflow = "hidden";
174                         }
175
176                         opt.curAnim = jQuery.extend({}, prop);
177
178                         jQuery.each( prop, function( name, val ) {
179                                 var e = new jQuery.fx( self, opt, name );
180
181                                 if ( rfxtypes.test(val) ) {
182                                         e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop );
183
184                                 } else {
185                                         var parts = rfxnum.exec(val),
186                                                 start = e.cur(true) || 0;
187
188                                         if ( parts ) {
189                                                 var end = parseFloat( parts[2] ),
190                                                         unit = parts[3] || "px";
191
192                                                 // We need to compute starting value
193                                                 if ( unit !== "px" ) {
194                                                         self.style[ name ] = (end || 1) + unit;
195                                                         start = ((end || 1) / e.cur(true)) * start;
196                                                         self.style[ name ] = start + unit;
197                                                 }
198
199                                                 // If a +=/-= token was provided, we're doing a relative animation
200                                                 if ( parts[1] ) {
201                                                         end = ((parts[1] === "-=" ? -1 : 1) * end) + start;
202                                                 }
203
204                                                 e.custom( start, end, unit );
205
206                                         } else {
207                                                 e.custom( start, val, "" );
208                                         }
209                                 }
210                         });
211
212                         // For JS strict compliance
213                         return true;
214                 });
215         },
216
217         stop: function( clearQueue, gotoEnd ) {
218                 var timers = jQuery.timers;
219
220                 if ( clearQueue ) {
221                         this.queue([]);
222                 }
223
224                 this.each(function() {
225                         // go in reverse order so anything added to the queue during the loop is ignored
226                         for ( var i = timers.length - 1; i >= 0; i-- ) {
227                                 if ( timers[i].elem === this ) {
228                                         if (gotoEnd) {
229                                                 // force the next step to be the last
230                                                 timers[i](true);
231                                         }
232
233                                         timers.splice(i, 1);
234                                 }
235                         }
236                 });
237
238                 // start the next in the queue if the last step wasn't forced
239                 if ( !gotoEnd ) {
240                         this.dequeue();
241                 }
242
243                 return this;
244         }
245
246 });
247
248 function genFx( type, num ) {
249         var obj = {};
250
251         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
252                 obj[ this ] = type;
253         });
254
255         return obj;
256 }
257
258 // Generate shortcuts for custom animations
259 jQuery.each({
260         slideDown: genFx("show", 1),
261         slideUp: genFx("hide", 1),
262         slideToggle: genFx("toggle", 1),
263         fadeIn: { opacity: "show" },
264         fadeOut: { opacity: "hide" }
265 }, function( name, props ) {
266         jQuery.fn[ name ] = function( speed, easing, callback ) {
267                 return this.animate( props, speed, easing, callback );
268         };
269 });
270
271 jQuery.extend({
272         speed: function( speed, easing, fn ) {
273                 var opt = speed && typeof speed === "object" ? jQuery.extend({}, speed) : {
274                         complete: fn || !fn && easing ||
275                                 jQuery.isFunction( speed ) && speed,
276                         duration: speed,
277                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
278                 };
279
280                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
281                         opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[opt.duration] : jQuery.fx.speeds._default;
282
283                 // Queueing
284                 opt.old = opt.complete;
285                 opt.complete = function() {
286                         if ( opt.queue !== false ) {
287                                 jQuery(this).dequeue();
288                         }
289                         if ( jQuery.isFunction( opt.old ) ) {
290                                 opt.old.call( this );
291                         }
292                 };
293
294                 return opt;
295         },
296
297         easing: {
298                 linear: function( p, n, firstNum, diff ) {
299                         return firstNum + diff * p;
300                 },
301                 swing: function( p, n, firstNum, diff ) {
302                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
303                 }
304         },
305
306         timers: [],
307
308         fx: function( elem, options, prop ) {
309                 this.options = options;
310                 this.elem = elem;
311                 this.prop = prop;
312
313                 if ( !options.orig ) {
314                         options.orig = {};
315                 }
316         }
317
318 });
319
320 jQuery.fx.prototype = {
321         // Simple function for setting a style value
322         update: function() {
323                 if ( this.options.step ) {
324                         this.options.step.call( this.elem, this.now, this );
325                 }
326
327                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
328         },
329
330         // Get the current size
331         cur: function() {
332                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
333                         return this.elem[ this.prop ];
334                 }
335
336                 var r = parseFloat( jQuery.css( this.elem, this.prop ) );
337                 return r && r > -10000 ? r : 0;
338         },
339
340         // Start an animation from one number to another
341         custom: function( from, to, unit ) {
342                 this.startTime = jQuery.now();
343                 this.start = from;
344                 this.end = to;
345                 this.unit = unit || this.unit || "px";
346                 this.now = this.start;
347                 this.pos = this.state = 0;
348
349                 var self = this, fx = jQuery.fx;
350                 function t( gotoEnd ) {
351                         return self.step(gotoEnd);
352                 }
353
354                 t.elem = this.elem;
355
356                 if ( t() && jQuery.timers.push(t) && !timerId ) {
357                         timerId = setInterval(fx.tick, fx.interval);
358                 }
359         },
360
361         // Simple 'show' function
362         show: function() {
363                 // Remember where we started, so that we can go back to it later
364                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
365                 this.options.show = true;
366
367                 // Begin the animation
368                 // Make sure that we start at a small width/height to avoid any
369                 // flash of content
370                 this.custom(this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur());
371
372                 // Start by showing the element
373                 jQuery( this.elem ).show();
374         },
375
376         // Simple 'hide' function
377         hide: function() {
378                 // Remember where we started, so that we can go back to it later
379                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
380                 this.options.hide = true;
381
382                 // Begin the animation
383                 this.custom(this.cur(), 0);
384         },
385
386         // Each step of an animation
387         step: function( gotoEnd ) {
388                 var t = jQuery.now(), done = true;
389
390                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
391                         this.now = this.end;
392                         this.pos = this.state = 1;
393                         this.update();
394
395                         this.options.curAnim[ this.prop ] = true;
396
397                         for ( var i in this.options.curAnim ) {
398                                 if ( this.options.curAnim[i] !== true ) {
399                                         done = false;
400                                 }
401                         }
402
403                         if ( done ) {
404                                 // Reset the overflow
405                                 if ( this.options.overflow != null ) {
406                                         this.elem.style.overflow = this.options.overflow[0];
407                                         this.elem.style.overflowX = this.options.overflow[1];
408                                         this.elem.style.overflowY = this.options.overflow[2];
409                                 }
410
411                                 // Hide the element if the "hide" operation was done
412                                 if ( this.options.hide ) {
413                                         jQuery(this.elem).hide();
414                                 }
415
416                                 // Reset the properties, if the item has been hidden or shown
417                                 if ( this.options.hide || this.options.show ) {
418                                         for ( var p in this.options.curAnim ) {
419                                                 jQuery.style( this.elem, p, this.options.orig[p] );
420                                         }
421                                 }
422
423                                 // Execute the complete function
424                                 this.options.complete.call( this.elem );
425                         }
426
427                         return false;
428
429                 } else {
430                         var n = t - this.startTime;
431                         this.state = n / this.options.duration;
432
433                         // Perform the easing function, defaults to swing
434                         var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop];
435                         var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear");
436                         this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration);
437                         this.now = this.start + ((this.end - this.start) * this.pos);
438
439                         // Perform the next step of the animation
440                         this.update();
441                 }
442
443                 return true;
444         }
445 };
446
447 jQuery.extend( jQuery.fx, {
448         tick: function() {
449                 var timers = jQuery.timers;
450
451                 for ( var i = 0; i < timers.length; i++ ) {
452                         if ( !timers[i]() ) {
453                                 timers.splice(i--, 1);
454                         }
455                 }
456
457                 if ( !timers.length ) {
458                         jQuery.fx.stop();
459                 }
460         },
461   
462         interval: 13,
463                 
464         stop: function() {
465                 clearInterval( timerId );
466                 timerId = null;
467         },
468         
469         speeds: {
470                 slow: 600,
471                 fast: 200,
472                 // Default speed
473                 _default: 400
474         },
475
476         step: {
477                 opacity: function( fx ) {
478                         jQuery.style( fx.elem, "opacity", fx.now );
479                 },
480
481                 _default: function( fx ) {
482                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
483                                 fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
484                         } else {
485                                 fx.elem[ fx.prop ] = fx.now;
486                         }
487                 }
488         }
489 });
490
491 if ( jQuery.expr && jQuery.expr.filters ) {
492         jQuery.expr.filters.animated = function( elem ) {
493                 return jQuery.grep(jQuery.timers, function( fn ) {
494                         return elem === fn.elem;
495                 }).length;
496         };
497 }
498
499 })( jQuery );