iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >js如何解决无法获取隐藏元素宽度和高度的问题
  • 875
分享到

js如何解决无法获取隐藏元素宽度和高度的问题

2024-04-02 19:04:59 875人浏览 独家记忆
摘要

这篇文章给大家分享的是有关js如何解决无法获取隐藏元素宽度和高度的问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在实际开发中会遇到确实需要获取隐藏元素的宽高,这儿所说的隐藏元

这篇文章给大家分享的是有关js如何解决无法获取隐藏元素宽度和高度的问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

在实际开发中会遇到确实需要获取隐藏元素的宽高,这儿所说的隐藏元素是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});

感谢各位的阅读!关于“js如何解决无法获取隐藏元素宽度和高度的问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: js如何解决无法获取隐藏元素宽度和高度的问题

本文链接: https://www.lsjlt.com/news/69039.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • js如何解决无法获取隐藏元素宽度和高度的问题
    这篇文章给大家分享的是有关js如何解决无法获取隐藏元素宽度和高度的问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。在实际开发中会遇到确实需要获取隐藏元素的宽高,这儿所说的隐藏元...
    99+
    2024-04-02
  • javascript如何获取隐藏元素的高度
    这篇文章将为大家详细讲解有关javascript如何获取隐藏元素的高度,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。 获取方法:1、引入jqu...
    99+
    2024-04-02
  • 如何解决父级元素未设置高度和宽度时高度塌陷问题
    本篇内容介绍了“如何解决父级元素未设置高度和宽度时高度塌陷问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成...
    99+
    2024-04-02
  • 使用display:none时隐藏DOM元素无法获取实际宽高的解决方法
    案例说明 当DOM元素被添加上display:none;的样式时,这个元素和它的子元素无法获取到实际的宽高。如图,我设置了一个父元素和一个子元素,并且通过一个按钮切换父元素是否带有d...
    99+
    2024-04-02
  • 微信小程序动态获取元素宽度高度的方法
    本文小编为大家详细介绍“微信小程序动态获取元素宽度高度的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“微信小程序动态获取元素宽度高度的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。首先,这个接口会返回一...
    99+
    2023-06-26
  • 如何使用JavaScript设置元素的宽度和高度
    JavaScript是一种流行的编程语言,用于在Web开发中创建动态和交互式的用户界面。在Web应用程序中,开发人员通常需要设置元素的宽度和高度。本文将探讨如何使用JavaScript设置元素的宽度和高度。在HTML中,有两种方式设置元素的...
    99+
    2023-05-14
  • CSS如何隐藏超过固定宽度和高度的文字
    这篇文章主要为大家展示了“CSS如何隐藏超过固定宽度和高度的文字”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“CSS如何隐藏超过固定宽度和高度的文字”这篇文章吧...
    99+
    2024-04-02
  • 如何解决span无法设置宽度的问题
    本篇内容介绍了“如何解决span无法设置宽度的问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! ...
    99+
    2024-04-02
  • JS与Jquery如何获取屏幕、浏览器、页面的宽度和高度
    今天小编给大家分享一下JS与Jquery如何获取屏幕、浏览器、页面的宽度和高度的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。...
    99+
    2023-07-05
  • JavaScript如何获取当前窗口内的宽度和高度
    今天小编给大家分享一下JavaScript如何获取当前窗口内的宽度和高度的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Jav...
    99+
    2023-07-05
  • Dreamweaver中的AP元素如何修改宽度高度和颜色
    这篇文章主要介绍Dreamweaver中的AP元素如何修改宽度高度和颜色,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先我们找到桌面的网页设计软件,双击打开应用程序然后我们我们进来了,进入之后新建一个空白的HTML...
    99+
    2023-06-08
  • 如何解决ulli内容宽度的问题
    本篇文章为大家展示了如何解决ulli内容宽度的问题,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。先来解决问题:li{ max-width: 100px; ...
    99+
    2024-04-02
  • 如何解决CSS的最大高度、最小高度及宽度在IE6下没有效果的问题
    本篇内容介绍了“如何解决CSS的最大高度、最小高度及宽度在IE6下没有效果的问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细...
    99+
    2024-04-02
  • 如何解决表格设置table-layout:fixed后对单元格宽度设置无效的问题
    本篇内容主要讲解“如何解决表格设置table-layout:fixed后对单元格宽度设置无效的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决表格设置...
    99+
    2024-04-02
  • react事件对象无法获取offsetLeft,offsetTop,X,Y等元素问题及解决
    目录react事件对象无法获取offsetLeft,offsetTop,X,Y等元素解决方法获取offsetLeft,offsetTop值不准的原因遇坑总结react事件对象无法获取...
    99+
    2022-11-13
    react无法获取offsetLeft react无法获取offsetTop react无法获取X react无法获取Y
  • 如何解决绝对定位的元素在ie6下不显示隐藏了的问题
    本篇内容主要讲解“如何解决绝对定位的元素在ie6下不显示隐藏了的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决绝对定位的元素在ie6下不显示隐藏了的...
    99+
    2024-04-02
  • JS如何解决超出精度数字的问题
    这篇文章主要为大家展示了“JS如何解决超出精度数字的问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JS如何解决超出精度数字的问题”这篇文章吧。精度问题最通俗易懂的解释比如一个数 1÷3=0....
    99+
    2023-06-20
  • 如何解决css中高度塌陷的问题
    本文将为大家详细介绍“如何解决css中高度塌陷的问题”,内容步骤清晰详细,细节处理妥当,而小编每天都会更新不同的知识点,希望这篇“如何解决css中高度塌陷的问题”能够给你意想不到的收获,请大家跟着小编的思路慢慢深入,具体内容如下,一起去收获...
    99+
    2023-06-06
  • 如何解决img标签设置display:block属性时宽度无法设定为100%的问题
    小编给大家分享一下如何解决img标签设置display:block属性时宽度无法设定为100%的问题,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!   如下代码,img标签设置了disp...
    99+
    2024-04-02
  • 如何解决Layui表格自适应高度的问题
    这篇文章将为大家详细讲解有关如何解决Layui表格自适应高度的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在写一个项目,发现成功将数据在页面显示后, 高度没有自适应...
    99+
    2024-04-02
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作