Removed easing formulas and placed them as a plugin under interface
[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/Animations\r
25          * @see hide(String|Number,Function)\r
26          */\r
27         show: function(speed,callback, easing){\r
28                 return speed ? this.animate({\r
29                         height: "show", width: "show", opacity: "show"\r
30                 }, speed, callback, easing) : 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/Animations\r
55          * @see show(String|Number,Function)\r
56          */\r
57         hide: function(speed,callback, easing){\r
58                 return speed ? this.animate({\r
59                         height: "hide", width: "hide", opacity: "hide"\r
60                 }, speed, callback, easing) : 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/Animations\r
82          * @see slideUp(String|Number,Function)\r
83          * @see slideToggle(String|Number,Function)\r
84          */\r
85         slideDown: function(speed,callback, easing){\r
86                 return this.animate({height: "show"}, speed, callback, easing);\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/Animations\r
108          * @see slideDown(String|Number,Function)\r
109          * @see slideToggle(String|Number,Function)\r
110          */\r
111         slideUp: function(speed,callback, easing){\r
112                 return this.animate({height: "hide"}, speed, callback, easing);\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/Animations\r
134          * @see slideDown(String|Number,Function)\r
135          * @see slideUp(String|Number,Function)\r
136          */\r
137         slideToggle: function(speed, callback, easing){\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, easing);\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/Animations\r
164          * @see fadeOut(String|Number,Function)\r
165          * @see fadeTo(String|Number,Number,Function)\r
166          */\r
167         fadeIn: function(speed, callback, easing){\r
168                 return this.animate({opacity: "show"}, speed, callback, easing);\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/Animations\r
191          * @see fadeIn(String|Number,Function)\r
192          * @see fadeTo(String|Number,Number,Function)\r
193          */\r
194         fadeOut: function(speed, callback, easing){\r
195                 return this.animate({opacity: "hide"}, speed, callback, easing);\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/Animations\r
219          * @see fadeIn(String|Number,Function)\r
220          * @see fadeOut(String|Number,Function)\r
221          */\r
222         fadeTo: function(speed,to,callback, easing){\r
223                 return this.animate({opacity: to}, speed, callback, easing);\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/Animations\r
253          */\r
254         animate: function(prop,speed,callback, easing) {\r
255                 return this.queue(function(){\r
256                 \r
257                         this.curAnim = jQuery.extend({}, prop);\r
258                         \r
259                         for ( var p in prop ) {\r
260                                 var e = new jQuery.fx( this, jQuery.speed(speed,callback), p, easing );\r
261                                 if ( prop[p].constructor == Number )\r
262                                         e.custom( e.cur(), prop[p] );\r
263                                 else\r
264                                         e[ prop[p] ]( prop );\r
265                         }\r
266                         \r
267                 });\r
268         },\r
269         \r
270         /**\r
271          *\r
272          * @private\r
273          */\r
274         queue: function(type,fn){\r
275                 if ( !fn ) {\r
276                         fn = type;\r
277                         type = "fx";\r
278                 }\r
279         \r
280                 return this.each(function(){\r
281                         if ( !this.queue )\r
282                                 this.queue = {};\r
283         \r
284                         if ( !this.queue[type] )\r
285                                 this.queue[type] = [];\r
286         \r
287                         this.queue[type].push( fn );\r
288                 \r
289                         if ( this.queue[type].length == 1 )\r
290                                 fn.apply(this);\r
291                 });\r
292         }\r
293 \r
294 });\r
295 \r
296 jQuery.extend({\r
297         \r
298         speed: function(s,o) {\r
299                 o = o || {};\r
300                 \r
301                 if ( o.constructor == Function )\r
302                         o = { complete: o };\r
303                 \r
304                 var ss = { slow: 600, fast: 200 };\r
305                 o.duration = (s && s.constructor == Number ? s : ss[s]) || 400;\r
306         \r
307                 // Queueing\r
308                 o.oldComplete = o.complete;\r
309                 o.complete = function(){\r
310                         jQuery.dequeue(this, "fx");\r
311                         if ( o.oldComplete && o.oldComplete.constructor == Function )\r
312                                 o.oldComplete.apply( this );\r
313                 };\r
314         \r
315                 return o;\r
316         },\r
317         \r
318         queue: {},\r
319         \r
320         dequeue: function(elem,type){\r
321                 type = type || "fx";\r
322         \r
323                 if ( elem.queue && elem.queue[type] ) {\r
324                         // Remove self\r
325                         elem.queue[type].shift();\r
326         \r
327                         // Get next function\r
328                         var f = elem.queue[type][0];\r
329                 \r
330                         if ( f ) f.apply( elem );\r
331                 }\r
332         },\r
333 \r
334         /*\r
335          * I originally wrote fx() as a clone of moo.fx and in the process\r
336          * of making it small in size the code became illegible to sane\r
337          * people. You've been warned.\r
338          */\r
339         \r
340         fx: function( elem, options, prop, easing ){\r
341 \r
342                 var z = this;\r
343 \r
344                 // The users options\r
345                 z.o = {\r
346                         duration: options.duration || 400,\r
347                         complete: options.complete,\r
348                         step: options.step,\r
349                         easing : easing || 'linear'\r
350                 };\r
351                 \r
352                 // The element\r
353                 z.el = elem;\r
354 \r
355                 // The styles\r
356                 var y = z.el.style;\r
357                 \r
358                 // Store display property\r
359                 var oldDisplay = jQuery.css(z.el, 'display');\r
360                 // Set display property to block for animation\r
361                 y.display = "block";\r
362                 // Make sure that nothing sneaks out\r
363                 y.overflow = "hidden";\r
364 \r
365                 // Simple function for setting a style value\r
366                 z.a = function(){\r
367                         if ( options.step )\r
368                                 options.step.apply( elem, [ z.now ] );\r
369 \r
370                         if ( prop == "opacity" )\r
371                                 jQuery.attr(y, "opacity", z.now); // Let attr handle opacity\r
372                         else if ( parseInt(z.now) ) // My hate for IE will never die\r
373                                 y[prop] = parseInt(z.now) + "px";\r
374                 };\r
375 \r
376                 // Figure out the maximum number to run to\r
377                 z.max = function(){\r
378                         return parseFloat( jQuery.css(z.el,prop) );\r
379                 };\r
380 \r
381                 // Get the current size\r
382                 z.cur = function(){\r
383                         var r = parseFloat( jQuery.curCSS(z.el, prop) );\r
384                         return r && r > -10000 ? r : z.max();\r
385                 };\r
386 \r
387                 // Start an animation from one number to another\r
388                 z.custom = function(from,to){\r
389                         z.startTime = (new Date()).getTime();\r
390                         z.now = from;\r
391                         z.a();\r
392 \r
393                         z.timer = setInterval(function(){\r
394                                 z.step(from, to);\r
395                         }, 13);\r
396                 };\r
397 \r
398                 // Simple 'show' function\r
399                 z.show = function(){\r
400                         if ( !z.el.orig ) z.el.orig = {};\r
401 \r
402                         // Remember where we started, so that we can go back to it later\r
403                         z.el.orig[prop] = this.cur();\r
404 \r
405                         z.o.show = true;\r
406 \r
407                         // Begin the animation\r
408                         z.custom(0, z.el.orig[prop]);\r
409 \r
410                         // Stupid IE, look what you made me do\r
411                         if ( prop != "opacity" )\r
412                                 y[prop] = "1px";\r
413                 };\r
414 \r
415                 // Simple 'hide' function\r
416                 z.hide = function(){\r
417                         if ( !z.el.orig ) z.el.orig = {};\r
418 \r
419                         // Remember where we started, so that we can go back to it later\r
420                         z.el.orig[prop] = this.cur();\r
421 \r
422                         z.o.hide = true;\r
423 \r
424                         // Begin the animation\r
425                         z.custom(z.el.orig[prop], 0);\r
426                 };\r
427                 \r
428                 //Simple 'toggle' function\r
429                 z.toggle = function() {\r
430                         if ( !z.el.orig ) z.el.orig = {};\r
431 \r
432                         // Remember where we started, so that we can go back to it later\r
433                         z.el.orig[prop] = this.cur();\r
434 \r
435                         if(oldDisplay == 'none')  {\r
436                                 z.o.show = true;\r
437                                 \r
438                                 // Stupid IE, look what you made me do\r
439                                 if ( prop != "opacity" )\r
440                                         y[prop] = "1px";\r
441 \r
442                                 // Begin the animation\r
443                                 z.custom(0, z.el.orig[prop]);   \r
444                         } else {\r
445                                 z.o.hide = true;\r
446 \r
447                                 // Begin the animation\r
448                                 z.custom(z.el.orig[prop], 0);\r
449                         }               \r
450                 };\r
451 \r
452                 // Each step of an animation\r
453                 z.step = function(firstNum, lastNum){\r
454                         var t = (new Date()).getTime();\r
455 \r
456                         if (t > z.o.duration + z.startTime) {\r
457                                 // Stop the timer\r
458                                 clearInterval(z.timer);\r
459                                 z.timer = null;\r
460 \r
461                                 z.now = lastNum;\r
462                                 z.a();\r
463 \r
464                                 if (z.el.curAnim) z.el.curAnim[ prop ] = true;\r
465 \r
466                                 var done = true;\r
467                                 for ( var i in z.el.curAnim )\r
468                                         if ( z.el.curAnim[i] !== true )\r
469                                                 done = false;\r
470 \r
471                                 if ( done ) {\r
472                                         // Reset the overflow\r
473                                         y.overflow = '';\r
474                                         \r
475                                         // Reset the display\r
476                                         y.display = oldDisplay;\r
477                                         if (jQuery.css(z.el, 'display') == 'none')\r
478                                                 y.display = 'block';\r
479 \r
480                                         // Hide the element if the "hide" operation was done\r
481                                         if ( z.o.hide ) \r
482                                                 y.display = 'none';\r
483 \r
484                                         // Reset the properties, if the item has been hidden or shown\r
485                                         if ( z.o.hide || z.o.show )\r
486                                                 for ( var p in z.el.curAnim )\r
487                                                         if (p == "opacity")\r
488                                                                 jQuery.attr(y, p, z.el.orig[p]);\r
489                                                         else\r
490                                                                 y[p] = '';\r
491                                 }\r
492 \r
493                                 // If a callback was provided, execute it\r
494                                 if( done && z.o.complete && z.o.complete.constructor == Function )\r
495                                         // Execute the complete function\r
496                                         z.o.complete.apply( z.el );\r
497                         } else {\r
498                                 var n = t - this.startTime;\r
499                                 // Figure out where in the animation we are and set the number\r
500                                 var p = n / z.o.duration;\r
501                                 //if the easing function exists then use it else use default linear easing\r
502                                 if (jQuery.easing && jQuery.easing[z.o.easing])\r
503                                         z.now = jQuery.easing[z.o.easing](p, n,  firstNum, (lastNum-firstNum), z.o.duration);\r
504                                 else \r
505                                         z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;\r
506 \r
507                                 // Perform the next step of the animation\r
508                                 z.a();\r
509                         }\r
510                 };\r
511         \r
512         }\r
513 });