国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

無(wú)法獲取隱藏元素寬度和高度的解決方案

2019-11-19 17:17:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在實(shí)際開發(fā)中會(huì)遇到確實(shí)需要獲取隱藏元素的寬高,這兒所說(shuō)的隱藏元素是display為none的元素。

可使用jQuery Actual Plugin插件來(lái)完成,其源碼如下:

;( function ( $ ){ $.fn.addBack = $.fn.addBack || $.fn.andSelf; $.fn.extend({  actual : function ( method, options ){   // check if the jQuery method exist   if( !this[ method ]){    throw '$.actual => The jQuery method "' + method + '" you called does not exist';   }   var defaults = {    absolute   : false,    clone     : false,    includeMargin : false,    display    : 'block'   };   var configs = $.extend( defaults, options );   var $target = this.eq( 0 );   var fix, restore;   if( configs.clone === true ){    fix = function (){     var style = 'position: absolute !important; top: -1000 !important; ';     // this is useful with css3pie     $target = $target.      clone().      attr( 'style', style ).      appendTo( 'body' );    };    restore = function (){     // remove DOM element after getting the width     $target.remove();    };   }else{    var tmp  = [];    var style = '';    var $hidden;    fix = function (){     // get all hidden parents     $hidden = $target.parents().addBack().filter( ':hidden' );     style  += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';     if( configs.absolute === true ) style += 'position: absolute !important; ';     // save the origin style props     // set the hidden el css to be got the actual value later     $hidden.each( function (){      // Save original style. If no style was set, attr() returns undefined      var $this   = $( this );      var thisStyle = $this.attr( 'style' );      tmp.push( thisStyle );      // Retain as much of the original style as possible, if there is one      $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style );     });    };    restore = function (){     // restore origin style values     $hidden.each( function ( i ){      var $this = $( this );      var _tmp = tmp[ i ];      if( _tmp === undefined ){       $this.removeAttr( 'style' );      }else{       $this.attr( 'style', _tmp );      }     });    };   }   fix();   // get the actual value with user specific methed   // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc   // configs.includeMargin only works for 'outerWidth' and 'outerHeight'   var actual = /(outer)/.test( method ) ?    $target[ method ]( configs.includeMargin ) :    $target[ method ]();   restore();   // IMPORTANT, this plugin only return the value of the first element   return actual;  } });})(jQuery);
 

當(dāng)然如果要支持模塊化開發(fā),直接使用官網(wǎng)下載的文件即可,源碼也貼上:

;( function ( factory ) {if ( typeof define === 'function' && define.amd ) {  // AMD. Register module depending on jQuery using requirejs define.  define( ['jquery'], factory );} else {  // No AMD.  factory( jQuery );}}( function ( $ ){ $.fn.addBack = $.fn.addBack || $.fn.andSelf; $.fn.extend({  actual : function ( method, options ){   // check if the jQuery method exist   if( !this[ method ]){    throw '$.actual => The jQuery method "' + method + '" you called does not exist';   }   var defaults = {    absolute   : false,    clone     : false,    includeMargin : false,    display    : 'block'   };   var configs = $.extend( defaults, options );   var $target = this.eq( 0 );   var fix, restore;   if( configs.clone === true ){    fix = function (){     var style = 'position: absolute !important; top: -1000 !important; ';     // this is useful with css3pie     $target = $target.      clone().      attr( 'style', style ).      appendTo( 'body' );    };    restore = function (){     // remove DOM element after getting the width     $target.remove();    };   }else{    var tmp  = [];    var style = '';    var $hidden;    fix = function (){     // get all hidden parents     $hidden = $target.parents().addBack().filter( ':hidden' );     style  += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';     if( configs.absolute === true ) style += 'position: absolute !important; ';     // save the origin style props     // set the hidden el css to be got the actual value later     $hidden.each( function (){      // Save original style. If no style was set, attr() returns undefined      var $this   = $( this );      var thisStyle = $this.attr( 'style' );      tmp.push( thisStyle );      // Retain as much of the original style as possible, if there is one      $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style );     });    };    restore = function (){     // restore origin style values     $hidden.each( function ( i ){      var $this = $( this );      var _tmp = tmp[ i ];      if( _tmp === undefined ){       $this.removeAttr( 'style' );      }else{       $this.attr( 'style', _tmp );      }     });    };   }   fix();   // get the actual value with user specific methed   // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc   // configs.includeMargin only works for 'outerWidth' and 'outerHeight'   var actual = /(outer)/.test( method ) ?    $target[ method ]( configs.includeMargin ) :    $target[ method ]();   restore();   // IMPORTANT, this plugin only return the value of the first element   return actual;  } });}));

代碼實(shí)例:

//get hidden element actual width$('.hidden').actual('width');//get hidden element actual innerWidth$('.hidden').actual('innerWidth');//get hidden element actual outerWidth$('.hidden').actual('outerWidth');//get hidden element actual outerWidth and set the `includeMargin` argument$('.hidden').actual('outerWidth',{includeMargin:true});//get hidden element actual height$('.hidden').actual('height');//get hidden element actual innerHeight$('.hidden').actual('innerHeight');//get hidden element actual outerHeight$('.hidden').actual('outerHeight');// get hidden element actual outerHeight and set the `includeMargin` argument$('.hidden').actual('outerHeight',{includeMargin:true});//if the page jumps or blinks, pass a attribute '{ absolute : true }'//be very careful, you might get a wrong result depends on how you makrup your html and css$('.hidden').actual('height',{absolute:true});// if you use css3pie with a float element// for example a rounded corner navigation menu you can also try to pass a attribute '{ clone : true }'// please see demo/css3pie in action$('.hidden').actual('width',{clone:true});

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤凰县| 永德县| 文安县| 潜江市| 怀安县| 黔江区| 宜州市| 娄烦县| 石阡县| 临安市| 莱芜市| 佛学| 巨野县| 宁陕县| 方山县| 上虞市| 阜宁县| 霍林郭勒市| 临洮县| 威宁| 东方市| 邢台县| 沂源县| 德惠市| 若尔盖县| 巴林左旗| 英山县| 花莲县| 铁岭县| 莒南县| 宁陵县| 霍林郭勒市| 禹州市| 安仁县| 醴陵市| 突泉县| 阜新市| 德保县| 兰坪| 资中县| 洛宁县|