IE is now forced layout if it doesn't have it (such that opacity now works as expecte...
[jquery.git] / fx / fx.js
1 $.speed = function(s,o) {
2         if ( o && o.constructor == Function ) { o = { onComplete: o }; }
3         o = o || {};
4         var ss = {"crawl":1200,"xslow":850,"slow":600,"medium":400,"fast":200,"xfast":75,"normal":400};
5         o.duration = typeof s == "number" ? s : ss[s] || 400;
6
7         o.oldComplete = o.onComplete;
8         o.onComplete = function(){
9                 $.dequeue(this, 'fx');
10                 if ( o.oldComplete && o.oldComplete.constructor == Function ) {
11                         $.apply( this, o.oldComplete );
12                 }
13         };
14
15         return o;
16 };
17
18 $.queue = {};
19
20 $.dequeue = function(elem,type){
21         type = type || 'fx';
22
23         if ( elem.$$queue && elem.$$queue[type] ) {
24                 // Remove self
25                 elem.$$queue[type].shift();
26
27                 // Get next function
28                 var f = elem.$$queue[type][0];
29         
30                 if ( f ) {
31                         $.apply( elem, f );
32                 }
33         }
34 };
35
36 $.fn.queue = function(type,fn){
37         if ( !fn ) {
38                 fn = type;
39                 type = 'fx';
40         }
41
42         return this.each(function(){
43                 if ( !this.$$queue ) {
44                         this.$$queue = {};
45                 }
46
47                 if ( !this.$$queue[type] ) {
48                         this.$$queue[type] = [];
49                 }
50
51                 this.$$queue[type].push( fn );
52         
53                 if ( this.$$queue[type].length == 1 ) {
54                         $.apply(this,fn);
55                 }
56         });
57 };
58
59 $.fn._hide = $.fn.hide;
60
61 $.fn.hide = function(a,o) {
62         o = $.speed(a,o);
63         return a ? this.queue(function(){
64                 new $.fx.FadeSize(this,o).hide();
65         }) : this._hide();
66 };
67
68 $.fn._show = $.fn.show;
69
70 $.fn.show = function(a,o) {
71         o = $.speed(a,o);
72         return a ? this.queue(function(){
73                 new $.fx.FadeSize(this,o).show();
74         }) : this._show();
75 };
76
77 $.fn.slideDown = function(a,o) {
78         o = $.speed(a,o);
79         return this.queue(function(){
80                 new $.fx.Resize(this,o).show("height");
81         });
82 };
83
84 $.fn.slideUp = function(a,o) {
85         o = $.speed(a,o);
86         return this.queue(function(){
87                 new $.fx.Resize(this,o).hide("height");
88         });
89 };
90
91 $.fn.fadeOut = function(a,o) {
92         o = $.speed(a,o);
93         return a ? this.queue(function(){
94                 new $.fx.Opacity(this,o,1).hide();
95         }) : this._hide();
96 };
97
98 $.fn.fadeIn = function(a,o) {
99         o = $.speed(a,o);
100         return a ? this.queue(function(){
101                 new $.fx.Opacity(this,o,1).show();
102         }) : this._show();
103 };
104
105 $.fn.fadeTo = function(a,ev,o) {
106         o = $.speed(a,o);
107         return a ? this.queue(function(){
108                 ef = new $.fx.Opacity(this,o);
109                 ef.custom(ef.cur(),parseFloat(ev));
110                 ef.show();
111         }) : this._show();
112 };
113
114 $.fn.center = function(f) {
115         return this.each(function(){
116                 if ( !f && this.nodeName == 'IMG' &&
117                                  !this.offsetWidth && !this.offsetHeight ) {
118                         var self = this;
119                         setTimeout(function(){
120                                 $(self).center(true);
121                         }, 13);
122                 } else {
123                         var s = this.style;
124                         var p = this.parentNode;
125                         if ( $.css(p,"position") == 'static' ) {
126                                 p.style.position = 'relative';
127                         }
128                         s.position = 'absolute';
129                         s.left = (($.css(p,"width") - $.css(this,"width"))/2) + "px";
130                         s.top = (($.css(p,"height") - $.css(this,"height"))/2) + "px";
131                 }
132   });
133 };
134
135 $.setAuto = function(e,p) {
136         var a = e.style[p];
137         var o = $.css(e,p);
138         e.style[p] = 'auto';
139         var n = $.css(e,p);
140         if ( o != n ) {
141                 e.style[p] = a;
142         }
143 };
144
145 /*
146  * I originally wrote fx() as a clone of moo.fx and in the process
147  * of making it small in size the code became illegible to sane
148  * people. You've been warned.
149  */
150
151 $.fx = function(el,op,ty){
152
153         var z = this;
154
155         // The users options
156         z.o = {
157                 duration: (op && op.duration) || 400,
158                 onComplete: (op && op.onComplete) || op
159         };
160
161         // The element
162         z.el = el;
163
164         // The styles
165         var y = z.el.style;
166
167         // Simple function for setting a style value
168         z.a = function(){
169                 z.el.style[ty] = z.now+'px';
170         };
171
172         // Figure out the maximum number to run to
173         z.max = function(){return z.el["$$orig"+ty]||z.cur();};
174
175         // Get the current size
176         z.cur = function(){return $.css(z.el,ty);};
177
178         // Start an animation from one number to another
179         z.custom = function(from,to){
180                 z.startTime = (new Date()).getTime();
181                 z.now = from;
182                 z.a();
183
184                 z.timer = setInterval(function(){
185                         z.step(from, to);
186                 }, 13);
187         };
188
189         // Simple 'show' function
190         z.show = function(){
191                 y.display = "block";
192                 z.o.auto = true;
193                 z.custom(0,z.max());
194         };
195
196         // Simple 'hide' function
197         z.hide = function(){
198                 // Remember where we started, so that we can go back to it later
199                 z.el["$$orig"+ty] = this.cur();
200
201                 // Begin the animation
202                 z.custom(z.cur(),0);
203         };
204
205         // Toggle between showing and hiding an element
206         z.toggle = function(){
207                 if ( z.cur() > 0 ) {
208                         z.hide();
209                 } else {
210                         z.show();
211                 }
212         };
213
214         // IE has trouble with opacity if it doesn't have layout
215         if ( $.browser == "msie" && !z.el.currentStyle.hasLayout ) {
216                 y.zoom = 1;
217         }
218
219         // Remember  the overflow of the element
220         z.oldOverflow = y.overflow;
221
222         // Make sure that nothing sneaks out
223         y.overflow = "hidden";
224
225         // Each step of an animation
226         z.step = function(firstNum, lastNum){
227                 var t = (new Date()).getTime();
228
229                 if (t > z.o.duration + z.startTime) {
230                         // Stop the timer
231                         clearInterval(z.timer);
232                         z.timer = null;
233
234                         z.now = lastNum;
235                         z.a();
236
237                         // Reset the overflow
238                         y.overflow = z.oldOverflow;
239
240                         // If the element was shown, and not using a custom number,
241                         // set its height and/or width to auto
242                         if ( (ty == "height" || ty == "width") && z.o.auto ) {
243                                 $.setAuto( z.el, ty );
244                         }
245
246                         // If a callback was provided, execute it
247                         if( z.o.onComplete.constructor == Function ) {
248
249                                 // Yes, this is a weird place for this, but it needs to be executed
250                                 // only once per cluster of effects.
251                                 // If the element is, effectively, hidden - hide it
252                                 if ( y.height == "0px" || y.width == "0px" ) {
253                                         y.display = "none";
254                                 }
255
256                                 $.apply( z.el, z.o.onComplete );
257                         }
258                 } else {
259                         // Figure out where in the animation we are and set the number
260                         var p = (t - this.startTime) / z.o.duration;
261                         z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
262
263                         // Perform the next step of the animation
264                         z.a();
265                 }
266         };
267
268 };
269
270 $.fx.fn = ["show","hide","toggle"];
271 $.fx.ty = ["Height","Width","Left","Top"];
272
273 (function(){
274         for(var $i in $.fx.ty){(function(){
275                 var c = $.fx.ty[$i];
276                 $.fx[c] = function(a,b){
277                         return new $.fx(a,b,c.toLowerCase());
278                 };
279         })();}
280 })();
281
282 $.fx.Opacity = function(a,b,sv){
283         var o = new $.fx(a,b,"opacity");
284         o.cur = function(){return parseFloat(o.el.style.opacity);};
285         o.a = function() {
286                 var e = o.el.style;
287                 if (o.now == 1) { o.now = 0.9999; }
288                 if (window.ActiveXObject) {
289                         e.filter = "alpha(opacity=" + o.now*100 + ")";
290                 }
291                 e.opacity = o.now;
292         };
293         o.io = o.now = (sv || o.cur());
294         o.a();
295         return o;
296 };
297
298 $.fx.Resize = function(e,o){
299         var z = this;
300         var h = new $.fx.Height(e,o);
301         if(o) { o.onComplete = null; }
302         var w = new $.fx.Width(e,o);
303         function c(a,b,d){return (!a||a==d||b==d);}
304         for(var i in $.fx.fn){(function(){
305                 var j = $.fx.fn[i];
306                 z[j] = function(a,b){
307                         if(c(a,b,"height")) { h[j](); }
308                         if(c(a,b,"width")) { w[j](); }
309                 };
310         })();}
311 };
312
313 $.fx.FadeSize = function(e,o){
314         var z = this;
315         var r = new $.fx.Resize(e,o);
316         if(o) { o.onComplete = null; }
317         var p = new $.fx.Opacity(e,o,1);
318         for(var i in $.fx.fn){(function(){
319                 var j = $.fx.fn[i];
320                 z[j] = function(a,b){p[j]();r[j](a,b);};
321         })();}
322 };