2 /* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
3 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
4 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
11 * Requires: jQuery 1.2+
20 // Create innerHeight, innerWidth, outerHeight and outerWidth methods
21 $.each( [ 'Height', 'Width' ], function(i, name){
23 // innerHeight and innerWidth
24 $.fn[ 'inner' + name ] = function() {
27 var torl = name == 'Height' ? 'Top' : 'Left', // top or left
28 borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
30 return this.css('display') != 'none' ? this[0]['client' + name] : num( this, name.toLowerCase() ) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
33 // outerHeight and outerWidth
34 $.fn[ 'outer' + name ] = function(options) {
37 var torl = name == 'Height' ? 'Top' : 'Left', // top or left
38 borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
40 options = $.extend({ margin: false }, options || {});
42 var val = this.css('display') != 'none' ?
43 this[0]['offset' + name] :
44 num( this, name.toLowerCase() )
45 + num(this, 'border' + torl + 'Width') + num(this, 'border' + borr + 'Width')
46 + num(this, 'padding' + torl) + num(this, 'padding' + borr);
48 return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
52 // Create scrollLeft and scrollTop methods
53 $.each( ['Left', 'Top'], function(i, name) {
54 $.fn[ 'scroll' + name ] = function(val) {
57 return val != undefined ?
59 // Set the scroll offset
60 this.each(function() {
61 this == window || this == document ?
63 name == 'Left' ? val : $(window)[ 'scrollLeft' ](),
64 name == 'Top' ? val : $(window)[ 'scrollTop' ]()
66 this[ 'scroll' + name ] = val;
69 // Return the scroll offset
70 this[0] == window || this[0] == document ?
71 self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
72 $.boxModel && document.documentElement[ 'scroll' + name ] ||
73 document.body[ 'scroll' + name ] :
74 this[0][ 'scroll' + name ];
79 position: function() {
80 var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
83 // Get *real* offsetParent
84 offsetParent = this.offsetParent();
86 // Get correct offsets
87 offset = this.offset();
88 parentOffset = offsetParent.offset();
90 // Subtract element margins
91 offset.top -= num(elem, 'marginTop');
92 offset.left -= num(elem, 'marginLeft');
94 // Add offsetParent borders
95 parentOffset.top += num(offsetParent, 'borderTopWidth');
96 parentOffset.left += num(offsetParent, 'borderLeftWidth');
98 // Subtract the two offsets
100 top: offset.top - parentOffset.top,
101 left: offset.left - parentOffset.left
108 offsetParent: function() {
109 var offsetParent = this[0].offsetParent;
110 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && $.css(offsetParent, 'position') == 'static') )
111 offsetParent = offsetParent.offsetParent;
112 return $(offsetParent);
116 function num(el, prop) {
117 return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;