
jQuery: The Write Less, Do More JavaScript Library

    * Download
    * Documentation
    * Plugins
    * Tutorials
    * Discuss
    * Blog

    * Login
    * About Trac
    * Preferences
    * Help/Guide
    * Register

    * Wiki
    * Roadmap
    * Browse Source
    * View Tickets
    * Search
    * Timeline

    * Last Change
    * Annotate
    * Revision Log

root / trunk / plugins / accordion / lib / jquery.dimensions.js
View revision:
Visit: trunkbranches/1.2branches/1.2-fxbranches/event_enhancementsbranches/jake-devbranches/joern-devbranches/john-devbranches/kelvin-devbranches/paul-devbranches/paulm-devbranches/pluginsbranches/richard-devbranches/sean-devbranches/stefan-devbranches/tane-devbranches/ui-experimentalbranches/yehuda-devtags/1.0tags/1.0.1tags/1.0.2tags/1.0.3tags/1.0.4tags/1.1tags/1.1.1tags/1.1.2tags/1.1.3tags/1.1.3.1tags/1.1.3atags/1.1.4tags/1.1atags/1.1btags/1.2tags/1.2.1tags/1.2.2tags/1.2.2btags/1.2.2b2tags/1.2.3atags/pluginstags/ui
Revision 3915, 3.4 kB (checked in by joern.zaefferer, 2 months ago)
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-10-06 20:11:15 +0200 (Sa, 06 Okt 2007) $
6	 * $Rev: 3581 $
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	
112	function 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 browser.
Download in other formats:

    * Plain Text
    * Original Format

© 2007 John Resig and the jQuery team.

    * Download
    * Documentation
    * Plugins
    * Tutorials
    * Discuss
    * Blog

