我发现了一个解决方案,我认为增加了什么是对SO对此“问题”。
(function ($) {
$.fn.getHiddenDimensions = function (includeMargin) {
var $item = this,
props = { position: 'absolute', visibility: 'hidden', display: 'block' },
dim = { width: 0, height: 0, innerWidth: 0, innerHeight: 0, outerWidth: 0, outerHeight: 0 },
$hiddenParents = $item.parents().andSelf().not(':visible'),
includeMargin = (includeMargin == null) ? false : includeMargin;
var oldProps = [];
$hiddenParents.each(function() {
var old = {};
for (var name in props) {
old[name] = this.style[name];
this.style[name] = props[name];
}
oldProps.push(old);
});
dim.width = $item.width();
dim.outerWidth = $item.outerWidth(includeMargin);
dim.innerWidth = $item.innerWidth();
dim.height = $item.height();
dim.innerHeight = $item.innerHeight();
dim.outerHeight = $item.outerHeight(includeMargin);
$hiddenParents.each(function (i) {
var old = oldProps[i];
for (var name in props) {
this.style[name] = old[name];
}
});
return dim;
}
} (jQuery));
来源:http://www.foliotek.com/devblog/getting-the-width-of-a-hidden-element-with-jquery-using-width/
http://jsfiddle.net/mKQx9/1/ – Paulpro
嗯,伤心。不幸的是,现实并非如此简单,因为投入深入树木,其中许多父母也可以“显示:无”。不管怎么说,还是要谢谢你! – ajbeaven
@ajbeaven嗨,我更新了我的解决方案。它可能需要一些调整才能使其跨浏览器,但我认为它是相当健壮的。 – Paulpro