無法獲取隱藏元素寬度和高度的解決方案
更新時間:2017年03月07日 10:01:38 作者:風雨后見彩虹
本文主要介紹了無法獲取隱藏元素(display:none)寬度(width)和高度(height)的解決方案,具有很好的參考價值。下面跟著小編一起來看下吧
在實際開發(fā)中會遇到確實需要獲取隱藏元素的寬高,這兒所說的隱藏元素是display為none的元素。
可使用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);
當然如果要支持模塊化開發(fā),直接使用官網(wǎng)下載的文件即可,源碼也貼上:
;( 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});
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
如何用jQuery實現(xiàn)ASP.NET GridView折疊伸展效果
我們今天就一個具體的需求進行分析,引出如何用jQuery實現(xiàn)ASP.NET GridView折疊伸展效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09在jquery中的ajax方法怎樣通過JSONP進行遠程調用
這一節(jié)主要演示下在JQUERY中的ajax方法怎樣通過JSONP進行遠程調用,需要的朋友可以參考下2014-04-04jquery實現(xiàn)Li滾動時滾動條自動添加樣式的方法
這篇文章主要介紹了jquery實現(xiàn)Li滾動時滾動條自動添加樣式的方法,實例分析了jquery響應鼠標事件及動態(tài)添加樣式的相關技巧,需要的朋友可以參考下2015-08-08jQuery插件boxScroll實現(xiàn)圖片輪播特效
本文給大家分享的是使用jQuery插件Boxscroll來實現(xiàn)簡單的圖片輪播特效的代碼,非常簡單實用,有需要的小伙伴可以參考下。2015-07-07JQuery select控件的相關操作實現(xiàn)代碼
JQuery獲取和設置Select選項方法匯總如下,需要的朋友可以參考下2012-09-09