Added fix for bug #204, yet untested
[jquery.git] / src / fx / fx.js
1 jQuery.fn.extend({\r
2 \r
3         // overwrite the old show method\r
4         _show: jQuery.fn.show,\r
5         \r
6         /**\r
7          * Show all matched elements using a graceful animation.\r
8          * The height, width, and opacity of each of the matched elements \r
9          * are changed dynamically according to the specified speed.\r
10          *\r
11          * @example $("p").show("slow");\r
12          *\r
13          * @name show\r
14          * @type jQuery\r
15          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
16          * @cat Effects/Animations\r
17          */\r
18          \r
19         /**\r
20          * Show all matched elements using a graceful animation and firing a callback\r
21          * function after completion.\r
22          * The height, width, and opacity of each of the matched elements \r
23          * are changed dynamically according to the specified speed.\r
24          *\r
25          * @example $("p").show("slow",function(){\r
26          *   alert("Animation Done.");\r
27          * });\r
28          *\r
29          * @name show\r
30          * @type jQuery\r
31          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
32          * @param Function callback A function to be executed whenever the animation completes.\r
33          * @cat Effects/Animations\r
34          */\r
35         show: function(speed,callback){\r
36                 return speed ? this.animate({\r
37                         height: "show", width: "show", opacity: "show"\r
38                 }, speed, callback) : this._show();\r
39         },\r
40         \r
41         // Overwrite the old hide method\r
42         _hide: jQuery.fn.hide,\r
43         \r
44         /**\r
45          * Hide all matched elements using a graceful animation.\r
46          * The height, width, and opacity of each of the matched elements \r
47          * are changed dynamically according to the specified speed.\r
48          *\r
49          * @example $("p").hide("slow");\r
50          *\r
51          * @name hide\r
52          * @type jQuery\r
53          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
54          * @cat Effects/Animations\r
55          */\r
56          \r
57         /**\r
58          * Hide all matched elements using a graceful animation and firing a callback\r
59          * function after completion.\r
60          * The height, width, and opacity of each of the matched elements \r
61          * are changed dynamically according to the specified speed.\r
62          *\r
63          * @example $("p").hide("slow",function(){\r
64          *   alert("Animation Done.");\r
65          * });\r
66          *\r
67          * @name hide\r
68          * @type jQuery\r
69          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
70          * @param Function callback A function to be executed whenever the animation completes.\r
71          * @cat Effects/Animations\r
72          */\r
73         hide: function(speed,callback){\r
74                 return speed ? this.animate({\r
75                         height: "hide", width: "hide", opacity: "hide"\r
76                 }, speed, callback) : this._hide();\r
77         },\r
78         \r
79         /**\r
80          * Reveal all matched elements by adjusting their height.\r
81          * Only the height is adjusted for this animation, causing all matched\r
82          * elements to be revealed in a "sliding" manner.\r
83          *\r
84          * @example $("p").slideDown("slow");\r
85          *\r
86          * @name slideDown\r
87          * @type jQuery\r
88          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
89          * @cat Effects/Animations\r
90          */\r
91          \r
92         /**\r
93          * Reveal all matched elements by adjusting their height and firing a callback\r
94          * function after completion.\r
95          * Only the height is adjusted for this animation, causing all matched\r
96          * elements to be revealed in a "sliding" manner.\r
97          *\r
98          * @example $("p").slideDown("slow",function(){\r
99          *   alert("Animation Done.");\r
100          * });\r
101          *\r
102          * @name slideDown\r
103          * @type jQuery\r
104          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
105          * @param Function callback A function to be executed whenever the animation completes.\r
106          * @cat Effects/Animations\r
107          */\r
108         slideDown: function(speed,callback){\r
109                 return this.animate({height: "show"}, speed, callback);\r
110         },\r
111         \r
112         /**\r
113          * Hide all matched elements by adjusting their height.\r
114          * Only the height is adjusted for this animation, causing all matched\r
115          * elements to be hidden in a "sliding" manner.\r
116          *\r
117          * @example $("p").slideUp("slow");\r
118          *\r
119          * @name slideUp\r
120          * @type jQuery\r
121          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
122          * @cat Effects/Animations\r
123          */\r
124          \r
125         /**\r
126          * Hide all matched elements by adjusting their height and firing a callback\r
127          * function after completion.\r
128          * Only the height is adjusted for this animation, causing all matched\r
129          * elements to be hidden in a "sliding" manner.\r
130          *\r
131          * @example $("p").slideUp("slow",function(){\r
132          *   alert("Animation Done.");\r
133          * });\r
134          *\r
135          * @name slideUp\r
136          * @type jQuery\r
137          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
138          * @param Function callback A function to be executed whenever the animation completes.\r
139          * @cat Effects/Animations\r
140          */\r
141         slideUp: function(speed,callback){\r
142                 return this.animate({height: "hide"}, speed, callback);\r
143         },\r
144 \r
145         /**\r
146          * Toggle the visibility of all matched elements by adjusting their height.\r
147          * Only the height is adjusted for this animation, causing all matched\r
148          * elements to be hidden in a "sliding" manner.\r
149          *\r
150          * @example $("p").slideToggle("slow");\r
151          *\r
152          * @name slideToggle\r
153          * @type jQuery\r
154          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
155          * @cat Effects/Animations\r
156          */\r
157          \r
158         /**\r
159          * Toggle the visibility of all matched elements by adjusting their height\r
160          * and firing a callback function after completion.\r
161          * Only the height is adjusted for this animation, causing all matched\r
162          * elements to be hidden in a "sliding" manner.\r
163          *\r
164          * @example $("p").slideToggle("slow",function(){\r
165          *   alert("Animation Done.");\r
166          * });\r
167          *\r
168          * @name slideToggle\r
169          * @type jQuery\r
170          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
171          * @param Function callback A function to be executed whenever the animation completes.\r
172          * @cat Effects/Animations\r
173          */\r
174         slideToggle: function(speed,callback){\r
175                 return this.each(function(){\r
176                         var state = $(this).is(":hidden") ? "show" : "hide";\r
177                         $(this).animate({height: state}, speed, callback);\r
178                 });\r
179         },\r
180         \r
181         /**\r
182          * Fade in all matched elements by adjusting their opacity.\r
183          * Only the opacity is adjusted for this animation, meaning that\r
184          * all of the matched elements should already have some form of height\r
185          * and width associated with them.\r
186          *\r
187          * @example $("p").fadeIn("slow");\r
188          *\r
189          * @name fadeIn\r
190          * @type jQuery\r
191          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
192          * @cat Effects/Animations\r
193          */\r
194          \r
195         /**\r
196          * Fade in all matched elements by adjusting their opacity and firing a \r
197          * callback function after completion.\r
198          * Only the opacity is adjusted for this animation, meaning that\r
199          * all of the matched elements should already have some form of height\r
200          * and width associated with them.\r
201          *\r
202          * @example $("p").fadeIn("slow",function(){\r
203          *   alert("Animation Done.");\r
204          * });\r
205          *\r
206          * @name fadeIn\r
207          * @type jQuery\r
208          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
209          * @param Function callback A function to be executed whenever the animation completes.\r
210          * @cat Effects/Animations\r
211          */\r
212         fadeIn: function(speed,callback){\r
213                 return this.animate({opacity: "show"}, speed, callback);\r
214         },\r
215         \r
216         /**\r
217          * Fade out all matched elements by adjusting their opacity.\r
218          * Only the opacity is adjusted for this animation, meaning that\r
219          * all of the matched elements should already have some form of height\r
220          * and width associated with them.\r
221          *\r
222          * @example $("p").fadeOut("slow");\r
223          *\r
224          * @name fadeOut\r
225          * @type jQuery\r
226          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
227          * @cat Effects/Animations\r
228          */\r
229          \r
230         /**\r
231          * Fade out all matched elements by adjusting their opacity and firing a \r
232          * callback function after completion.\r
233          * Only the opacity is adjusted for this animation, meaning that\r
234          * all of the matched elements should already have some form of height\r
235          * and width associated with them.\r
236          *\r
237          * @example $("p").fadeOut("slow",function(){\r
238          *   alert("Animation Done.");\r
239          * });\r
240          *\r
241          * @name fadeOut\r
242          * @type jQuery\r
243          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
244          * @param Function callback A function to be executed whenever the animation completes.\r
245          * @cat Effects/Animations\r
246          */\r
247         fadeOut: function(speed,callback){\r
248                 return this.animate({opacity: "hide"}, speed, callback);\r
249         },\r
250         \r
251         /**\r
252          * Fade the opacity of all matched elements to a specified opacity.\r
253          * Only the opacity is adjusted for this animation, meaning that\r
254          * all of the matched elements should already have some form of height\r
255          * and width associated with them.\r
256          *\r
257          * @example $("p").fadeTo("slow", 0.5);\r
258          *\r
259          * @name fadeTo\r
260          * @type jQuery\r
261          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
262          * @param Number opacity The opacity to fade to (a number from 0 to 1).\r
263          * @cat Effects/Animations\r
264          */\r
265          \r
266         /**\r
267          * Fade the opacity of all matched elements to a specified opacity and \r
268          * firing a callback function after completion.\r
269          * Only the opacity is adjusted for this animation, meaning that\r
270          * all of the matched elements should already have some form of height\r
271          * and width associated with them.\r
272          *\r
273          * @example $("p").fadeTo("slow", 0.5, function(){\r
274          *   alert("Animation Done.");\r
275          * });\r
276          *\r
277          * @name fadeTo\r
278          * @type jQuery\r
279          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
280          * @param Number opacity The opacity to fade to (a number from 0 to 1).\r
281          * @param Function callback A function to be executed whenever the animation completes.\r
282          * @cat Effects/Animations\r
283          */\r
284         fadeTo: function(speed,to,callback){\r
285                 return this.animate({opacity: to}, speed, callback);\r
286         },\r
287         \r
288         /**\r
289          * A function for making your own, custom, animations. The key aspect of\r
290          * this function is the object of style properties that will be animated,\r
291          * and to what end. Each key within the object represents a style property\r
292          * that will also be animated (for example: "height", "top", or "opacity").\r
293          *\r
294          * The value associated with the key represents to what end the property\r
295          * will be animated. If a number is provided as the value, then the style\r
296          * property will be transitioned from its current state to that new number.\r
297          * Oterwise if the string "hide", "show", or "toggle" is provided, a default\r
298          * animation will be constructed for that property.\r
299          *\r
300          * @example $("p").animate({\r
301          *   height: 'toggle', opacity: 'toggle'\r
302          * }, "slow");\r
303          *\r
304          * @example $("p").animate({\r
305          *   left: 50, opacity: 'show'\r
306          * }, 500);\r
307          *\r
308          * @name animate\r
309          * @type jQuery\r
310          * @param Hash params A set of style attributes that you wish to animate, and to what end.\r
311          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).\r
312          * @param Function callback A function to be executed whenever the animation completes.\r
313          * @cat Effects/Animations\r
314          */\r
315         animate: function(prop,speed,callback) {\r
316                 return this.queue(function(){\r
317                 \r
318                         this.curAnim = prop;\r
319                         \r
320                         for ( var p in prop ) {\r
321                                 var e = new jQuery.fx( this, jQuery.speed(speed,callback), p );\r
322                                 if ( prop[p].constructor == Number )\r
323                                         e.custom( e.cur(), prop[p] );\r
324                                 else\r
325                                         e[ prop[p] ]( prop );\r
326                         }\r
327                         \r
328                 });\r
329         },\r
330         \r
331         /**\r
332          *\r
333          * @private\r
334          */\r
335         queue: function(type,fn){\r
336                 if ( !fn ) {\r
337                         fn = type;\r
338                         type = "fx";\r
339                 }\r
340         \r
341                 return this.each(function(){\r
342                         if ( !this.queue )\r
343                                 this.queue = {};\r
344         \r
345                         if ( !this.queue[type] )\r
346                                 this.queue[type] = [];\r
347         \r
348                         this.queue[type].push( fn );\r
349                 \r
350                         if ( this.queue[type].length == 1 )\r
351                                 fn.apply(this);\r
352                 });\r
353         }\r
354 \r
355 });\r
356 \r
357 jQuery.extend({\r
358 \r
359         setAuto: function(e,p) {\r
360                 if ( e.notAuto ) return;\r
361 \r
362                 if ( p == "height" && e.scrollHeight != parseInt(jQuery.curCSS(e,p)) ) return;\r
363                 if ( p == "width" && e.scrollWidth != parseInt(jQuery.curCSS(e,p)) ) return;\r
364 \r
365                 // Remember the original height\r
366                 var a = e.style[p];\r
367 \r
368                 // Figure out the size of the height right now\r
369                 var o = jQuery.curCSS(e,p,1);\r
370 \r
371                 if ( p == "height" && e.scrollHeight != o ||\r
372                         p == "width" && e.scrollWidth != o ) return;\r
373 \r
374                 // Set the height to auto\r
375                 e.style[p] = e.currentStyle ? "" : "auto";\r
376 \r
377                 // See what the size of "auto" is\r
378                 var n = jQuery.curCSS(e,p,1);\r
379 \r
380                 // Revert back to the original size\r
381                 if ( o != n && n != "auto" ) {\r
382                         e.style[p] = a;\r
383                         e.notAuto = true;\r
384                 }\r
385         },\r
386         \r
387         speed: function(s,o) {\r
388                 o = o || {};\r
389                 \r
390                 if ( o.constructor == Function )\r
391                         o = { complete: o };\r
392                 \r
393                 var ss = { slow: 600, fast: 200 };\r
394                 o.duration = (s && s.constructor == Number ? s : ss[s]) || 400;\r
395         \r
396                 // Queueing\r
397                 o.oldComplete = o.complete;\r
398                 o.complete = function(){\r
399                         jQuery.dequeue(this, "fx");\r
400                         if ( o.oldComplete && o.oldComplete.constructor == Function )\r
401                                 o.oldComplete.apply( this );\r
402                 };\r
403         \r
404                 return o;\r
405         },\r
406         \r
407         queue: {},\r
408         \r
409         dequeue: function(elem,type){\r
410                 type = type || "fx";\r
411         \r
412                 if ( elem.queue && elem.queue[type] ) {\r
413                         // Remove self\r
414                         elem.queue[type].shift();\r
415         \r
416                         // Get next function\r
417                         var f = elem.queue[type][0];\r
418                 \r
419                         if ( f ) f.apply( elem );\r
420                 }\r
421         },\r
422 \r
423         /*\r
424          * I originally wrote fx() as a clone of moo.fx and in the process\r
425          * of making it small in size the code became illegible to sane\r
426          * people. You've been warned.\r
427          */\r
428         \r
429         fx: function( elem, options, prop ){\r
430         \r
431                 var z = this;\r
432         \r
433                 // The users options\r
434                 z.o = {\r
435                         duration: options.duration || 400,\r
436                         complete: options.complete,\r
437                         step: options.step\r
438                 };\r
439         \r
440                 // The element\r
441                 z.el = elem;\r
442         \r
443                 // The styles\r
444                 var y = z.el.style;\r
445         \r
446                 // Simple function for setting a style value\r
447                 z.a = function(){\r
448                         if ( options.step )\r
449                                 options.step.apply( elem, [ z.now ] );\r
450 \r
451                         if ( prop == "opacity" ) {\r
452                                 if (jQuery.browser.mozilla && z.now == 1) z.now = 0.9999;\r
453                                 if (window.ActiveXObject)\r
454                                         y.filter = "alpha(opacity=" + z.now*100 + ")";\r
455                                 else\r
456                                         y.opacity = z.now;\r
457 \r
458                         // My hate for IE will never die\r
459                         } else if ( parseInt(z.now) )\r
460                                 y[prop] = parseInt(z.now) + "px";\r
461                                 \r
462                         y.display = "block";\r
463                 };\r
464         \r
465                 // Figure out the maximum number to run to\r
466                 z.max = function(){\r
467                         return parseFloat( jQuery.css(z.el,prop) );\r
468                 };\r
469         \r
470                 // Get the current size\r
471                 z.cur = function(){\r
472                         var r = parseFloat( jQuery.curCSS(z.el, prop) );\r
473                         return r && r > -10000 ? r : z.max();\r
474                 };\r
475         \r
476                 // Start an animation from one number to another\r
477                 z.custom = function(from,to){\r
478                         z.startTime = (new Date()).getTime();\r
479                         z.now = from;\r
480                         z.a();\r
481         \r
482                         z.timer = setInterval(function(){\r
483                                 z.step(from, to);\r
484                         }, 13);\r
485                 };\r
486         \r
487                 // Simple 'show' function\r
488                 z.show = function( p ){\r
489                         if ( !z.el.orig ) z.el.orig = {};\r
490 \r
491                         // Remember where we started, so that we can go back to it later\r
492                         z.el.orig[prop] = this.cur();\r
493 \r
494                         z.custom( 0, z.el.orig[prop] );\r
495 \r
496                         // Stupid IE, look what you made me do\r
497                         if ( prop != "opacity" )\r
498                                 y[prop] = "1px";\r
499                 };\r
500         \r
501                 // Simple 'hide' function\r
502                 z.hide = function(){\r
503                         if ( !z.el.orig ) z.el.orig = {};\r
504 \r
505                         // Remember where we started, so that we can go back to it later\r
506                         z.el.orig[prop] = this.cur();\r
507 \r
508                         z.o.hide = true;\r
509 \r
510                         // Begin the animation\r
511                         z.custom(z.el.orig[prop], 0);\r
512                 };\r
513         \r
514                 // IE has trouble with opacity if it does not have layout\r
515                 if ( jQuery.browser.msie && !z.el.currentStyle.hasLayout )\r
516                         y.zoom = "1";\r
517         \r
518                 // Remember  the overflow of the element\r
519                 if ( !z.el.oldOverlay )\r
520                         z.el.oldOverflow = jQuery.css( z.el, "overflow" );\r
521         \r
522                 // Make sure that nothing sneaks out\r
523                 y.overflow = "hidden";\r
524         \r
525                 // Each step of an animation\r
526                 z.step = function(firstNum, lastNum){\r
527                         var t = (new Date()).getTime();\r
528         \r
529                         if (t > z.o.duration + z.startTime) {\r
530                                 // Stop the timer\r
531                                 clearInterval(z.timer);\r
532                                 z.timer = null;\r
533 \r
534                                 z.now = lastNum;\r
535                                 z.a();\r
536 \r
537                                 z.el.curAnim[ prop ] = true;\r
538                                 \r
539                                 var done = true;\r
540                                 for ( var i in z.el.curAnim )\r
541                                         if ( z.el.curAnim[i] !== true )\r
542                                                 done = false;\r
543                                                 \r
544                                 if ( done ) {\r
545                                         // Reset the overflow\r
546                                         y.overflow = z.el.oldOverflow;\r
547                                 \r
548                                         // Hide the element if the "hide" operation was done\r
549                                         if ( z.o.hide ) \r
550                                                 y.display = 'none';\r
551                                         \r
552                                         // Reset the property, if the item has been hidden\r
553                                         if ( z.o.hide ) {\r
554                                                 for ( var p in z.el.curAnim ) {\r
555                                                         y[ p ] = z.el.orig[p] + ( p == "opacity" ? "" : "px" );\r
556         \r
557                                                         // set its height and/or width to auto\r
558                                                         if ( p == 'height' || p == 'width' )\r
559                                                                 jQuery.setAuto( z.el, p );\r
560                                                 }\r
561                                         }\r
562                                 }\r
563 \r
564                                 // If a callback was provided, execute it\r
565                                 if( done && z.o.complete && z.o.complete.constructor == Function )\r
566                                         // Execute the complete function\r
567                                         z.o.complete.apply( z.el );\r
568                         } else {\r
569                                 // Figure out where in the animation we are and set the number\r
570                                 var p = (t - this.startTime) / z.o.duration;\r
571                                 z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;\r
572         \r
573                                 // Perform the next step of the animation\r
574                                 z.a();\r
575                         }\r
576                 };\r
577         \r
578         }\r
579 \r
580 });\r