jQuery无法获取隐藏元素的宽高

用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢工了。


  用Chrome自带的开发工具一查,发现罢工的Tab中。小插件一些重要元素的宽度都变成“0”了,因为这个小插件需要计算动态宽度来实现,于是马上想到是小插件中的宽度获取失败了,果不其然。


  汗,居然一直没发现jQuery无法获取隐藏元素(display:none)的宽度(width)和高度(height),为了兼容IE6,我用1.x版,而且是官方最新的1.10.2版,不知道在2.x版中这个问题有没有解决。


  既然jQuery都不支持,那么Javascript也就肯定不支持了,网上搜索了一下,有个解决方案是用visibility:hidden来代替display:none,由于visibility:hidden占用物理空间,因此可以获取宽高。


  问题是这需要我去改动已经写好的Tab插件,这种拆东墙补西墙的事情,总会让人感觉不爽的。长时间搜索其它解决方案无果,就在我快要妥协的时候,突然眼前一亮,发现了个好东西:jQuery Actual


  可以说它几乎完美的解决了这个问题,而且使用方法极其简单,使用$('#someElement').actual('width')的方式来代替$('#someElement').width()就可以了,兼容性也十分出色,可以兼容IE6浏览器,压缩后体积只有1.1K也是一大亮点,更牛的是还支持inner和outer的计算。


官方信息


jQuery Actual 官网
jQuery Actual 演示
jQuery Actual 文档
jQuery Actual 下载




jQuery版本要求
jQuery 1.2.3+


所兼容的浏览器




Firefox 2.0+
Internet Explorer 6+
Safari 3+
Opera 10.6+
Chrome 8+






安装方法
HTML文档需要声明DOCTYPE
引用JS文件即可
<script type="text/javascript" src="path/jquery.min.js"></script>
<script type="text/javascript" src="path/jquery.actual.js"></script>
使用方法
代码范例
//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官方应该考虑将这个功能写进内核,那就更方便了。为了防止以后jQuery Actual的官网打不开(现在就时不时会和谐)或者下载不了,在这里存一份jquery.actual.js的源码,以备不时只需。


源码:jquery.actual.js


;( 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
     };


     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: block !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 (){
           var $this = $( this );


           // Save original style. If no style was set, attr() returns undefined
           tmp.push( $this.attr( 'style' ));
           $this.attr( '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 );

猜你喜欢

转载自blog.csdn.net/q290057637/article/details/80268824