全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

无法获取隐藏元素宽度和高度的解决方案

在实际开发中会遇到确实需要获取隐藏元素的宽高,这儿所说的隐藏元素是display为none的元素。

可使用jQuery Actual Plugin插件来完成,其源码如下:

;( 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);
 

当然如果要支持模块化开发,直接使用官网下载的文件即可,源码也贴上:

;( 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;
  }
 });
}));

代码实例:

//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});

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


# jq获取隐藏元素宽度  # 获取隐藏元素的高度  # 获取隐藏元素的宽度  # javascript获取隐藏元素(display:none)的高度和宽度的方法  # js获取浏览器高度 窗口高度 元素尺寸 偏移属性的方法  # js获取Html元素的实际宽度高度的方法  # js获取页面及个元素高度、宽度的代码  # jquery如何获取元素的滚动条高度等实现代码  # jQuery获取页面及个元素高度、宽度的总结——超实用  # 一个JavaScript获取元素当前高度的实例  # 原生JS获取元素集合的子元素宽度实例  # 贴上  # 来完成  # 中会  # 官网  # 在实际  # style  # true  # restore  # position  # top  # important  # block  # props  # appendTo  # configs  # fix  # eq  # target  # hidden  # tmp 


相关文章: 建站上传速度慢?如何优化加速网站加载效率?  如何生成腾讯云建站专用兑换码?  C++用Dijkstra(迪杰斯特拉)算法求最短路径  IOS倒计时设置UIButton标题title的抖动问题  建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析  实例解析angularjs的filter过滤器  ,网页ppt怎么弄成自己的ppt?  宁波免费建站如何选择可靠模板与平台?  如何快速生成高效建站系统源代码?  如何在新浪SAE免费搭建个人博客?  ui设计制作网站有哪些,手机UI设计网址吗?  头像制作网站在线制作软件,dw网页背景图像怎么设置?  深圳网站制作培训,深圳哪些招聘网站比较好?  建站之星如何通过成品分离优化网站效率?  网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?  建站之星如何助力网站排名飙升?揭秘高效技巧  岳西云建站教程与模板下载_一站式快速建站系统操作指南  常州自助建站费用包含哪些项目?  安云自助建站系统如何快速提升SEO排名?  阿里云网站搭建费用解析:服务器价格与建站成本优化指南  建站VPS配置与SEO优化指南:关键词排名提升策略  如何在宝塔面板创建新站点?  制作公司内部网站有哪些,内网如何建网站?  如何获取上海专业网站定制建站电话?  无锡营销型网站制作公司,无锡网选车牌流程?  商务网站制作工程师,从哪几个方面把握电子商务网站主页和页面的特色设计?  可靠的网站设计制作软件,做网站设计需要什么样的电脑配置?  制作国外网站的软件,国外有哪些比较优质的网站推荐?  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  如何通过远程VPS快速搭建个人网站?  免费制作小说封面的网站有哪些,怎么接网站批量的封面单?  制作网站的软件免费下载,免费制作app哪个平台好?  如何选择CMS系统实现快速建站与SEO优化?  杭州银行网站设计制作流程,杭州银行怎么开通认证方式?  高配服务器限时抢购:企业级配置与回收服务一站式优惠方案  如何在自有机房高效搭建专业网站?  赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?  建站之星伪静态规则如何设置?  如何在云虚拟主机上快速搭建个人网站?  测试制作网站有哪些,测试性取向的权威测试或者网站?  如何在IIS7上新建站点并设置安全权限?  用v-html解决Vue.js渲染中html标签不被解析的问题  利用JavaScript实现拖拽改变元素大小  常州企业建站如何选择最佳模板?  电商平台网站制作流程,电商网站如何制作?  在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?  如何快速搭建高效服务器建站系统?  微信h5制作网站有哪些,免费微信H5页面制作工具?  免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?  视频网站制作教程,怎么样制作优酷网的小视频? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。