我發現了一個解決方案,我認爲增加了什麼是對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