使用display:none時(shí)隱藏DOM元素?zé)o法獲取實(shí)際寬高的解決方法
案例說(shuō)明
當(dāng)DOM元素被添加上display:none;的樣式時(shí),這個(gè)元素和它的子元素?zé)o法獲取到實(shí)際的寬高。
如圖,我設(shè)置了一個(gè)父元素和一個(gè)子元素,并且通過(guò)一個(gè)按鈕切換父元素是否帶有display:none;。
然后每次點(diǎn)擊按鈕后,在控制臺(tái)輸出兩個(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>
語(yǔ)法格式
//獲取帶有.hidden類(lèi)的元素的實(shí)際高度
$('.hidden').actual('height');
使用actual方法后獲取寬高正常,無(wú)論元素有沒(méi)有被設(shè)置display:none;。(下圖中兩次輸出,一次是帶有display:none;的,一次是沒(méi)有的,均能獲取到實(shí)際高度)
原理:設(shè)置display:none;的元素沒(méi)有物理尺寸,而同樣能提供隱形效果的visibility則有物理尺寸,但是不能直接用visibility:hidden;代替display:none;,因?yàn)樵O(shè)置visibility之后的元素還是會(huì)占用頁(yè)面空間的。正確的解決方法是要獲取寬度或者高度時(shí),首先將display:none;更換成display:block;,然后設(shè)置visibility讓其隱形,從而獲取實(shí)際寬高。
這里需要注意的是,當(dāng)元素設(shè)置為塊級(jí)元素時(shí),可能會(huì)因?yàn)槠浯笮∈沟弥車(chē)脑乇煌苿?dòng),因此我們?cè)讷@取某個(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.
如果打不開(kāi)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:(來(lái)源于jquery.actual github項(xiàng)目說(shuō)明) // 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 }); // 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
- 有時(shí)候圖片太大,會(huì)破環(huán)網(wǎng)頁(yè)整齊的布局。這時(shí)可以用css來(lái)強(qiáng)制按比例壓縮圖片的高度或?qū)挾取?/div> 2008-06-06
網(wǎng)頁(yè)中關(guān)閉IE的圖像工具欄和XP主題
網(wǎng)頁(yè)中關(guān)閉IE的圖像工具欄和XP主題...2007-01-01Cross-Browser Variable Opacity with PNG
Cross-Browser Variable Opacity with PNG...2006-12-12淺談html中id和name的區(qū)別實(shí)例代碼
這個(gè)是form里面的name與id的區(qū)別2008-07-07調(diào)用265天氣預(yù)報(bào),不顯示鏈接
調(diào)用265天氣預(yù)報(bào),不顯示鏈接...2006-12-12relative absolute無(wú)法沖破的等級(jí)問(wèn)題解決
如果我我們?cè)O(shè)定LI為position:relative;設(shè)置span為position:absolute;那么我們會(huì)發(fā)現(xiàn)無(wú)論SPAN的z-index值設(shè)置得再高都將永遠(yuǎn)在后面父級(jí)的下面。2008-06-06最新評(píng)論