Fixed issue with opacity being set to only 0.9999 for Opera, Safari, et. al - which...
[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         slideToggle: function(speed,callback){
146                 return this.each(function(){
147                         var state = $(this).is(":hidden") ? "show" : "hide";
148                         $(this).animate({height: state}, speed, callback);
149                 });
150         },
151         
152         /**
153          * Fade in all matched elements by adjusting their opacity.
154          * Only the opacity is adjusted for this animation, meaning that
155          * all of the matched elements should already have some form of height
156          * and width associated with them.
157          *
158          * @example $("p").fadeIn("slow");
159          *
160          * @name fadeIn
161          * @type jQuery
162          * @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).
163          * @cat Effects/Animations
164          */
165          
166         /**
167          * Fade in all matched elements by adjusting their opacity and firing a 
168          * callback function after completion.
169          * Only the opacity is adjusted for this animation, meaning that
170          * all of the matched elements should already have some form of height
171          * and width associated with them.
172          *
173          * @example $("p").fadeIn("slow",function(){
174          *   alert("Animation Done.");
175          * });
176          *
177          * @name fadeIn
178          * @type jQuery
179          * @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).
180          * @param Function callback A function to be executed whenever the animation completes.
181          * @cat Effects/Animations
182          */
183         fadeIn: function(speed,callback){
184                 return this.animate({opacity: "show"}, speed, callback);
185         },
186         
187         /**
188          * Fade out all matched elements by adjusting their opacity.
189          * Only the opacity is adjusted for this animation, meaning that
190          * all of the matched elements should already have some form of height
191          * and width associated with them.
192          *
193          * @example $("p").fadeOut("slow");
194          *
195          * @name fadeOut
196          * @type jQuery
197          * @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).
198          * @cat Effects/Animations
199          */
200          
201         /**
202          * Fade out all matched elements by adjusting their opacity and firing a 
203          * callback function after completion.
204          * Only the opacity is adjusted for this animation, meaning that
205          * all of the matched elements should already have some form of height
206          * and width associated with them.
207          *
208          * @example $("p").fadeOut("slow",function(){
209          *   alert("Animation Done.");
210          * });
211          *
212          * @name fadeOut
213          * @type jQuery
214          * @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).
215          * @param Function callback A function to be executed whenever the animation completes.
216          * @cat Effects/Animations
217          */
218         fadeOut: function(speed,callback){
219                 return this.animate({opacity: "hide"}, speed, callback);
220         },
221         
222         /**
223          * Fade the opacity of all matched elements to a specified opacity.
224          * Only the opacity is adjusted for this animation, meaning that
225          * all of the matched elements should already have some form of height
226          * and width associated with them.
227          *
228          * @example $("p").fadeTo("slow", 0.5);
229          *
230          * @name fadeTo
231          * @type jQuery
232          * @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).
233          * @param Number opacity The opacity to fade to (a number from 0 to 1).
234          * @cat Effects/Animations
235          */
236          
237         /**
238          * Fade the opacity of all matched elements to a specified opacity and 
239          * firing a callback function after completion.
240          * Only the opacity is adjusted for this animation, meaning that
241          * all of the matched elements should already have some form of height
242          * and width associated with them.
243          *
244          * @example $("p").fadeTo("slow", 0.5, function(){
245          *   alert("Animation Done.");
246          * });
247          *
248          * @name fadeTo
249          * @type jQuery
250          * @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).
251          * @param Number opacity The opacity to fade to (a number from 0 to 1).
252          * @param Function callback A function to be executed whenever the animation completes.
253          * @cat Effects/Animations
254          */
255         fadeTo: function(speed,to,callback){
256                 return this.animate({opacity: to}, speed, callback);
257         },
258         
259         /**
260          * @private
261          */
262         animate: function(prop,speed,callback) {
263                 return this.queue(function(){
264                 
265                         this.curAnim = prop;
266                         
267                         for ( var p in prop ) {
268                                 var e = new jQuery.fx( this, jQuery.speed(speed,callback), p );
269                                 if ( prop[p].constructor == Number )
270                                         e.custom( e.cur(), prop[p] );
271                                 else
272                                         e[ prop[p] ]( prop );
273                         }
274                         
275                 });
276         },
277         
278         /**
279          *
280          * @private
281          */
282         queue: function(type,fn){
283                 if ( !fn ) {
284                         fn = type;
285                         type = "fx";
286                 }
287         
288                 return this.each(function(){
289                         if ( !this.queue )
290                                 this.queue = {};
291         
292                         if ( !this.queue[type] )
293                                 this.queue[type] = [];
294         
295                         this.queue[type].push( fn );
296                 
297                         if ( this.queue[type].length == 1 )
298                                 fn.apply(this);
299                 });
300         }
301
302 });
303
304 jQuery.extend({
305
306         setAuto: function(e,p) {
307                 if ( e.notAuto ) return;
308
309                 if ( p == "height" && e.scrollHeight != parseInt(jQuery.curCSS(e,p)) ) return;
310                 if ( p == "width" && e.scrollWidth != parseInt(jQuery.curCSS(e,p)) ) return;
311
312                 // Remember the original height
313                 var a = e.style[p];
314
315                 // Figure out the size of the height right now
316                 var o = jQuery.curCSS(e,p,1);
317
318                 if ( p == "height" && e.scrollHeight != o ||
319                         p == "width" && e.scrollWidth != o ) return;
320
321                 // Set the height to auto
322                 e.style[p] = e.currentStyle ? "" : "auto";
323
324                 // See what the size of "auto" is
325                 var n = jQuery.curCSS(e,p,1);
326
327                 // Revert back to the original size
328                 if ( o != n && n != "auto" ) {
329                         e.style[p] = a;
330                         e.notAuto = true;
331                 }
332         },
333         
334         speed: function(s,o) {
335                 o = o || {};
336                 
337                 if ( o.constructor == Function )
338                         o = { complete: o };
339                 
340                 var ss = { slow: 600, fast: 200 };
341                 o.duration = (s && s.constructor == Number ? s : ss[s]) || 400;
342         
343                 // Queueing
344                 o.oldComplete = o.complete;
345                 o.complete = function(){
346                         jQuery.dequeue(this, "fx");
347                         if ( o.oldComplete && o.oldComplete.constructor == Function )
348                                 o.oldComplete.apply( this );
349                 };
350         
351                 return o;
352         },
353         
354         queue: {},
355         
356         dequeue: function(elem,type){
357                 type = type || "fx";
358         
359                 if ( elem.queue && elem.queue[type] ) {
360                         // Remove self
361                         elem.queue[type].shift();
362         
363                         // Get next function
364                         var f = elem.queue[type][0];
365                 
366                         if ( f ) f.apply( elem );
367                 }
368         },
369
370         /*
371          * I originally wrote fx() as a clone of moo.fx and in the process
372          * of making it small in size the code became illegible to sane
373          * people. You've been warned.
374          */
375         
376         fx: function( elem, options, prop ){
377         
378                 var z = this;
379         
380                 // The users options
381                 z.o = {
382                         duration: options.duration || 400,
383                         complete: options.complete,
384                         step: options.step
385                 };
386         
387                 // The element
388                 z.el = elem;
389         
390                 // The styles
391                 var y = z.el.style;
392         
393                 // Simple function for setting a style value
394                 z.a = function(){
395                         if ( options.step )
396                                 options.step.apply( elem, [ z.now ] );
397
398                         if ( prop == "opacity" ) {
399                                 if (jQuery.browser.mozilla && z.now == 1) z.now = 0.9999;
400                                 if (window.ActiveXObject)
401                                         y.filter = "alpha(opacity=" + z.now*100 + ")";
402                                 else
403                                         y.opacity = z.now;
404
405                         // My hate for IE will never die
406                         } else if ( parseInt(z.now) )
407                                 y[prop] = parseInt(z.now) + "px";
408                                 
409                         y.display = "block";
410                 };
411         
412                 // Figure out the maximum number to run to
413                 z.max = function(){
414                         return parseFloat( jQuery.css(z.el,prop) );
415                 };
416         
417                 // Get the current size
418                 z.cur = function(){
419                         var r = parseFloat( jQuery.curCSS(z.el, prop) );
420                         return r && r > -10000 ? r : z.max();
421                 };
422         
423                 // Start an animation from one number to another
424                 z.custom = function(from,to){
425                         z.startTime = (new Date()).getTime();
426                         z.now = from;
427                         z.a();
428         
429                         z.timer = setInterval(function(){
430                                 z.step(from, to);
431                         }, 13);
432                 };
433         
434                 // Simple 'show' function
435                 z.show = function( p ){
436                         if ( !z.el.orig ) z.el.orig = {};
437
438                         // Remember where we started, so that we can go back to it later
439                         z.el.orig[prop] = this.cur();
440
441                         z.custom( 0, z.el.orig[prop] );
442
443                         // Stupid IE, look what you made me do
444                         if ( prop != "opacity" )
445                                 y[prop] = "1px";
446                 };
447         
448                 // Simple 'hide' function
449                 z.hide = function(){
450                         if ( !z.el.orig ) z.el.orig = {};
451
452                         // Remember where we started, so that we can go back to it later
453                         z.el.orig[prop] = this.cur();
454
455                         z.o.hide = true;
456
457                         // Begin the animation
458                         z.custom(z.el.orig[prop], 0);
459                 };
460         
461                 // IE has trouble with opacity if it does not have layout
462                 if ( jQuery.browser.msie && !z.el.currentStyle.hasLayout )
463                         y.zoom = "1";
464         
465                 // Remember  the overflow of the element
466                 if ( !z.el.oldOverlay )
467                         z.el.oldOverflow = jQuery.css( z.el, "overflow" );
468         
469                 // Make sure that nothing sneaks out
470                 y.overflow = "hidden";
471         
472                 // Each step of an animation
473                 z.step = function(firstNum, lastNum){
474                         var t = (new Date()).getTime();
475         
476                         if (t > z.o.duration + z.startTime) {
477                                 // Stop the timer
478                                 clearInterval(z.timer);
479                                 z.timer = null;
480
481                                 z.now = lastNum;
482                                 z.a();
483
484                                 z.el.curAnim[ prop ] = true;
485                                 
486                                 var done = true;
487                                 for ( var i in z.el.curAnim )
488                                         if ( z.el.curAnim[i] !== true )
489                                                 done = false;
490                                                 
491                                 if ( done ) {
492                                         // Reset the overflow
493                                         y.overflow = z.el.oldOverflow;
494                                 
495                                         // Hide the element if the "hide" operation was done
496                                         if ( z.o.hide ) 
497                                                 y.display = 'none';
498                                         
499                                         // Reset the property, if the item has been hidden
500                                         if ( z.o.hide ) {
501                                                 for ( var p in z.el.curAnim ) {
502                                                         y[ p ] = z.el.orig[p] + ( p == "opacity" ? "" : "px" );
503         
504                                                         // set its height and/or width to auto
505                                                         if ( p == 'height' || p == 'width' )
506                                                                 jQuery.setAuto( z.el, p );
507                                                 }
508                                         }
509                                 }
510
511                                 // If a callback was provided, execute it
512                                 if( done && z.o.complete && z.o.complete.constructor == Function )
513                                         // Execute the complete function
514                                         z.o.complete.apply( z.el );
515                         } else {
516                                 // Figure out where in the animation we are and set the number
517                                 var p = (t - this.startTime) / z.o.duration;
518                                 z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
519         
520                                 // Perform the next step of the animation
521                                 z.a();
522                         }
523                 };
524         
525         }
526
527 });