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