Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read scrollHeight of div with a display:none parent

Tags:

html

jquery

With a div element in a parent div that is hidden with display:none.

  • I'm dumping the jQuery textarea element to the console. I see that the scrollHeight property of the 0th element is 88.
  • I try to read this property to a var (using $(element)[0].scrollHeight or $(element).prop('scrollHeight') and I'm getting 0.

I also tried to set the textarea to position: absolute and display: block with jQuery, before the read, with the same result.

How can I read the property correctly?

like image 710
RolandG Avatar asked Jan 09 '13 08:01

RolandG


People also ask

How do I get the scroll height of a div?

To get the height of the scroll bar the offsetHeight of div is subtracted from the clientHeight of div. OffsetHeight = Height of an element + Scrollbar Height. ClientHeight = Height of an element. Height of scrollbar = offsetHeight – clientHeight.

How is scrollHeight calculated?

scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar. The height is measured in the same way as clientHeight: it includes the element's padding, but not its border, margin or horizontal scrollbar.

Why is clientHeight and scrollHeight the same?

If the element's content can fit without a need for vertical scrollbar, its scrollHeight is equal to clientHeight.

What is scrollHeight property?

The scrollHeight property returns the height of an element including padding, but excluding borders, scrollbars, or margins. The scrollHeight property returns the height in pixels.


2 Answers

What is the size of an element when display:none? 0px by 0px

The element hasn't been rendered or painted anywhere on the screen, and so it truly occupies zero pixels worth of space. The question of if it was visible, how much space it would consume is tricky to answer, because answering it accurately means turning the hypothetical into a reality. We would need to actually render it to get a reliable measure of its size.

Generally speaking, this involves either:

  1. Toggling - Quickly making the element visible, checking the height, and restore back

    When toggling, you might be able to make visible and undo in between paint cycles, but there's no guarantee. Especially if the element you're looking to measure is deeply nested inside a hidden parent.

  2. Cloning - Emulate the element's properties as best you can & rendering off screen

    When cloning, an element's dimensions are based on a large number of other elements and properties surrounding it. Depending on the complexity of your page, there's also no guarantee that creating an element offscreen will yield the same exact dimensions that the original would.

There are several solutions laid out in the following questions, but most follow that basic pattern:

  • Need to find height of hidden div set to display:none
  • Get the size of an element when inside a display:none parent
  • Check if element is visible in DOM

There's also a library called jQuery.Actual (2.4KB) which abstracts away some of that work:

Here's their own Demo Page and also a Stack Snippet:

console.log("width: ", $("#inner").actual("width"))
console.log("height: ", $("#inner").actual("height"))
#outer, #inner {
  margin: 10px;
  padding: 10px;
}
#outer {
  background: #d1e3ef;
}
#inner {
  background: #9ebae9;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

<div id="outer" style="display:none">
  <code>#outer</code>
  <div id="inner">
    <code>#inner</code>
  </div>
</div>

Example

like image 87
KyleMit Avatar answered Oct 17 '22 01:10

KyleMit


You need to set dispaly:block property for parent div. And after getting scrollheight by using $(element)[0].scrollHeight you can set display:none to main div.

So your code will be

$('#mainDiv').show();
// Dump html to div
// Read height
var heightOfDiv = $('#mainDiv')[0].scrollHeight
// Hide the div
$('#mainDiv').hide();
like image 3
mujaffars Avatar answered Oct 16 '22 23:10

mujaffars