1
|
/**
|
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.
|
5
|
*
|
6
|
* $LastChangedDate: 2007-12-20 08:46:55 -0600 (Thu, 20 Dec 2007) $
|
7
|
* $Rev: 4259 $
|
8
|
*
|
9
|
* Version: 1.2
|
10
|
*
|
11
|
* Requires: jQuery 1.2+
|
12
|
*/
|
13
|
|
14
|
(function($) {
|
15
|
|
16
|
$.dimensions = {
|
17
|
version : '1.2'
|
18
|
};
|
19
|
|
20
|
// Create innerHeight, innerWidth, outerHeight and outerWidth methods.
|
21
|
$.each([ 'Height', 'Width' ], function(i, name) {
|
22
|
|
23
|
// innerHeight and innerWidth.
|
24
|
$.fn['inner' + name] = function() {
|
25
|
if (!this[0])
|
26
|
return;
|
27
|
|
28
|
var torl = name == 'Height' ? 'Top' : 'Left', // Top or left.
|
29
|
borr = name == 'Height' ? 'Bottom' : 'Right'; // Bottom or right.
|
30
|
|
31
|
return this.is(':visible') ? this[0]['client' + name] : num(this, name
|
32
|
.toLowerCase())
|
33
|
+ num(this, 'padding' + torl) + num(this, 'padding' + borr);
|
34
|
};
|
35
|
|
36
|
// outerHeight and outerWidth.
|
37
|
$.fn['outer' + name] = function(options) {
|
38
|
if (!this[0])
|
39
|
return;
|
40
|
|
41
|
var torl = name == 'Height' ? 'Top' : 'Left', // Top or left.
|
42
|
borr = name == 'Height' ? 'Bottom' : 'Right'; // Bottom or right.
|
43
|
|
44
|
options = $.extend({
|
45
|
margin : false
|
46
|
}, options || {});
|
47
|
|
48
|
var val = this.is(':visible') ? this[0]['offset' + name] : num(this, name
|
49
|
.toLowerCase())
|
50
|
+ num(this, 'border' + torl + 'Width')
|
51
|
+ num(this, 'border' + borr + 'Width')
|
52
|
+ num(this, 'padding' + torl)
|
53
|
+ num(this, 'padding' + borr);
|
54
|
|
55
|
return val
|
56
|
+ (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin'
|
57
|
+ borr)) : 0);
|
58
|
};
|
59
|
});
|
60
|
|
61
|
// Create scrollLeft and scrollTop methods.
|
62
|
$.each(
|
63
|
[ 'Left', 'Top' ],
|
64
|
function(i, name) {
|
65
|
$.fn['scroll' + name] = function(val) {
|
66
|
if (!this[0])
|
67
|
return;
|
68
|
|
69
|
return val != undefined ?
|
70
|
|
71
|
// Set the scroll offset
|
72
|
this.each(function() {
|
73
|
this == window || this == document ? window.scrollTo(
|
74
|
name == 'Left' ? val : $(window)['scrollLeft'](),
|
75
|
name == 'Top' ? val : $(window)['scrollTop']())
|
76
|
: this['scroll' + name] = val;
|
77
|
})
|
78
|
:
|
79
|
|
80
|
// Return the scroll offset.
|
81
|
this[0] == window || this[0] == document ? self[(name == 'Left' ? 'pageXOffset'
|
82
|
: 'pageYOffset')]
|
83
|
|| $.boxModel
|
84
|
&& document.documentElement['scroll' + name]
|
85
|
|| document.body['scroll' + name]
|
86
|
: this[0]['scroll' + name];
|
87
|
};
|
88
|
});
|
89
|
|
90
|
$.fn
|
91
|
.extend({
|
92
|
position : function() {
|
93
|
var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
|
94
|
|
95
|
if (elem) {
|
96
|
// Get *real* offsetParent.
|
97
|
offsetParent = this.offsetParent();
|
98
|
|
99
|
// Get correct offsets.
|
100
|
offset = this.offset();
|
101
|
parentOffset = offsetParent.offset();
|
102
|
|
103
|
// Subtract element margins.
|
104
|
offset.top -= num(elem, 'marginTop');
|
105
|
offset.left -= num(elem, 'marginLeft');
|
106
|
|
107
|
// Add offsetParent borders.
|
108
|
parentOffset.top += num(offsetParent, 'borderTopWidth');
|
109
|
parentOffset.left += num(offsetParent, 'borderLeftWidth');
|
110
|
|
111
|
// Subtract the two offsets.
|
112
|
results = {
|
113
|
top : offset.top - parentOffset.top,
|
114
|
left : offset.left - parentOffset.left
|
115
|
};
|
116
|
}
|
117
|
|
118
|
return results;
|
119
|
},
|
120
|
|
121
|
offsetParent : function() {
|
122
|
var offsetParent = this[0].offsetParent;
|
123
|
while (offsetParent
|
124
|
&& (!/^body|html$/i.test(offsetParent.tagName) && $.css(
|
125
|
offsetParent, 'position') == 'static'))
|
126
|
offsetParent = offsetParent.offsetParent;
|
127
|
return $(offsetParent);
|
128
|
}
|
129
|
});
|
130
|
|
131
|
function num(el, prop) {
|
132
|
return parseInt($.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
|
133
|
}
|
134
|
;
|
135
|
|
136
|
})(jQuery);
|