-
Notifications
You must be signed in to change notification settings - Fork 12
/
jquery.dimensions.js
116 lines (92 loc) · 3.36 KB
/
jquery.dimensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Copyright (c) 2007 Paul Bakaus ([email protected]) and Brandon Aaron ([email protected] || http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* $LastChangedDate: 2007-09-11 02:38:31 +0000 (Tue, 11 Sep 2007) $
* $Rev: 3238 $
*
* Version: @VERSION
*
* Requires: jQuery 1.2+
*/
(function($){
$.dimensions = {
version: '@VERSION'
};
// Create innerHeight, innerWidth, outerHeight and outerWidth methods
$.each( [ 'Height', 'Width' ], function(i, name){
// innerHeight and innerWidth
$.fn[ 'inner' + name ] = function() {
if (!this[0]) return;
var torl = name == 'Height' ? 'Top' : 'Left', // top or left
borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
return this[ name.toLowerCase() ]() + num(this, 'padding' + torl) + num(this, 'padding' + borr);
};
// outerHeight and outerWidth
$.fn[ 'outer' + name ] = function(options) {
if (!this[0]) return;
var torl = name == 'Height' ? 'Top' : 'Left', // top or left
borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
options = $.extend({ margin: false }, options || {});
return this[ name.toLowerCase() ]()
+ num(this, 'border' + torl + 'Width') + num(this, 'border' + borr + 'Width')
+ num(this, 'padding' + torl) + num(this, 'padding' + borr)
+ (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
};
});
// Create scrollLeft and scrollTop methods
$.each( ['Left', 'Top'], function(i, name) {
$.fn[ 'scroll' + name ] = function(val) {
if (!this[0]) return;
return val != undefined ?
// Set the scroll offset
this.each(function() {
this == window || this == document ?
window.scrollTo(
name == 'Left' ? val : $(window)[ 'scrollLeft' ](),
name == 'Top' ? val : $(window)[ 'scrollTop' ]()
) :
this[ 'scroll' + name ] = val;
}) :
// Return the scroll offset
this[0] == window || this[0] == document ?
self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
$.boxModel && document.documentElement[ 'scroll' + name ] ||
document.body[ 'scroll' + name ] :
this[0][ 'scroll' + name ];
};
});
$.fn.extend({
position: function() {
var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
if (elem) {
// Get *real* offsetParent
offsetParent = this.offsetParent();
// Get correct offsets
offset = this.offset();
parentOffset = offsetParent.offset();
// Subtract element margins
offset.top -= num(elem, 'marginTop');
offset.left -= num(elem, 'marginLeft');
// Add offsetParent borders
parentOffset.top += num(offsetParent, 'borderTopWidth');
parentOffset.left += num(offsetParent, 'borderLeftWidth');
// Subtract the two offsets
results = {
top: offset.top - parentOffset.top,
left: offset.left - parentOffset.left
};
}
return results;
},
offsetParent: function() {
var offsetParent = this[0].offsetParent;
while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && $.css(offsetParent, 'position') == 'static') )
offsetParent = offsetParent.offsetParent;
return $(offsetParent);
}
});
var num = function(el, prop) {
return parseInt($.css(el.jquery?el[0]:el,prop))||0;
};
})(jQuery);