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