1 | $.fn.center = function(f) { |
---|
2 | return this.each(function(){ |
---|
3 | if ( !f && this.nodeName == 'IMG' && |
---|
4 | !this.offsetWidth && !this.offsetHeight ) { |
---|
5 | var self = this; |
---|
6 | setTimeout(function(){ |
---|
7 | $(self).center(true); |
---|
8 | }, 13); |
---|
9 | } else { |
---|
10 | var s = this.style; |
---|
11 | var p = this.parentNode; |
---|
12 | var b = document.body; |
---|
13 | var t = 0; |
---|
14 | |
---|
15 | if ( f != "vertical" ) |
---|
16 | { |
---|
17 | if ( $.css(p,"position") == 'static' ) |
---|
18 | p.style.position = 'relative'; |
---|
19 | s.position = 'absolute'; |
---|
20 | if ( ! isNaN(parseInt($.css(p,"width"))) ) |
---|
21 | { |
---|
22 | t = parseInt(($.css(p,"width") - $.css(this,"width"))/2); |
---|
23 | if ( t > 0 ) |
---|
24 | s.left = t + "px"; |
---|
25 | else |
---|
26 | s.left = "0px"; |
---|
27 | } |
---|
28 | else if ( ! isNaN( b.clientWidth ) ) |
---|
29 | { |
---|
30 | t = parseInt((b.clientWidth - $.css(this,"width"))/2); |
---|
31 | if ( t > 0 ) |
---|
32 | s.left = t + "px"; |
---|
33 | else |
---|
34 | s.left = "0px"; |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | if ( f != "horizontal" ) |
---|
39 | { |
---|
40 | if ( ! isNaN(parseInt($.css(p,"height"))) ) |
---|
41 | { |
---|
42 | t = parseInt(($.css(p,"height") - $.css(this,"height"))/2); |
---|
43 | if ( t > 0 ) |
---|
44 | s.top = t + "px"; |
---|
45 | else |
---|
46 | s.top = "0px"; |
---|
47 | } |
---|
48 | else if ( ! isNaN( b.clientHeight ) ) |
---|
49 | { |
---|
50 | t = parseInt((b.clientHeight - $.css(this,"height"))/2); |
---|
51 | if ( t > 0 ) |
---|
52 | s.top = t + "px"; |
---|
53 | else |
---|
54 | s.top = "0px"; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | }); |
---|
59 | }; |
---|