Statistiques
| Révision :

root / tei / js / jquery.dimensions.js @ 2

Historique | Voir | Annoter | Télécharger (3,44 ko)

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$
6
 * $Rev$
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 this.is(':visible') ? this[0]['client' + name] : 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
                var val = this.is(':visible') ? 
42
                                this[0]['offset' + name] : 
43
                                num( this, name.toLowerCase() )
44
                                        + num(this, 'border' + torl + 'Width') + num(this, 'border' + borr + 'Width')
45
                                        + num(this, 'padding' + torl) + num(this, 'padding' + borr);
46
                
47
                return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
48
        };
49
});
50

    
51
// Create scrollLeft and scrollTop methods
52
$.each( ['Left', 'Top'], function(i, name) {
53
        $.fn[ 'scroll' + name ] = function(val) {
54
                if (!this[0]) return;
55
                
56
                return val != undefined ?
57
                
58
                        // Set the scroll offset
59
                        this.each(function() {
60
                                this == window || this == document ?
61
                                        window.scrollTo( 
62
                                                name == 'Left' ? val : $(window)[ 'scrollLeft' ](),
63
                                                name == 'Top'  ? val : $(window)[ 'scrollTop'  ]()
64
                                        ) :
65
                                        this[ 'scroll' + name ] = val;
66
                        }) :
67
                        
68
                        // Return the scroll offset
69
                        this[0] == window || this[0] == document ?
70
                                self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
71
                                        $.boxModel && document.documentElement[ 'scroll' + name ] ||
72
                                        document.body[ 'scroll' + name ] :
73
                                this[0][ 'scroll' + name ];
74
        };
75
});
76

    
77
$.fn.extend({
78
        position: function() {
79
                var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
80
                
81
                if (elem) {
82
                        // Get *real* offsetParent
83
                        offsetParent = this.offsetParent();
84
                        
85
                        // Get correct offsets
86
                        offset       = this.offset();
87
                        parentOffset = offsetParent.offset();
88
                        
89
                        // Subtract element margins
90
                        offset.top  -= num(elem, 'marginTop');
91
                        offset.left -= num(elem, 'marginLeft');
92
                        
93
                        // Add offsetParent borders
94
                        parentOffset.top  += num(offsetParent, 'borderTopWidth');
95
                        parentOffset.left += num(offsetParent, 'borderLeftWidth');
96
                        
97
                        // Subtract the two offsets
98
                        results = {
99
                                top:  offset.top  - parentOffset.top,
100
                                left: offset.left - parentOffset.left
101
                        };
102
                }
103
                
104
                return results;
105
        },
106
        
107
        offsetParent: function() {
108
                var offsetParent = this[0].offsetParent;
109
                while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && $.css(offsetParent, 'position') == 'static') )
110
                        offsetParent = offsetParent.offsetParent;
111
                return $(offsetParent);
112
        }
113
});
114

    
115
function num(el, prop) {
116
        return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;
117
};
118

    
119
})(jQuery);