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