How do you get the rendered height of an element?

前端 未结 18 2317
情书的邮戳
情书的邮戳 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

    Have you set the height in the css specifically? If you haven't you need to use offsetHeight; rather than height

    var h = document.getElementById('someDiv').style.offsetHeight;
    
    0 讨论(0)
  • 2020-11-22 03:44
    style = window.getComputedStyle(your_element);
    

    then simply: style.height

    0 讨论(0)
  • 2020-11-22 03:44

    I think the best way to do this in 2020 is to use plain js and getBoundingClientRect().height;

    Here's an example

    let div = document.querySelector('div');
    let divHeight = div.getBoundingClientRect().height;
    
    console.log(`Div Height: ${divHeight}`);
    <div>
      How high am I?                                                                     
    0 讨论(0)
  • 2020-11-22 03:45
    document.querySelector('.project_list_div').offsetHeight;
    
    0 讨论(0)
  • 2020-11-22 03:45

    So is this the answer?

    "If you need to calculate something but not show it, set the element to visibility:hidden and position:absolute, add it to the DOM tree, get the offsetHeight, and remove it. (That's what the prototype library does behind the lines last time I checked)."

    I have the same problem on a number of elements. There is no jQuery or Prototype to be used on the site but I'm all in favor of borrowing the technique if it works. As an example of some things that failed to work, followed by what did, I have the following code:

    // Layout Height Get
    function fnElementHeightMaxGet(DoScroll, DoBase, elementPassed, elementHeightDefault)
    {
        var DoOffset = true;
        if (!elementPassed) { return 0; }
        if (!elementPassed.style) { return 0; }
        var thisHeight = 0;
        var heightBase = parseInt(elementPassed.style.height);
        var heightOffset = parseInt(elementPassed.offsetHeight);
        var heightScroll = parseInt(elementPassed.scrollHeight);
        var heightClient = parseInt(elementPassed.clientHeight);
        var heightNode = 0;
        var heightRects = 0;
        //
        if (DoBase) {
            if (heightBase > thisHeight) { thisHeight = heightBase; }
        }
        if (DoOffset) {
            if (heightOffset > thisHeight) { thisHeight = heightOffset; }
        }
        if (DoScroll) {
            if (heightScroll > thisHeight) { thisHeight = heightScroll; }
        }
        //
        if (thisHeight == 0) { thisHeight = heightClient; }
        //
        if (thisHeight == 0) { 
            // Dom Add:
            // all else failed so use the protype approach...
            var elBodyTempContainer = document.getElementById('BodyTempContainer');
            elBodyTempContainer.appendChild(elementPassed);
            heightNode = elBodyTempContainer.childNodes[0].offsetHeight;
            elBodyTempContainer.removeChild(elementPassed);
            if (heightNode > thisHeight) { thisHeight = heightNode; }
            //
            // Bounding Rect:
            // Or this approach...
            var clientRects = elementPassed.getClientRects();
            heightRects = clientRects.height;
            if (heightRects > thisHeight) { thisHeight = heightRects; }
        }
        //
        // Default height not appropriate here
        // if (thisHeight == 0) { thisHeight = elementHeightDefault; }
        if (thisHeight > 3000) {
            // ERROR
            thisHeight = 3000;
        }
        return thisHeight;
    }
    

    which basically tries anything and everything only to get a zero result. ClientHeight with no affect. With the problem elements I typically get NaN in the Base and zero in the Offset and Scroll heights. I then tried the Add DOM solution and clientRects to see if it works here.

    29 Jun 2011, I did indeed update the code to try both adding to DOM and clientHeight with better results than I expected.

    1) clientHeight was also 0.

    2) Dom actually gave me a height which was great.

    3) ClientRects returns a result almost identical to the DOM technique.

    Because the elements added are fluid in nature, when they are added to an otherwise empty DOM Temp element they are rendered according to the width of that container. This get weird, because that is 30px shorter than it eventually ends up.

    I added a few snapshots to illustrate how the height is calculated differently. Menu block rendered normally Menu block added to DOM Temp element

    The height differences are obvious. I could certainly add absolute positioning and hidden but I am sure that will have no effect. I continued to be convinced this would not work!

    (I digress further) The height comes out (renders) lower than the true rendered height. This could be addressed by setting the width of the DOM Temp element to match the existing parent and could be done fairly accurately in theory. I also do not know what would result from removing them and adding them back into their existing location. As they arrived through an innerHTML technique I will be looking using this different approach.

    * HOWEVER * None of that was necessary. In fact it worked as advertised and returned the correct height!!!

    When I was able to get the menus visible again amazingly DOM had returned the correct height per the fluid layout at the top of the page (279px). The above code also uses getClientRects which return 280px.

    This is illustrated in the following snapshot (taken from Chrome once working.)
    enter image description here

    Now I have noooooo idea why that prototype trick works, but it seems to. Alternatively, getClientRects also works.

    I suspect the cause of all this trouble with these particular elements was the use of innerHTML instead of appendChild, but that is pure speculation at this point.

    0 讨论(0)
  • 2020-11-22 03:46

    NON JQUERY since there were a bunch of links using elem.style.height in the top of these answers...

    INNER HEIGHT:
    https://developer.mozilla.org/en-US/docs/Web/API/Element.clientHeight

    document.getElementById(id_attribute_value).clientHeight;
    

    OUTER HEIGHT:
    https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.offsetHeight

    document.getElementById(id_attribute_value).offsetHeight; 
    

    Or one of my favorite references: http://youmightnotneedjquery.com/

    0 讨论(0)
提交回复
热议问题