Get the div height in pixels, although its height is set to 100%

I wonder if there is a way to get the div height in pixels, although its height is set earlier than 100% of the height.
This is necessary because the div content is dynamic, so the height of the div has different values ​​based on the content itself.

[edit] Div is hidden by default.

Do I need to get div height in pixels for later manipulations (smooth scrolling will be done for div)?
Is there any way to do this?

+4
source share
5 answers

Since you marked jQuery, use

$("#myElement").height(); 

http://api.jquery.com/height/

For Plain Ol 'Javascript, you can use element.clientHeight or element.offsetHeight , depending on which one suits your needs.


Since the div is hidden, you will need to .show() it before calling .height() , and you can immediately hide it:
 var $myEl = $('#myElement').show(); var height = $myEl.height(); $myEl.hide(); 
+10
source
 theDiv.clientHeight 
+5
source

You can use height ()

 $("#divInQuestion").height(); 
+1
source

You can use the function . height () :

 $('#divid').height() 
+1
source

Well in slow browsers, the show / hide method MAY cause a window to flicker (although the computer should be very slow). Therefore, if you want to avoid this, give the div opacity: 0 - and possibly even a position: absolute, so it does not push the content. Therefore, to extend the code to:

 var $myEl = $('#myElement').css({"opacity": "0", "position": "absolute"}).show(); var height = $myEl.height(); $myEl.hide().css({"opacity": "", "position": ""}); 

But again. This may be redundant.

0
source

Source: https://habr.com/ru/post/1313641/


All Articles