电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

使用display:none時隱藏DOM元素無法獲取實際寬高的解決方法

瀏覽:170日期:2022-06-03 09:31:25

案例說明

當DOM元素被添加上display:none;的樣式時,這個元素和它的子元素無法獲取到實際的寬高。
如圖,我設置了一個父元素和一個子元素,并且通過一個按鈕切換父元素是否帶有display:none;。

然后每次點擊按鈕后,在控制臺輸出兩個元素的高度。

可以看到,當元素正常顯示時,獲取寬高正常。而當元素添加上`display:none;`之后,獲取到的值變為0.

解決方法

使用jquery的actual方法可以很方便的獲取到元素的真實寬高。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>
語法格式
//獲取帶有.hidden類的元素的實際高度
$('.hidden').actual('height');
使用actual方法后獲取寬高正常,無論元素有沒有被設置display:none;。(下圖中兩次輸出,一次是帶有display:none;的,一次是沒有的,均能獲取到實際高度)

原理:設置display:none;的元素沒有物理尺寸,而同樣能提供隱形效果的visibility則有物理尺寸,但是不能直接用visibility:hidden;代替display:none;,因為設置visibility之后的元素還是會占用頁面空間的。正確的解決方法是要獲取寬度或者高度時,首先將display:none;更換成display:block;,然后設置visibility讓其隱形,從而獲取實際寬高。

這里需要注意的是,當元素設置為塊級元素時,可能會因為其大小使得周圍的元素被推動,因此我們在獲取某個元素的實際寬高時,除了設置display和visibility,還要設置position:absolute;,在獲取到寬高值之后取消掉。

這個實現方法其實就是jquery的actual方法的內容:

源地址jquery.actual:Get the actual width/height of invisible DOM elements with jQuery.

如果打不開github也可以看下面的代碼(jquery.actual.js的代碼內容)

/*! Copyright 2012, Ben Lin (http://dreamerslab.com/) * Licensed under the MIT License (LICENSE.txt). * * Version: 1.0.19 * * Requires: jQuery >= 1.2.3 */;( 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;    }  });}));

actual方法的參數

Example Code:(來源于jquery.actual github項目說明)// 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 }"http:// 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 }"http:// please see demo/css3pie in action$( ".hidden" ).actual( "width", { clone : true });// if it is not a block element. By default { display: "block" }.// for example a inline element$( ".hidden" ).actual( "width", { display: "inline-block" });

到此這篇關于使用display:none時隱藏DOM元素無法獲取實際寬高的解決方法的文章就介紹到這了,更多相關隱藏DOM元素無法獲取實際寬高的解決方法內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

標簽: CSS HTML
主站蜘蛛池模板: 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 电主轴,车床电磨头,变频制动电机-博山鸿达特种电机 | 气密性检测仪_气密性检测设备_防水测试仪_密封测试仪-岳信仪器 | 留学生辅导网-在线课程论文辅导-留学生挂科申诉机构 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 钢衬玻璃厂家,钢衬玻璃管道 -山东东兴扬防腐设备有限公司 | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 活性氧化铝球|氧化铝干燥剂|分子筛干燥剂|氢氧化铝粉-淄博同心材料有限公司 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | 波纹补偿器_不锈钢波纹补偿器_巩义市润达管道设备制造有限公司 | 二氧化碳/活性炭投加系统,次氯酸钠发生器,紫外线消毒设备|广州新奥 | 气动隔膜泵-电动隔膜泵-循环热水泵-液下排污/螺杆/管道/化工泵「厂家」浙江绿邦 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 今日扫码_溯源二维码_产品防伪一物一码_红包墙营销方案 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 钢制拖链生产厂家-全封闭钢制拖链-能源钢铝拖链-工程塑料拖链-河北汉洋机械制造有限公司 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 冻干机(冷冻干燥机)_小型|实验型|食品真空冷冻干燥机-松源 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 挤奶设备过滤纸,牛奶过滤纸,挤奶机过滤袋-济南蓝贝尔工贸有限公司 | 防勒索软件_数据防泄密_Trellix(原McAfee)核心代理商_Trellix(原Fireeye)售后-广州文智信息科技有限公司 | ISO9001认证咨询_iso9001企业认证代理机构_14001|18001|16949|50430认证-艾世欧认证网 | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 德国进口电锅炉_商用电热水器_壁挂炉_电采暖器_电热锅炉[德国宝] | 刹车盘机床-刹车盘生产线-龙口亨嘉智能装备| 免费分销系统 — 分销商城系统_分销小程序开发 -【微商来】 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 | 挤出机_橡胶挤出机_塑料挤出机_胶片冷却机-河北伟源橡塑设备有限公司 |