获取页面隐藏元素的高宽
来源:互联网 发布:无3c被淘宝下架了宝贝 编辑:程序博客网 时间:2024/05/16 01:46
jquery中无法获取display:none的元素的高度和宽度,这时,只需要使用jquery.actual.js即可。
具体方法如下:
引入jquery.actual.js
<script src="jquery.js" type="text/javascript" ></script><script src="jquery.actual.min.js" type="text/javascript"></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.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 );
jquery.actual.min.js源码:
/*! Copyright 2012, Ben Lin (http://dreamerslab.com/) * Licensed under the MIT License (LICENSE.txt). * * Version: 1.0.18 * * Requires: jQuery >= 1.2.3 */(function(a){if(typeof define==="function"&&define.amd){define(["jquery"],a);}else{a(jQuery);}}(function(a){a.fn.addBack=a.fn.addBack||a.fn.andSelf;a.fn.extend({actual:function(b,l){if(!this[b]){throw'$.actual => The jQuery method "'+b+'" you called does not exist';}var f={absolute:false,clone:false,includeMargin:false,display:"block"};var i=a.extend(f,l);var e=this.eq(0);var h,j;if(i.clone===true){h=function(){var m="position: absolute !important; top: -1000 !important; ";e=e.clone().attr("style",m).appendTo("body");};j=function(){e.remove();};}else{var g=[];var d="";var c;h=function(){c=e.parents().addBack().filter(":hidden");d+="visibility: hidden !important; display: "+i.display+" !important; ";if(i.absolute===true){d+="position: absolute !important; ";}c.each(function(){var m=a(this);var n=m.attr("style");g.push(n);m.attr("style",n?n+";"+d:d);});};j=function(){c.each(function(m){var o=a(this);var n=g[m];if(n===undefined){o.removeAttr("style");}else{o.attr("style",n);}});};}h();var k=/(outer)/.test(b)?e[b](i.includeMargin):e[b]();j();return k;}});}));
原文引用自:http://blog.sina.com.cn/s/blog_8a9505840101fzsk.html
0 0
- 获取页面隐藏元素的高宽
- js及jquery获取页面和元素的宽高
- js及jquery获取页面和元素的宽高
- 获取元素的高宽
- 【jQuery】使用jQuery.Actual.js插件获取隐藏元素的高和宽
- 获取页面的宽高
- 获取隐藏元素的尺寸
- 获取隐藏元素的尺寸
- js怎么获取元素的宽高
- js如何获取元素的宽高
- 9元素宽高的获取
- 通过jquery获取元素宽高时,此元素必须明确定义了宽高或已经被追加到页面中,否则返回的宽高都是0
- 获取页面的高宽的参数
- 如何动态获取页面的宽高
- 获取元素的宽和高
- js获取元素相对于父元素的位移,获取元素本身的宽高
- 隐藏web页面上的某元素
- css隐藏页面元素的方法
- 剑指Offer_49_把字符串转换成整数
- 【bzoj1066】[SCOI2007]蜥蜴
- ajax/php/javascript实现分页
- Android优化策略
- PHP SPL简介
- 获取页面隐藏元素的高宽
- iOS备忘录之mac常用命令行
- Android内存的优化总结
- IO流一点整理
- 网易视频云:InnoDB透明页压缩与稀疏文件
- 158.N-Queens
- Ubuntu15.10上arm-linux-gcc4.3.2的安装、配置及问题解决方法
- ecshop二次开发--左侧导航
- 环环相扣的信用卡案情