在实际开发中会遇到确实需要获取隐藏元素的宽高,这儿所说的隐藏元素是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);
当然如果要支持模块化开发,直接使用官网下载的文件即可,源码也贴上:
;( 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});
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
# jq获取隐藏元素宽度
# 获取隐藏元素的高度
# 获取隐藏元素的宽度
# javascript获取隐藏元素(display:none)的高度和宽度的方法
# js获取浏览器高度 窗口高度 元素尺寸 偏移属性的方法
# js获取Html元素的实际宽度高度的方法
# js获取页面及个元素高度、宽度的代码
# jquery如何获取元素的滚动条高度等实现代码
# jQuery获取页面及个元素高度、宽度的总结——超实用
# 一个JavaScript获取元素当前高度的实例
# 原生JS获取元素集合的子元素宽度实例
# 贴上
# 来完成
# 中会
# 官网
# 在实际
# style
# true
# restore
# position
# top
# important
# block
# props
# appendTo
# configs
# fix
# eq
# target
# hidden
# tmp
相关文章:
如何用西部建站助手快速创建专业网站?
建站之星如何实现网站加密操作?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
广州商城建站系统开发成本与周期如何控制?
如何用PHP快速搭建CMS系统?
公司门户网站制作流程,华为官网怎么做?
如何在景安云服务器上绑定域名并配置虚拟主机?
如何用VPS主机快速搭建个人网站?
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?
如何通过免费商城建站系统源码自定义网站主题与功能?
如何选择CMS系统实现快速建站与SEO优化?
建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略
h5在线制作网站电脑版下载,h5网页制作软件?
,制作一个手机app网站要多少钱?
网站制作话术技巧,网站推广做的好怎么话术?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
山东云建站价格为何差异显著?
如何选择域名并搭建高效网站?
建站之星安装需要哪些步骤及注意事项?
建站之星后台管理系统如何操作?
合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?
高防服务器租用首荐平台,企业级优惠套餐快速部署
潍坊网站制作公司有哪些,潍坊哪家招聘网站好?
建站之星安全性能如何?防护体系能否抵御黑客入侵?
已有域名和空间如何搭建网站?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
如何在Golang中处理模块冲突_解决依赖版本不兼容问题
网站制作公司,橙子建站是合法的吗?
如何在IIS中新建站点并配置端口与IP地址?
中山网站制作网页,中山新生登记系统登记流程?
成都响应式网站开发,dw怎么把手机适应页面变成网页?
如何在Ubuntu系统下快速搭建WordPress个人网站?
贸易公司网站制作流程,出口贸易网站设计怎么做?
如何破解联通资金短缺导致的基站建设难题?
如何注册花生壳免费域名并搭建个人网站?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
定制建站是什么?如何实现个性化需求?
定制建站方案优化指南:企业官网开发与建站费用解析
建站ABC备案流程中有哪些关键注意事项?
免费公司网站制作软件,如何申请免费主页空间做自己的网站?
建站之星如何快速更换网站模板?
如何获取PHP WAP自助建站系统源码?
教学论文网站制作软件有哪些,写论文用什么软件
?
建站之星下载版如何获取与安装?
建站主机功能解析:服务器选择与快速搭建指南
网站制作和推广的区别,想自己建立一个网站做推广,有什么快捷方法马上做好一个网站?
网站制作模板下载什么软件,ppt模板免费下载网站?
测试制作网站有哪些,测试性取向的权威测试或者网站?
制作网站外包平台,自动化接单网站有哪些?
*请认真填写需求信息,我们会在24小时内与您取得联系。