搜尋
首頁web前端js教程無法取得隱藏元素寬度和高度如何解決
無法取得隱藏元素寬度和高度如何解決Jan 23, 2018 pm 01:13 PM
元素寬度隱藏

在實際開發中會遇到確實需要取得隱藏元素的寬高,這兒所說的隱藏元素是display為none的元素。本文主要介紹了無法取得隱藏元素(display:none)寬度(width)和高度(height)的解決方案,具有很好的參考價值。下面跟著小編一起來看吧,希望能幫助大家。

可使用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取得隱藏元素的寬高

js取得隱藏元素寬高的實作方法_javascript技巧


jquery顯示隱藏元素的實作程式碼_jquery##############

以上是無法取得隱藏元素寬度和高度如何解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在JavaFX中,有哪些不同的路径元素?在JavaFX中,有哪些不同的路径元素?Aug 28, 2023 pm 12:53 PM

javafx.scene.shape包提供了一些类,您可以使用它们绘制各种2D形状,但这些只是原始形状,如直线、圆形、多边形和椭圆形等等...因此,如果您想绘制复杂的自定义形状,您需要使用Path类。Path类Path类使用此表示形状的几何轮廓您可以绘制自定义路径。为了绘制自定义路径,JavaFX提供了各种路径元素,所有这些都可以作为javafx.scene.shape包中的类使用。LineTo-该类表示路径元素line。它可以帮助您从当前坐标到指定(新)坐标绘制一条直线。HlineTo-这是表

C++程序:向数组中添加一个元素C++程序:向数组中添加一个元素Aug 25, 2023 pm 10:29 PM

数组是一种线性顺序数据结构,用于在连续的内存位置中保存同质数据。与其他数据结构一样,数组也必须具备以某种有效方式插入、删除、遍历和更新元素的功能。在C++中,我们的数组是静态的。C++中还提供了一些动态数组结构。对于静态数组,该数组内可能存储Z个元素。到目前为止,我们已经有n个元素了。在本文中,我们将了解如何在C++中在数组末尾插入元素(也称为追加元素)。通过示例理解概念‘this’关键字的使用方式如下GivenarrayA=[10,14,65,85,96,12,35,74,69]Afterin

html5不支持哪些元素html5不支持哪些元素Aug 11, 2023 pm 01:25 PM

html5不支持的元素有纯表现性元素、基于框架的元素、应用程序元素、可替换元素和旧的表单元素。详细介绍:1、纯表现性的元素,如font、center、s、u等,这些元素通常被用于控制文本样式和布局;2、基于框架的元素,如frame、frameset和noframes,这些元素在过去用于创建网页布局和分割窗口;3、应用程序相关的元素,如applet和isinde等等。

jquery怎么移除 元素jquery怎么移除 元素Feb 17, 2023 am 09:49 AM

jquery移除元素的方法:1、通过jQuery remove()方法删除被选元素及其子元素,语法是“$("#div1").remove();”;2、通过jQuery empty()方法删除被选元素的子元素,语法是“$("#div1").empty();”。

Vue3中的ref函数详解:直接访问组件元素的应用Vue3中的ref函数详解:直接访问组件元素的应用Jun 18, 2023 am 11:51 AM

在Vue3中,ref函数是非常有用的,在开发过程中提供了很方便的操作方式。它允许直接访问Vue组件元素并对其进行操作。ref函数是一个创建一个被响应式地绑定的对象的函数。可以在Vue组件中使用它来引用一个元素或子组件,并从父组件操作这些元素或子组件。ref函数返回一个响应式的对象,并通过该对象暴露指定元素或子组件的引用。因此,可以通过该对象直接访问元素或子组

Python程序用于测试列表中的所有元素是否最大间隔为KPython程序用于测试列表中的所有元素是否最大间隔为KAug 28, 2023 pm 05:25 PM

在许多编程场景中,我们都会遇到需要确定列表中的所有元素是否最多相距K个位置的情况。这个问题出现在各个领域,例如数据分析、序列处理和算法挑战。能够测试和验证这些条件对于确保我们程序的完整性和正确性至关重要。在本文中,我们将探索一个Python程序来有效地解决这个问题。我们将讨论这个概念,提出解决问题的分步方法,并提供工作代码实现。读完本文后,您将清楚地了解如何使用Python检查列表中的元素是否最多相距K个位置。理解问题在深入研究解决方案之前,让我们首先详细了解问题陈述。给定一个元素列表,我们需要

Java程序:在循环链表中搜索元素Java程序:在循环链表中搜索元素Sep 11, 2023 am 11:45 AM

什么是喜欢列表和循环链表?链表是一种数据结构,其中每个节点都包含两部分,数据和地址路径。这些部分指向下一个节点,该节点始终与先前的节点创建互连。基于此,循环链表是最后一个节点与第一个节点有内部链接,这就是这种类型的链表称为循环链表。在Java环境中,当我们查找元素循环链表时,需要在链表中创建一个临时节点来指向。这样我们还需要声明两个变量。它们是曲目索引和曲目搜索。如果Temp节点在起始点为空,那么遍历列表就很重要,因为此时它不包含任何项目。循环链表的工作原理及其应用?循环链表的工作方法对于循环链

如何在Java中删除ArrayList的所有元素?如何在Java中删除ArrayList的所有元素?Sep 20, 2023 pm 02:21 PM

List接口扩展了Collection接口并存储元素序列。List接口提供了两种方法来有效地在列表中的任意点插入和删除多个元素。与集合不同,列表允许重复元素,如果列表中允许空值,则允许多个空值。List提供了add、remove方法来添加/删除元素。为了清除列表或从列表中删除所有元素,我们可以使用List的clear()方法。我们也可以使用removeAll()方法来达到与clear()方法相同的效果。在本文中,我们将通过相应的示例介绍clear()和removeAll()方法。语法-clear

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境