Changeset 465

Show
Ignore:
Timestamp:
11/30/08 17:48:46 (1 month ago)
Author:
armin
Message:

updated jquery dimensions plugin to v1.2

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pmapper/trunk/javascript/jquery/jquery.dimensions.js

    r49 r465  
    1 /** 
    2  * @cat Plugins/Dimensions 
    3  * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net) 
     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:46:55 -0600 (Thu, 20 Dec 2007) $ 
     6 * $Rev: 4259 $ 
     7 * 
     8 * Version: 1.2 
     9 * 
     10 * Requires: jQuery 1.2+ 
    411 */ 
    5   
    6  jQuery.fn.height=function(){if(this.get(0)==window)return self.innerHeight||jQuery.boxModel&&document.documentElement.clientHeight||document.body.clientHeight;if(this.get(0)==document)return Math.max(document.body.scrollHeight,document.body.offsetHeight);return this.css("height",arguments[0]);};jQuery.fn.width=function(){if(this.get(0)==window)return self.innerWidth||jQuery.boxModel&&document.documentElement.clientWidth||document.body.clientWidth;if(this.get(0)==document)return Math.max(document.body.scrollWidth,document.body.offsetWidth);return this.css("width",arguments[0]);};jQuery.fn.innerHeight=function(){return this.get(0)==window||this.get(0)==document?this.height():this.get(0).offsetHeight-parseInt(this.css("borderTop")||0)-parseInt(this.css("borderBottom")||0);};jQuery.fn.innerWidth=function(){return this.get(0)==window||this.get(0)==document?this.width():this.get(0).offsetWidth-parseInt(this.css("borderLeft")||0)-parseInt(this.css("borderRight")||0);};jQuery.fn.outerHeight=function(){return this.get(0)==window||this.get(0)==document?this.height():this.get(0).offsetHeight;};jQuery.fn.outerWidth=function(){return this.get(0)==window||this.get(0)==document?this.width():this.get(0).offsetWidth;};jQuery.fn.scrollLeft=function(){if(this.get(0)==window||this.get(0)==document)return self.pageXOffset||jQuery.boxModel&&document.documentElement.scrollLeft||document.body.scrollLeft;return this.get(0).scrollLeft;};jQuery.fn.scrollTop=function(){if(this.get(0)==window||this.get(0)==document)return self.pageYOffset||jQuery.boxModel&&document.documentElement.scrollTop||document.body.scrollTop;return this.get(0).scrollTop;};jQuery.fn.offset=function(refElem){if(!this[0])throw 'jQuery.fn.offset requires an element.';refElem=(refElem)?jQuery(refElem)[0]:null;var x=0,y=0,elem=this[0],parent=this[0],sl=0,st=0;do{if(parent.tagName=='BODY'||parent.tagName=='HTML'){if((jQuery.browser.safari||jQuery.browser.msie)&&jQuery.css(parent,'position')!='absolute'){x+=parseInt(jQuery.css(parent,'marginLeft'))||0;y+=parseInt(jQuery.css(parent,'marginTop'))||0;};break;};x+=parent.offsetLeft||0;y+=parent.offsetTop||0;if(jQuery.browser.mozilla||jQuery.browser.msie){x+=parseInt(jQuery.css(parent,'borderLeftWidth'))||0;y+=parseInt(jQuery.css(parent,'borderTopWidth'))||0;};if(jQuery.browser.mozilla&&jQuery.css(parent,'overflow')=='hidden'){x+=parseInt(jQuery.css(parent,'borderLeftWidth'))||0;y+=parseInt(jQuery.css(parent,'borderTopWidth'))||0;};var op=parent.offsetParent;do{sl+=parent.scrollLeft||0;st+=parent.scrollTop||0;parent=parent.parentNode;}while(parent !=op);}while(parent);if(refElem){var offset=jQuery(refElem).offset();x=x-offset.left;y=y-offset.top;sl=sl-offset.scrollLeft;st=st-offset.scrollTop;};if(jQuery.browser.safari||jQuery.browser.opera){x+=parseInt(jQuery.css(elem,'borderLeftWidth'))||0;y+=parseInt(jQuery.css(elem,'borderTopWidth'))||0;};return{top:y-st,left:x-sl,width:elem.offsetWidth,height:elem.offsetHeight,borderTop:parseInt(jQuery.css(elem,'borderTopWidth'))||0,borderLeft:parseInt(jQuery.css(elem,'borderLeftWidth'))||0,marginTop:parseInt(jQuery.css(elem,'marginTopWidth'))||0,marginLeft:parseInt(jQuery.css(elem,'marginLeftWidth'))||0,scrollTop:st,scrollLeft:sl,pageYOffset:window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,pageXOffset:window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0};}; 
     12 
     13(function($){ 
     14         
     15$.dimensions = { 
     16        version: '1.2' 
     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 
     115function num(el, prop) { 
     116        return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0; 
     117}; 
     118 
     119})(jQuery);