1 /* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
2 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
3 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
5 * $LastChangedDate: 2007-09-10 19:38:31 -0700 (Mon, 10 Sep 2007) $
10 * Requires: jQuery 1.2+
19 // Create innerHeight, innerWidth, outerHeight and outerWidth methods
20 $.each( [ 'Height', 'Width' ], function(i
, name
){
22 // innerHeight and innerWidth
23 $.fn
[ 'inner' + name
] = function() {
26 var torl
= name
== 'Height' ? 'Top' : 'Left', // top or left
27 borr
= name
== 'Height' ? 'Bottom' : 'Right'; // bottom or right
29 return this[ name
.toLowerCase() ]() + num(this, 'padding' + torl
) + num(this, 'padding' + borr
);
32 // outerHeight and outerWidth
33 $.fn
[ 'outer' + name
] = function(options
) {
36 var torl
= name
== 'Height' ? 'Top' : 'Left', // top or left
37 borr
= name
== 'Height' ? 'Bottom' : 'Right'; // bottom or right
39 options
= $.extend({ margin
: false }, options
|| {});
41 return this[ name
.toLowerCase() ]()
42 + num(this, 'border' + torl
+ 'Width') + num(this, 'border' + borr
+ 'Width')
43 + num(this, 'padding' + torl
) + num(this, 'padding' + borr
)
44 + (options
.margin
? (num(this, 'margin' + torl
) + num(this, 'margin' + borr
)) : 0);
48 // Create scrollLeft and scrollTop methods
49 $.each( ['Left', 'Top'], function(i
, name
) {
50 $.fn
[ 'scroll' + name
] = function(val
) {
53 return val
!= undefined ?
55 // Set the scroll offset
56 this.each(function() {
57 this == window
|| this == document
?
59 name
== 'Left' ? val
: $(window
)[ 'scrollLeft' ](),
60 name
== 'Top' ? val
: $(window
)[ 'scrollTop' ]()
62 this[ 'scroll' + name
] = val
;
65 // Return the scroll offset
66 this[0] == window
|| this[0] == document
?
67 self
[ (name
== 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
68 $.boxModel
&& document
.documentElement
[ 'scroll' + name
] ||
69 document
.body
[ 'scroll' + name
] :
70 this[0][ 'scroll' + name
];
75 position: function() {
76 var left
= 0, top
= 0, elem
= this[0], offset
, parentOffset
, offsetParent
, results
;
79 // Get *real* offsetParent
80 offsetParent
= this.offsetParent();
82 // Get correct offsets
83 offset
= this.offset();
84 parentOffset
= offsetParent
.offset();
86 // Subtract element margins
87 offset
.top
-= num(elem
, 'marginTop');
88 offset
.left
-= num(elem
, 'marginLeft');
90 // Add offsetParent borders
91 parentOffset
.top
+= num(offsetParent
, 'borderTopWidth');
92 parentOffset
.left
+= num(offsetParent
, 'borderLeftWidth');
94 // Subtract the two offsets
96 top
: offset
.top
- parentOffset
.top
,
97 left
: offset
.left
- parentOffset
.left
104 offsetParent: function() {
105 var offsetParent
= this[0].offsetParent
;
106 while ( offsetParent
&& (!/^body|html$/i.test(offsetParent
.tagName
) && $.css(offsetParent
, 'position') == 'static') )
107 offsetParent
= offsetParent
.offsetParent
;
108 return $(offsetParent
);
112 var num = function(el
, prop
) {
113 return parseInt($.css(el
.jquery
?el
[0]:el
,prop
))||0;