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