How do you get the rendered height of an element?

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

    Try one of:

    var h = document.getElementById('someDiv').clientHeight;
    var h = document.getElementById('someDiv').offsetHeight;
    var h = document.getElementById('someDiv').scrollHeight;
    

    clientHeight includes the height and vertical padding.

    offsetHeight includes the height, vertical padding, and vertical borders.

    scrollHeight includes the height of the contained document (would be greater than just height in case of scrolling), vertical padding, and vertical borders.

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

    If you are using jQuery already, your best bet is .outerHeight() or .height(), as has been stated.

    Without jQuery, you can check the box-sizing in use and add up various paddings + borders + clientHeight, or you can use getComputedStyle:

    var h = getComputedStyle(document.getElementById('someDiv')).height;

    h will now be a string like a "53.825px".

    And I can't find the reference, but I think I heard getComputedStyle() can be expensive, so it's probably not something you want to call on each window.onscroll event (but then, neither is jQuery's height()).

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

    offsetHeight, usually.

    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 scenes last time I checked).

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

    With MooTools:

    $('someDiv').getSize().y

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

    You can use .outerHeight() for this purpose.

    It will give you full rendered height of the element. Also, you don't need to set any css-height of the element. For precaution you can keep its height auto so it can be rendered as per content's height.

    //if you need height of div excluding margin/padding/border
    $('#someDiv').height();
    
    //if you need height of div with padding but without border + margin
    $('#someDiv').innerHeight();
    
    // if you need height of div including padding and border
    $('#someDiv').outerHeight();
    
    //and at last for including border + margin + padding, can use
    $('#someDiv').outerHeight(true);
    

    For a clear view of these function you can go for jQuery's site or a detailed post here.

    it will clear the difference between .height() / innerHeight() / outerHeight()

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

    It should just be

    $('#someDiv').height();
    

    with jQuery. This retrieves the height of the first item in the wrapped set as a number.

    Trying to use

    .style.height
    

    only works if you have set the property in the first place. Not very useful!

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