787f3da1e2b935686cc28acdfcc06d64d4f25907
[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         return o;
7 };
8
9 $.fn.hide = function(a,o) {
10         o = $.speed(a,o);
11         return a ? this.each(function(){
12                 new $.fx.FadeSize(this,o).hide();
13         }) : this._hide();
14 };
15
16 $.fn.show = function(a,o) {
17         o = $.speed(a,o);
18         return a ? this.each(function(){
19                 new $.fx.FadeSize(this,o).show();
20         }) : this._show();
21 };
22
23 $.fn.slideDown = function(a,o) {
24         o = $.speed(a,o);
25         return this.each(function(){
26                 new $.fx.Resize(this,o).show("height");
27         });
28 };
29
30 $.fn.slideUp = function(a,o) {
31         o = $.speed(a,o);
32         return this.each(function(){
33                 new $.fx.Resize(this,o).hide("height");
34         });
35 };
36
37 $.fn.fadeOut = function(a,o) {
38         o = $.speed(a,o);
39         return a ? this.each(function(){
40                 new $.fx.Opacity(this,o,1).hide();
41         }) : this._hide();
42 };
43
44 $.fn.fadeIn = function(a,o) {
45         o = $.speed(a,o);
46         return a ? this.each(function(){
47                 new $.fx.Opacity(this,o,1).show();
48         }) : this._show();
49 };
50
51 $.fn.fadeTo = function(a,ev,o) {
52         o = $.speed(a,o);
53         return a ? this.each(function(){
54                 ef = new $.fx.Opacity(this,o);
55                 ef.custom(ef.cur(),parseFloat(ev));
56                 ef.show();
57         }) : this._show();
58 };
59
60 $.fn.center = function(f) {
61         return this.each(function(){
62                 if ( !f && this.nodeName == 'IMG' &&
63                                  !this.offsetWidth && !this.offsetHeight ) {
64                         var self = this;
65                         setTimeout(function(){
66                                 $(self).center(true);
67                         }, 13);
68                 } else {
69                         var s = this.style;
70                         var p = this.parentNode;
71                         if ( $.css(p,"position") == 'static' ) {
72                                 p.style.position = 'relative';
73                         }
74                         s.position = 'absolute';
75                         s.left = (($.css(p,"width") - $.css(this,"width"))/2) + "px";
76                         s.top = (($.css(p,"height") - $.css(this,"height"))/2) + "px";
77                 }
78   });
79 };
80
81 $.setAuto = function(e,p) {
82         var a = e.style[p];
83         var o = $.css(e,p);
84         e.style[p] = 'auto';
85         var n = $.css(e,p);
86         if ( o != n ) {
87                 e.style[p] = a;
88         }
89 };
90
91 /*
92  * I originally wrote fx() as a clone of moo.fx and in the process
93  * of making it small in size the code became illegible to sane
94  * people. You've been warned.
95  */
96
97 $.fx = function(el,op,ty){
98
99         var z = this;
100
101         // The users options
102         z.o = {
103                 duration: (op && op.duration) || 400,
104                 onComplete: (op && op.onComplete) || op
105         };
106
107         // The element
108         z.el = el;
109
110         // The styles
111         var y = z.el.style;
112
113         // Simple function for setting a style value
114         z.a = function(){
115                 z.el.style[ty] = z.now+'px';
116         };
117
118         // Figure out the maximum number to run to
119         z.max = function(){return z.el["$$orig"+ty]||z.cur();};
120
121         // Get the current size
122         z.cur = function(){return $.css(z.el,ty);};
123
124         // Start an animation from one number to another
125         z.custom = function(from,to){
126                 z.startTime = (new Date()).getTime();
127                 z.now = from;
128                 z.a();
129
130                 z.timer = setInterval(function(){
131                         z.step(from, to);
132                 }, 13);
133         };
134
135         // Simple 'show' function
136         z.show = function(){
137                 y.display = "block";
138                 z.o.auto = true;
139                 z.custom(0,z.max());
140         };
141
142         // Simple 'hide' function
143         z.hide = function(){
144                 // Remember where we started, so that we can go back to it later
145                 z.el["$$orig"+ty] = this.cur();
146
147                 // Begin the animation
148                 z.custom(z.cur(),0);
149         };
150
151         // Toggle between showing and hiding an element
152         z.toggle = function(){
153                 if ( z.cur() > 0 ) {
154                         z.hide();
155                 } else {
156                         z.show();
157                 }
158         };
159
160         // Remember  the overflow of the element
161         z.oldOverflow = y.overflow;
162
163         // Make sure that nothing sneaks out
164         y.overflow = "hidden";
165
166         // Each step of an animation
167         z.step = function(firstNum, lastNum){
168                 var t = (new Date()).getTime();
169
170                 if (t > z.o.duration + z.startTime) {
171                         // Stop the timer
172                         clearInterval(z.timer);
173                         z.timer = null;
174
175                         // Reset the overflow
176                         y.overflow = z.oldOverflow;
177
178                         // If the element is, effectively, hidden - hide it
179                         if( y.height == "0px" || y.width == "0px" ) {
180                                 y.display = "none";
181                         }
182
183                         // If the element was shown, and not using a custom number,
184                         // set its height and width to auto
185                         if ( ty != "opacity" && z.o.auto ) {
186                                 $.setAuto( z.el, 'height' );
187                                 $.setAuto( z.el, 'width' );
188                         }
189
190                         // If a callback was provided, execute it
191                         if( z.o.onComplete.constructor == Function ) {
192                                 $.apply( z.el, z.onComplete );
193                         }
194                 } else {
195                         // Figure out where in the animation we are and set the number
196                         var p = (t - this.startTime) / z.o.duration;
197                         z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
198
199                         // Perform the next step of the animation
200                         z.a();
201                 }
202         };
203
204 };
205
206 $.fx.fn = ["show","hide","toggle"];
207 $.fx.ty = ["Height","Width","Left","Top"];
208
209 (function(){
210         for(var $i in $.fx.ty){(function(){
211                 var c = $.fx.ty[$i];
212                 $.fx[c] = function(a,b){
213                         return new $.fx(a,b,c.toLowerCase());
214                 };
215         })();}
216 })();
217
218 $.fx.Opacity = function(a,b,sv){
219         var o = new $.fx(a,b,"opacity");
220         o.cur = function(){return parseFloat(o.el.style.opacity);};
221         o.a = function() {
222                 var e = o.el.style;
223                 if (o.now == 1) { o.now = 0.9999; }
224                 if (window.ActiveXObject) {
225                         e.filter = "alpha(opacity=" + o.now*100 + ")";
226                 }
227                 e.opacity = o.now;
228         };
229         o.io = o.now = (sv || o.cur());
230         o.a();
231         return o;
232 };
233
234 $.fx.Resize = function(e,o){
235         var z = this;
236         var h = new $.fx.Height(e,o);
237         if(o) { o.onComplete = null; }
238         var w = new $.fx.Width(e,o);
239         function c(a,b,d){return (!a||a==d||b==d);}
240         for(var i in $.fx.fn){(function(){
241                 var j = $.fx.fn[i];
242                 z[j] = function(a,b){
243                         if(c(a,b,"height")) { h[j](); }
244                         if(c(a,b,"width")) { w[j](); }
245                 };
246         })();}
247 };
248
249 $.fx.FadeSize = function(e,o){
250         var z = this;
251         var r = new $.fx.Resize(e,o);
252         if(o) { o.onComplete = null; }
253         var p = new $.fx.Opacity(e,o,1);
254         for(var i in $.fx.fn){(function(){
255                 var j = $.fx.fn[i];
256                 z[j] = function(a,b){p[j]();r[j](a,b);};
257         })();}
258 };