jquery-actual.js
107 lines
| 1 | /*! Copyright 2012, Ben Lin (http://dreamerslab.com/) |
| 2 | * Licensed under the MIT License (LICENSE.txt). |
| 3 | * |
| 4 | * Version: 1.0.19 |
| 5 | * |
| 6 | * Requires: jQuery >= 1.2.3 |
| 7 | */ |
| 8 | ;( function ( factory ) { |
| 9 | if ( typeof define === 'function' && define.amd ) { |
| 10 | // AMD. Register module depending on jQuery using requirejs define. |
| 11 | define( ['jquery'], factory ); |
| 12 | } else { |
| 13 | // No AMD. |
| 14 | factory( jQuery ); |
| 15 | } |
| 16 | }( function ( $ ){ |
| 17 | $.fn.addBack = $.fn.addBack || $.fn.andSelf; |
| 18 | |
| 19 | $.fn.extend({ |
| 20 | |
| 21 | actual : function ( method, options ){ |
| 22 | // check if the jQuery method exist |
| 23 | if( !this[ method ]){ |
| 24 | throw '$.actual => The jQuery method "' + method + '" you called does not exist'; |
| 25 | } |
| 26 | |
| 27 | var defaults = { |
| 28 | absolute : false, |
| 29 | clone : false, |
| 30 | includeMargin : false, |
| 31 | display : 'block' |
| 32 | }; |
| 33 | |
| 34 | var configs = $.extend( defaults, options ); |
| 35 | |
| 36 | var $target = this.eq( 0 ); |
| 37 | var fix, restore; |
| 38 | |
| 39 | if( configs.clone === true ){ |
| 40 | fix = function (){ |
| 41 | var style = 'position: absolute !important; top: -1000 !important; '; |
| 42 | |
| 43 | // this is useful with css3pie |
| 44 | $target = $target. |
| 45 | clone(). |
| 46 | attr( 'style', style ). |
| 47 | appendTo( 'body' ); |
| 48 | }; |
| 49 | |
| 50 | restore = function (){ |
| 51 | // remove DOM element after getting the width |
| 52 | $target.remove(); |
| 53 | }; |
| 54 | }else{ |
| 55 | var tmp = []; |
| 56 | var style = ''; |
| 57 | var $hidden; |
| 58 | |
| 59 | fix = function (){ |
| 60 | // get all hidden parents |
| 61 | $hidden = $target.parents().addBack().filter( ':hidden' ); |
| 62 | style += 'visibility: hidden !important; display: ' + configs.display + ' !important; '; |
| 63 | |
| 64 | if( configs.absolute === true ) style += 'position: absolute !important; '; |
| 65 | |
| 66 | // save the origin style props |
| 67 | // set the hidden el css to be got the actual value later |
| 68 | $hidden.each( function (){ |
| 69 | // Save original style. If no style was set, attr() returns undefined |
| 70 | var $this = $( this ); |
| 71 | var thisStyle = $this.attr( 'style' ); |
| 72 | |
| 73 | tmp.push( thisStyle ); |
| 74 | // Retain as much of the original style as possible, if there is one |
| 75 | $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style ); |
| 76 | }); |
| 77 | }; |
| 78 | |
| 79 | restore = function (){ |
| 80 | // restore origin style values |
| 81 | $hidden.each( function ( i ){ |
| 82 | var $this = $( this ); |
| 83 | var _tmp = tmp[ i ]; |
| 84 | |
| 85 | if( _tmp === undefined ){ |
| 86 | $this.removeAttr( 'style' ); |
| 87 | }else{ |
| 88 | $this.attr( 'style', _tmp ); |
| 89 | } |
| 90 | }); |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | fix(); |
| 95 | // get the actual value with user specific methed |
| 96 | // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc |
| 97 | // configs.includeMargin only works for 'outerWidth' and 'outerHeight' |
| 98 | var actual = /(outer)/.test( method ) ? |
| 99 | $target[ method ]( configs.includeMargin ) : |
| 100 | $target[ method ](); |
| 101 | |
| 102 | restore(); |
| 103 | // IMPORTANT, this plugin only return the value of the first element |
| 104 | return actual; |
| 105 | } |
| 106 | }); |
| 107 | })); |