jquery.actual.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. $.fn.extend({
  19. actual : function ( method, options ){
  20. // check if the jQuery method exist
  21. if( !this[ method ]){
  22. throw '$.actual => The jQuery method "' + method + '" you called does not exist';
  23. }
  24. var defaults = {
  25. absolute : false,
  26. clone : false,
  27. includeMargin : false,
  28. display : 'block'
  29. };
  30. var configs = $.extend( defaults, options );
  31. var $target = this.eq( 0 );
  32. var fix, restore;
  33. if( configs.clone === true ){
  34. fix = function (){
  35. var style = 'position: absolute !important; top: -1000 !important; ';
  36. // this is useful with css3pie
  37. $target = $target.
  38. clone().
  39. attr( 'style', style ).
  40. appendTo( 'body' );
  41. };
  42. restore = function (){
  43. // remove DOM element after getting the width
  44. $target.remove();
  45. };
  46. }else{
  47. var tmp = [];
  48. var style = '';
  49. var $hidden;
  50. fix = function (){
  51. // get all hidden parents
  52. $hidden = $target.parents().addBack().filter( ':hidden' );
  53. style += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';
  54. if( configs.absolute === true ) style += 'position: absolute !important; ';
  55. // save the origin style props
  56. // set the hidden el css to be got the actual value later
  57. $hidden.each( function (){
  58. // Save original style. If no style was set, attr() returns undefined
  59. var $this = $( this );
  60. var thisStyle = $this.attr( 'style' );
  61. tmp.push( thisStyle );
  62. // Retain as much of the original style as possible, if there is one
  63. $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style );
  64. });
  65. };
  66. restore = function (){
  67. // restore origin style values
  68. $hidden.each( function ( i ){
  69. var $this = $( this );
  70. var _tmp = tmp[ i ];
  71. if( _tmp === undefined ){
  72. $this.removeAttr( 'style' );
  73. }else{
  74. $this.attr( 'style', _tmp );
  75. }
  76. });
  77. };
  78. }
  79. fix();
  80. // get the actual value with user specific methed
  81. // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc
  82. // configs.includeMargin only works for 'outerWidth' and 'outerHeight'
  83. var actual = /(outer)/.test( method ) ?
  84. $target[ method ]( configs.includeMargin ) :
  85. $target[ method ]();
  86. restore();
  87. // IMPORTANT, this plugin only return the value of the first element
  88. return actual;
  89. }
  90. });
  91. }));