source: extensions/Autosize/js/jquery.dimensions.js @ 7353

Last change on this file since 7353 was 7353, checked in by cljosse, 14 years ago

[Autosize][beta] fix bug with Opera.


File size: 8.1 KB
Line 
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.
4 *
5 * $LastChangedDate: 2007-12-20 08:43:48 -0600 (Thu, 20 Dec 2007) $
6 * $Rev: 4257 $
7 *
8 * $LastChangedDate: 2010-10-23 08:43:48 -0600 (Thu, 20 Dec 2007) $
9 * By cljosse
10 * Version: 1.2
11 *
12 * Requires: jQuery 1.2+
13 *
14 */
15(function (jQuery) {
16    jQuery.dimensions = { version: '1.3' };
17    /*
18    * Interception Height, Width
19    */
20    jQuery.each(['Height', 'Width'],
21            function (i, name) {
22                jQuery.fn['inner' + name] = function () {
23                    if (!this[0]) return;
24                    var torl = name == 'Height' ? 'Top' : 'Left',
25                        borr = name == 'Height' ? 'Bottom' : 'Right';
26                    return this.is(':visible') ? this[0]['client' + name] : num(this, name.toLowerCase()) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
27                };
28
29                jQuery.fn['outer' + name] = function (options) {
30                    if (!this[0]) return;
31                    var torl = name == 'Height' ? 'Top' : 'Left',
32                        borr = name == 'Height' ? 'Bottom' : 'Right';
33                    options = jQuery.extend({ margin: false },
34                                                options || {});
35                    var val = this.is(':visible') ? this[0]['offset' + name] :
36                        num(this,
37                        name.toLowerCase()) +
38                        num(this, 'border' + torl + 'Width') +
39                        num(this, 'border' + borr + 'Width') +
40                        num(this, 'padding' + torl) +
41                        num(this, 'padding' + borr);
42                    return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
43                };
44            });
45    /*
46    *
47    */
48    jQuery.each(['Left', 'Top'],
49      function (i, name) {
50          jQuery.fn['scroll' + name] = function (val) {
51              if (!this[0]) return;
52              return val != undefined ? this.each(function () {
53                  this == window || this == document ? window.scrollTo(name == 'Left' ? val : $(window)['scrollLeft'](),
54           name == 'Top' ? val : $(window)['scrollTop']()) : this['scroll' + name] = val;
55              }) : this[0] == window || this[0] == document ? self[(name == 'Left' ? 'pageXOffset' : 'pageYOffset')] || jQuery.boxModel && document.documentElement['scroll' + name] || document.body['scroll' + name] : this[0]['scroll' + name];
56          };
57
58      });
59    jQuery.fn.extend({
60
61        info: function () {
62            var width = 0, height = 0;
63            var elem = jQuery(this).get(0);
64
65
66            var left = 0, top = 0, offset, parentOffset, offsetParent, results;
67            var borderwidth = { width: "0 0 0 0",
68                top: 0,
69                left: 0,
70                right: 0,
71                bottom: 0
72            }
73            var padding = { padding: "0 0 0 0",
74                top: 0,
75                left: 0,
76                right: 0,
77                bottom: 0
78            };
79            var margin = { margin: "0 0 0 0",
80                top: 0,
81                left: 0,
82                right: 0,
83                bottom: 0
84            };
85            //=====================================================================
86            results = {
87                position: jQuery(elem).css("position"),
88                top: 0,
89                left: 0,
90                width: 0,
91                height: 0,
92                right: 0,
93                bottom: 0,
94                borderwidth: borderwidth,
95                padding: padding,
96                margin: margin
97            };
98            ;
99            if (elem) {
100                Position = jQuery(elem).position();
101                left = Position.left;
102                top = Position.top;
103                width = jQuery(elem).outerWidth();
104                height = jQuery(elem).outerHeight();
105
106
107                borderwidth.left = jQuery(elem).css("borderLeftWidth");
108                borderwidth.right = jQuery(elem).css("borderRightWidth");
109                borderwidth.top = jQuery(elem).css("borderTopWidth");
110                borderwidth.bottom = jQuery(elem).css("borderBottomWidth");
111                try {
112                    borderwidth.width = jQuery(elem).css("borderWidth");
113                } catch (e) {
114                    borderwidth.width = '"' + borderwidth.left + ' ' + borderwidth.top + ' ' + borderwidth.right + ' ' + borderwidth.bottom + '"';
115                }
116
117
118                margin.left = jQuery(elem).css("marginLeft");
119                margin.right = jQuery(elem).css("marginRight");
120                margin.top = jQuery(elem).css("marginTop");
121                margin.bottom = jQuery(elem).css("marginBottom");
122                try {
123                    margin.margin = jQuery(elem).css("margin");
124                } catch (e) {
125                    margin.margin = '"' + margin.left + ' ' + margin.top + ' ' + margin.right + ' ' + margin.bottom + '"';
126                }
127
128                padding.left = jQuery(elem).css("paddingLeft");
129                padding.right = jQuery(elem).css("paddingRight");
130                padding.top = jQuery(elem).css("paddingTop");
131                padding.bottom = jQuery(elem).css("paddingBottom");
132                try { padding.padding = jQuery(elem).css("padding"); } catch (e) {
133                    padding.padding = '"' + padding.left + ' ' + padding.top + ' ' + padding.right + ' ' + padding.bottom + '"';
134                }
135
136                results = {
137                    position: jQuery(elem).css("position"),
138                    top: top,
139                    left: left,
140                    width: width,
141                    height: height,
142                    right: left + width,
143                    bottom: top + height,
144                    borderwidth: borderwidth,
145                    margin: margin,
146                    padding: padding
147                };
148
149                return results
150            } return results;
151        },
152        /*
153        * Position de l'object elem
154        */
155        position: function () {
156            var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
157            if (elem) {
158                offsetParent = this.offsetParent();
159                offset = this.offset();
160                if (offsetParent) {
161                    if (typeof (offsetParent.offset) != "undefined")
162                        parentOffset = offsetParent.offset();
163                    else
164                        parentOffset = { top: 0, left: 0 };
165
166                    offset.top -= num(elem, 'marginTop');
167                    offset.left -= num(elem, 'marginLeft');
168
169                    if (offsetParent.length > 0) {
170                        parentOffset.top += num(offsetParent, 'borderTopWidth');
171                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
172                    } else {
173                        parentOffset = { top: 0, left: 0 };
174
175                    }
176                } else {
177                    parentOffset = { top: 0, left: 0 };
178                }
179                results = {
180                    top: offset.top - parentOffset.top, left: offset.left - parentOffset.left
181                };
182            } return results;
183        },
184        /*
185        * offsetParent
186        */
187        offsetParent: function () {
188            var offsetParent = this[0].offsetParent;
189            while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static'))
190                offsetParent = offsetParent.offsetParent;
191            return jQuery(offsetParent);
192        }
193    });
194    /*
195    *
196    */
197    function num(el, prop) {
198        return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0;
199    };
200})(jQuery);
Note: See TracBrowser for help on using the repository browser.