source: trunk/template-common/lib/jquery.dimensions.js @ 2488

Last change on this file since 2488 was 2313, checked in by vdigital, 16 years ago

New: jQuery and Accordion Admin menus

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 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: 2008-04-26 13:19:24 +0000 (Sat, 26 Apr 2008) $
6 * $Rev: 2313 $
7 *
8 * Version: @VERSION
9 *
10 * Requires: jQuery 1.2+
11 */
12
13(function($){
14       
15$.dimensions = {
16        version: '@VERSION'
17};
18
19// Create innerHeight, innerWidth, outerHeight and outerWidth methods
20$.each( [ 'Height', 'Width' ], function(i, name){
21       
22        // innerHeight and innerWidth
23        $.fn[ 'inner' + name ] = function() {
24                if (!this[0]) return;
25               
26                var torl = name == 'Height' ? 'Top'    : 'Left',  // top or left
27                    borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
28               
29                return num( this, name.toLowerCase() ) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
30        };
31       
32        // outerHeight and outerWidth
33        $.fn[ 'outer' + name ] = function(options) {
34                if (!this[0]) return;
35               
36                var torl = name == 'Height' ? 'Top'    : 'Left',  // top or left
37                    borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
38               
39                options = $.extend({ margin: false }, options || {});
40               
41                return num( 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);
45        };
46});
47
48// Create scrollLeft and scrollTop methods
49$.each( ['Left', 'Top'], function(i, name) {
50        $.fn[ 'scroll' + name ] = function(val) {
51                if (!this[0]) return;
52               
53                return val != undefined ?
54               
55                        // Set the scroll offset
56                        this.each(function() {
57                                this == window || this == document ?
58                                        window.scrollTo( 
59                                                name == 'Left' ? val : $(window)[ 'scrollLeft' ](),
60                                                name == 'Top'  ? val : $(window)[ 'scrollTop'  ]()
61                                        ) :
62                                        this[ 'scroll' + name ] = val;
63                        }) :
64                       
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 ];
71        };
72});
73
74$.fn.extend({
75        position: function() {
76                var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
77               
78                if (elem) {
79                        // Get *real* offsetParent
80                        offsetParent = this.offsetParent();
81                       
82                        // Get correct offsets
83                        offset       = this.offset();
84                        parentOffset = offsetParent.offset();
85                       
86                        // Subtract element margins
87                        offset.top  -= num(elem, 'marginTop');
88                        offset.left -= num(elem, 'marginLeft');
89                       
90                        // Add offsetParent borders
91                        parentOffset.top  += num(offsetParent, 'borderTopWidth');
92                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
93                       
94                        // Subtract the two offsets
95                        results = {
96                                top:  offset.top  - parentOffset.top,
97                                left: offset.left - parentOffset.left
98                        };
99                }
100               
101                return results;
102        },
103       
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);
109        }
110});
111
112function num(el, prop) {
113        return parseInt($.css(el.jquery?el[0]:el,prop))||0;
114};
115
116})(jQuery);
Note: See TracBrowser for help on using the repository browser.