Need to find height of hidden div on page (set to display:none)

前端 未结 13 1549
萌比男神i
萌比男神i 2020-11-27 06:00

I need to measure the offsetHeight of a div that is inside of a hidden element.

相关标签:
13条回答
  • 2020-11-27 06:58

    A workaround is to set the height to 0

    .hidden { 
      height: 0; 
      overflow: hidden; 
    }
    

    Then to get the elements scrollHeight.

    document.querySelector('.hidden').scrollHeight
    

    The scrollHeight will correctly give you the height though the element does not appear. I don't think it affects element flow either.

    Example: https://jsfiddle.net/de3vk8p4/7/
    Reference: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements#How_big_is_the_content.3F

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