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

您的位置:首頁技術(shù)文章
文章詳情頁

使用display:none時(shí)隱藏DOM元素?zé)o法獲取實(shí)際寬高的解決方法

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

案例說明

當(dāng)DOM元素被添加上display:none;的樣式時(shí),這個(gè)元素和它的子元素?zé)o法獲取到實(shí)際的寬高。
如圖,我設(shè)置了一個(gè)父元素和一個(gè)子元素,并且通過一個(gè)按鈕切換父元素是否帶有display:none;。

然后每次點(diǎn)擊按鈕后,在控制臺輸出兩個(gè)元素的高度。

可以看到,當(dāng)元素正常顯示時(shí),獲取寬高正常。而當(dāng)元素添加上`display:none;`之后,獲取到的值變?yōu)?.

解決方法

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

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

這里需要注意的是,當(dāng)元素設(shè)置為塊級元素時(shí),可能會因?yàn)槠浯笮∈沟弥車脑乇煌苿樱虼宋覀冊讷@取某個(gè)元素的實(shí)際寬高時(shí),除了設(shè)置display和visibility,還要設(shè)置position:absolute;,在獲取到寬高值之后取消掉。

這個(gè)實(shí)現(xiàn)方法其實(shí)就是jquery的actual方法的內(nèi)容:

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

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

/*! 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方法的參數(shù)

Example Code:(來源于jquery.actual github項(xiàng)目說明)// 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" });

到此這篇關(guān)于使用display:none時(shí)隱藏DOM元素?zé)o法獲取實(shí)際寬高的解決方法的文章就介紹到這了,更多相關(guān)隱藏DOM元素?zé)o法獲取實(shí)際寬高的解決方法內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: CSS HTML
主站蜘蛛池模板: 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 无刷电机_直流无刷电机_行星减速机-佛山市藤尺机电设备有限公司 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 陶瓷砂磨机,盘式砂磨机,棒销式砂磨机-无锡市少宏粉体科技有限公司 | 顶空进样器-吹扫捕集仪-热脱附仪-二次热解吸仪-北京华盛谱信仪器 | 喷漆房_废气处理设备-湖北天地鑫环保设备有限公司 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 蒜肠网-动漫,二次元,COSPLAY,漫展以及收藏型模型,手办,玩具的新媒体.(原变形金刚变迷TF圈) | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | 铝合金重力铸造_铝合金翻砂铸造_铝铸件厂家-东莞市铝得旺五金制品有限公司 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 北京办公室装修,办公室设计,写字楼装修-北京金视觉装饰工程公司 北京成考网-北京成人高考网 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 开锐教育-学历提升-职称评定-职业资格培训-积分入户 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 纯化水设备-EDI-制药-实验室-二级反渗透-高纯水|超纯水设备 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 桑茶-七彩贝壳桑叶茶 长寿茶 | 电镀电源整流器_高频电解电源_单脉双脉冲电源 - 东阳市旭东电子科技 | 必胜高考网_全国高考备考和志愿填报信息平台 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 大型冰雕-景区冰雕展制作公司,3D创意设计源头厂家-[赛北冰雕] | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 实验室隔膜泵-无油防腐蚀隔膜泵-耐腐蚀隔膜真空泵-杭州景程仪器 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 齿轮减速马达一体式_蜗轮蜗杆减速机配电机-德国BOSERL齿轮减速电动机生产厂家 | 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 法钢特种钢材(上海)有限公司 - 耐磨钢板、高强度钢板销售加工 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 |