How do you get the rendered height of an element?

前端 未结 18 2318
情书的邮戳
情书的邮戳 2020-11-22 03:11

How do you get the rendered height of an element?

Let\'s say you have a

element with some content inside. This content inside is going to st
18条回答
  •  长情又很酷
    2020-11-22 03:42

    Sometimes offsetHeight will return zero because the element you've created has not been rendered in the Dom yet. I wrote this function for such circumstances:

    function getHeight(element)
    {
        var e = element.cloneNode(true);
        e.style.visibility = "hidden";
        document.body.appendChild(e);
        var height = e.offsetHeight + 0;
        document.body.removeChild(e);
        e.style.visibility = "visible";
        return height;
    }
    

提交回复
热议问题