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

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

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

13条回答
  •  萌比男神i
    2020-11-27 06:36

    Until the element is rendered, it has no height. Even if you clone the parent object and display it somewhere that can't be seen by the user, there's not guarantee that the clone will have the same height as the final size of the hidden object.

    There are many things that can affect the height that wouldn't necessarily be rendered in the clone - anything in the DOM and its interaction with the CSS rules could cause a change in rendering any other element of the DOM. Short of cloning the entire document (and even that's not fool-proof) you have no way of determining the height of the hidden object.

    If you must know the height before it's displayed to the user, you'll have to "hack" it by displaying it for as short of a time as possible then hiding it again. Most likely, the user will see this hiccup and not be pleased by the result.

提交回复
热议问题