javascript How to get dynamically height (when i r

2019-08-20 17:06发布

问题:

I'm trying to get height from adjustable div because I'd like to add overflow style into css when height value will be greater than a set value. When you resize window height of this the div change. I'll explain this with some pictures:

The main problem is how to get it dynamically? I mean,when you resize your own window etc.?

回答1:

In case you are able to use jQuery, I would suggest using the window.onresize method and compute the height of the div.

$(document).ready(function()
{
   $(window).on("resize", function()
   {
        $("#myDiv").text("My height is " + $("#myDiv").height());
   });                                                           
});​

Take a look at this fiddle I created for you: http://jsfiddle.net/9ftGe/

I hope this is what you wanted.



回答2:

I think you're looking for monitoring the browser variables...

window.innerHeight

window.innerWidth

Whenever you check/call these variables you should get the exact window inner size and take it from there.

NB: These variables (constants to us) are case sensitive.