摘要:在實(shí)際開(kāi)發(fā)中會(huì)遇到確實(shí)需要獲取隱藏元素的寬高,這兒所說(shuō)的隱藏元素是為的元素。可使用插件來(lái)完成,其源碼如下使用實(shí)例插件地址
在實(shí)際開(kāi)發(fā)中會(huì)遇到確實(shí)需要獲取隱藏元素的寬高,這兒所說(shuō)的隱藏元素是display為none的元素。
可使用jQuery Actual Plugin插件來(lái)完成,其源碼如下:
;( 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; } }); }));
使用實(shí)例:
//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});
插件地址:http://dreamerslab.com/works
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/89769.html
摘要:概述在世界這本書(shū)中有一些黑魔法給列舉出來(lái),在結(jié)合自己的理解。篇幅有點(diǎn)長(zhǎng),希望大家能夠堅(jiān)持看完,一定會(huì)有收獲以下是摘自每章內(nèi)一些重要的概念與技巧。 概述 在《css世界》這本書(shū)中有一些黑魔法給列舉出來(lái),在結(jié)合自己的理解。篇幅有點(diǎn)長(zhǎng),希望大家能夠堅(jiān)持看完,一定會(huì)有收獲!!!以下是摘自每章內(nèi)一些重要的概念與技巧。其中有解決圖片間隙的問(wèn)題、小圖標(biāo)與文字居中問(wèn)題等; ps: 特別是 line-h...
摘要:概述在世界這本書(shū)中有一些黑魔法給列舉出來(lái),在結(jié)合自己的理解。篇幅有點(diǎn)長(zhǎng),希望大家能夠堅(jiān)持看完,一定會(huì)有收獲以下是摘自每章內(nèi)一些重要的概念與技巧。 概述 在《css世界》這本書(shū)中有一些黑魔法給列舉出來(lái),在結(jié)合自己的理解。篇幅有點(diǎn)長(zhǎng),希望大家能夠堅(jiān)持看完,一定會(huì)有收獲!!!以下是摘自每章內(nèi)一些重要的概念與技巧。其中有解決圖片間隙的問(wèn)題、小圖標(biāo)與文字居中問(wèn)題等; ps: 特別是 line-h...
摘要:當(dāng)元素在容器中被滾動(dòng)超過(guò)指定的偏移值時(shí),元素在容器內(nèi)固定在指定位置。詳見(jiàn)浮動(dòng)與清除浮動(dòng)浮動(dòng)相關(guān)知識(shí)屬性的取值元素向左浮動(dòng)。是相對(duì)長(zhǎng)度單位,相對(duì)于當(dāng)前對(duì)象內(nèi)文本的字體尺寸。 這些個(gè)知識(shí)點(diǎn)是我個(gè)人認(rèn)為的,下面我們就來(lái)看看這些個(gè)知識(shí)點(diǎn)。 1.怎么讓一個(gè)不定寬高的 DIV,垂直水平居中? 使用Flex 只需要在父盒子設(shè)置:display: flex; justify-content: cent...
閱讀 2761·2021-09-24 10:34
閱讀 1862·2021-09-22 10:02
閱讀 2253·2021-09-09 09:33
閱讀 1459·2021-08-13 15:02
閱讀 3271·2020-12-03 17:10
閱讀 1180·2019-08-30 15:44
閱讀 2144·2019-08-30 12:58
閱讀 3229·2019-08-26 13:40