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