我确定答案是否定的,但是在将元素添加到DOM之前确定元素的宽度是可能的吗?
一旦添加了元素,我知道可以使用offsetWidth和offsetHeight。
谢谢
我确定答案是否定的,但是在将元素添加到DOM之前确定元素的宽度是可能的吗?
一旦添加了元素,我知道可以使用offsetWidth和offsetHeight。
谢谢
关键在于展示元素 (display:block),同时也隐藏它 (visibility:hidden),并将其位置设为绝对定位,以便不影响页面布局。
正如 Oscar 提到的那样,MooTools 的 Element.Measure 类可以实现这一点。
Oscar提到的Mootools Element.Measure功能非常棒。对于使用jQuery的人来说,这里有一个快速插件可以实现相同的功能:
$.fn.measure = (fn)->
el = $(this).clone(false)
el.css
visibility: 'hidden'
position: 'absolute'
el.appendTo('body')
result = fn.apply(el)
el.remove()
return result
你可以这样调用它,确保返回该值(感谢 Sam Fen 指出!):width = $('.my-class-name').measure( function(){ return this.width() } )
稍微修改了一下代码。这是一个纯JS解决方案:
function measure(el, fn) {
var pV = el.style.visibility,
pP = el.style.position;
el.style.visibility = 'hidden';
el.style.position = 'absolute';
document.body.appendChild(el);
var result = fn(el);
el.parentNode.removeChild(el);
el.style.visibility = pV;
el.style.position = pP;
return result;
}
var div = document.createElement('div');
div.innerHTML = "<p>Hello</p><br/>";
alert(div.offsetHeight); // 0
alert(measure(div, function(el){return el.offsetHeight})); // 68
至少目前来说是不可能的,因为样式会影响这些属性,而它放置的位置决定了它的样式以及哪些规则会影响它。
例如,在页面中放置一个<p></p>
元素,默认情况下如果将其作为子元素添加到body中,则其宽度将与body一致,但如果将其添加到一个<div style="width: 100px;"></div>
中,那么你就会看到它很快就会改变大小。
/**
* Get bounding client rect for an element (not exists at current DOM tree)
* @param {!HTMLElement} el
* @return {!Promise<!ClientRect>}
*/
function getElementRect(el) {
return new Promise(resolve => {
const element = el.cloneNode(true);
element.style.visibility = "hidden";
element.style.position = "absolute";
document.body.appendChild(element);
resolve(element.getBoundingClientRect());
element.remove();
});
}
const div = /** @type {!HTMLElement} */ (document.createElement("div"));
div.innerHTML = "<p>Hello</p><br/>";
// Execute
(async () => {
const rect = await getElementRect(div);
console.log(rect.width);
})();
function getSizeOfNonDomElement(domElement)
{
// setup
let div = document.createElement("div");
div.style.position = "absolute";
div.style.visibility = "hidden";
div.style.display = "block";
div.appendChild (domElement);
document.body.appendChild (div);
// run
let rect = domElement.getBoundingClientRect ();
// cleanup
document.body.removeChild (div);
div.removeChild (domElement);
return rect;
}
body
中是不够的。如果你需要获取元素的实际宽度,在某些情况下这种方法行不通,不要忘记考虑元素或其子元素的 __内边距__。 - Mehdi Dehghani