Fixed up the easing documentation.
[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\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\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 (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
77          * @param Function callback (optional) A function to be executed whenever the animation completes.\r
78          * @cat Effects\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 (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
102          * @param Function callback (optional) A function to be executed whenever the animation completes.\r
103          * @cat Effects\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 (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
127          * @param Function callback (optional) A function to be executed whenever the animation completes.\r
128          * @cat Effects\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 (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
156          * @param Function callback (optional) A function to be executed whenever the animation completes.\r
157          * @cat Effects\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 (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
182          * @param Function callback (optional) A function to be executed whenever the animation completes.\r
183          * @cat Effects\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\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);
237          *
238          * @example $("p").animate({\r
239          *   opacity: 'show'\r
240          * }, "slow", "easein");
241          * @desc An example of using an 'easing' function to provide a different style of animation. This will only work if you have a plugin that provides this easing function (Only 'linear' is provided by default, with jQuery).\r
242          *\r
243          * @name animate\r
244          * @type jQuery\r
245          * @param Hash params A set of style attributes that you wish to animate, and to what end.\r
246          * @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).
247          * @param String easing (optional) The name of the easing effect that you want to use (Plugin Required).\r
248          * @param Function callback (optional) A function to be executed whenever the animation completes.\r
249          * @cat Effects\r
250          */\r
251         animate: function( prop, speed, easing, callback ) {\r
252                 return this.queue(function(){\r
253                 \r
254                         this.curAnim = jQuery.extend({}, prop);
255                         var opt = jQuery.speed(speed, easing, callback);\r
256                         \r
257                         for ( var p in prop ) {\r
258                                 var e = new jQuery.fx( this, opt, p );\r
259                                 if ( prop[p].constructor == Number )\r
260                                         e.custom( e.cur(), prop[p] );\r
261                                 else\r
262                                         e[ prop[p] ]( prop );\r
263                         }\r
264                         \r
265                 });\r
266         },\r
267         \r
268         /**\r
269          *\r
270          * @private\r
271          */\r
272         queue: function(type,fn){\r
273                 if ( !fn ) {\r
274                         fn = type;\r
275                         type = "fx";\r
276                 }\r
277         \r
278                 return this.each(function(){\r
279                         if ( !this.queue )\r
280                                 this.queue = {};\r
281         \r
282                         if ( !this.queue[type] )\r
283                                 this.queue[type] = [];\r
284         \r
285                         this.queue[type].push( fn );\r
286                 \r
287                         if ( this.queue[type].length == 1 )\r
288                                 fn.apply(this);\r
289                 });\r
290         }\r
291 \r
292 });\r
293 \r
294 jQuery.extend({\r
295         \r
296         speed: function(speed, easing, fn) {\r
297                 var opt = speed.constructor == Object ? speed : {
298                         complete: fn || !fn && easing || 
299                                 speed.constructor == Function && speed,
300                         duration: speed,
301                         easing: fn && easing || easing && easing.constructor != Function && easing
302                 };
303 \r
304                 opt.duration = (opt.duration.constructor == Number ? 
305                         opt.duration : 
306                         { slow: 600, fast: 200 }[opt.duration]) || 400;\r
307         \r
308                 // Queueing\r
309                 opt.oldComplete = opt.complete;\r
310                 opt.complete = function(){\r
311                         jQuery.dequeue(this, "fx");\r
312                         if ( opt.oldComplete && opt.oldComplete.constructor == Function )\r
313                                 opt.oldComplete.apply( this );\r
314                 };\r
315         \r
316                 return opt;\r
317         },
318         
319         easing: {},\r
320         \r
321         queue: {},\r
322         \r
323         dequeue: function(elem,type){\r
324                 type = type || "fx";\r
325         \r
326                 if ( elem.queue && elem.queue[type] ) {\r
327                         // Remove self\r
328                         elem.queue[type].shift();\r
329         \r
330                         // Get next function\r
331                         var f = elem.queue[type][0];\r
332                 \r
333                         if ( f ) f.apply( elem );\r
334                 }\r
335         },\r
336 \r
337         /*\r
338          * I originally wrote fx() as a clone of moo.fx and in the process\r
339          * of making it small in size the code became illegible to sane\r
340          * people. You've been warned.\r
341          */\r
342         \r
343         fx: function( elem, options, prop ){\r
344 \r
345                 var z = this;\r
346 \r
347                 // The styles\r
348                 var y = elem.style;\r
349                 \r
350                 // Store display property\r
351                 var oldDisplay = jQuery.css(elem, 'display');\r
352                 // Set display property to block for animation\r
353                 y.display = "block";\r
354                 // Make sure that nothing sneaks out\r
355                 y.overflow = "hidden";\r
356 \r
357                 // Simple function for setting a style value\r
358                 z.a = function(){\r
359                         if ( options.step )\r
360                                 options.step.apply( elem, [ z.now ] );\r
361 \r
362                         if ( prop == "opacity" )\r
363                                 jQuery.attr(y, "opacity", z.now); // Let attr handle opacity\r
364                         else if ( parseInt(z.now) ) // My hate for IE will never die\r
365                                 y[prop] = parseInt(z.now) + "px";\r
366                 };\r
367 \r
368                 // Figure out the maximum number to run to\r
369                 z.max = function(){\r
370                         return parseFloat( jQuery.css(elem,prop) );\r
371                 };\r
372 \r
373                 // Get the current size\r
374                 z.cur = function(){\r
375                         var r = parseFloat( jQuery.curCSS(elem, prop) );\r
376                         return r && r > -10000 ? r : z.max();\r
377                 };\r
378 \r
379                 // Start an animation from one number to another\r
380                 z.custom = function(from,to){\r
381                         z.startTime = (new Date()).getTime();\r
382                         z.now = from;\r
383                         z.a();\r
384 \r
385                         z.timer = setInterval(function(){\r
386                                 z.step(from, to);\r
387                         }, 13);\r
388                 };\r
389 \r
390                 // Simple 'show' function\r
391                 z.show = function(){\r
392                         if ( !elem.orig ) elem.orig = {};\r
393 \r
394                         // Remember where we started, so that we can go back to it later\r
395                         elem.orig[prop] = this.cur();\r
396 \r
397                         options.show = true;\r
398 \r
399                         // Begin the animation\r
400                         z.custom(0, elem.orig[prop]);\r
401 \r
402                         // Stupid IE, look what you made me do\r
403                         if ( prop != "opacity" )\r
404                                 y[prop] = "1px";\r
405                 };\r
406 \r
407                 // Simple 'hide' function\r
408                 z.hide = function(){\r
409                         if ( !elem.orig ) elem.orig = {};\r
410 \r
411                         // Remember where we started, so that we can go back to it later\r
412                         elem.orig[prop] = this.cur();\r
413 \r
414                         options.hide = true;\r
415 \r
416                         // Begin the animation\r
417                         z.custom(elem.orig[prop], 0);\r
418                 };\r
419                 \r
420                 //Simple 'toggle' function\r
421                 z.toggle = function() {\r
422                         if ( !elem.orig ) elem.orig = {};\r
423 \r
424                         // Remember where we started, so that we can go back to it later\r
425                         elem.orig[prop] = this.cur();\r
426 \r
427                         if(oldDisplay == 'none')  {\r
428                                 options.show = true;\r
429                                 \r
430                                 // Stupid IE, look what you made me do\r
431                                 if ( prop != "opacity" )\r
432                                         y[prop] = "1px";\r
433 \r
434                                 // Begin the animation\r
435                                 z.custom(0, elem.orig[prop]);   \r
436                         } else {\r
437                                 options.hide = true;\r
438 \r
439                                 // Begin the animation\r
440                                 z.custom(elem.orig[prop], 0);\r
441                         }               \r
442                 };\r
443 \r
444                 // Each step of an animation\r
445                 z.step = function(firstNum, lastNum){\r
446                         var t = (new Date()).getTime();\r
447 \r
448                         if (t > options.duration + z.startTime) {\r
449                                 // Stop the timer\r
450                                 clearInterval(z.timer);\r
451                                 z.timer = null;\r
452 \r
453                                 z.now = lastNum;\r
454                                 z.a();\r
455 \r
456                                 if (elem.curAnim) elem.curAnim[ prop ] = true;\r
457 \r
458                                 var done = true;\r
459                                 for ( var i in elem.curAnim )\r
460                                         if ( elem.curAnim[i] !== true )\r
461                                                 done = false;\r
462 \r
463                                 if ( done ) {\r
464                                         // Reset the overflow\r
465                                         y.overflow = '';\r
466                                         \r
467                                         // Reset the display\r
468                                         y.display = oldDisplay;\r
469                                         if (jQuery.css(elem, 'display') == 'none')\r
470                                                 y.display = 'block';\r
471 \r
472                                         // Hide the element if the "hide" operation was done\r
473                                         if ( options.hide ) \r
474                                                 y.display = 'none';\r
475 \r
476                                         // Reset the properties, if the item has been hidden or shown\r
477                                         if ( options.hide || options.show )\r
478                                                 for ( var p in elem.curAnim )\r
479                                                         if (p == "opacity")\r
480                                                                 jQuery.attr(y, p, elem.orig[p]);\r
481                                                         else\r
482                                                                 y[p] = '';\r
483                                 }\r
484 \r
485                                 // If a callback was provided, execute it\r
486                                 if ( done && options.complete && options.complete.constructor == Function )\r
487                                         // Execute the complete function\r
488                                         options.complete.apply( elem );\r
489                         } else {\r
490                                 var n = t - this.startTime;\r
491                                 // Figure out where in the animation we are and set the number\r
492                                 var p = n / options.duration;
493                                 \r
494                                 // If the easing function exists, then use it \r
495                                 z.now = options.easing && jQuery.easing[options.easing] ?
496                                         jQuery.easing[options.easing](p, n,  firstNum, (lastNum-firstNum), options.duration) :
497                                         // else use default linear easing
498                                         ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;\r
499 \r
500                                 // Perform the next step of the animation\r
501                                 z.a();\r
502                         }\r
503                 };\r
504         \r
505         }\r
506 });