Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .height() problem with chrome

I've been using jQuery Height function. But there was a problem in getting the correct height of my div element. This div element's height is set to auto, which means the div's height depends only on the elements inside of it. When I tried using the .height() in my page, I got the following result:

Chrome: 1276 px
Firefox: 1424 px

But when you see them both they have equal height. The only thing is that the height() function returns different result.

Here's the code:

<div class="single-mid" style="position:relative;width:700px;margin:-1px 0 0 1px;padding-right:56px;">
    <div>Sample Inside Element</div>
</div>

<script type="text/javascript">
$(document).ready(function(){
     var less_height = $('.single-mid').height() -10;
     $('.single-mid').height(less_height);
});
</script>

But I tried to set the div's height into 1424px. Both browser return the same result.

Any ideas? Thanks in advance.

like image 771
Trez Avatar asked Aug 19 '10 03:08

Trez


People also ask

What is jQuery height?

In jQuery, height method is used to get the height of any element in HTML. The height method sets and returns the height of the HTML elements. Method 1: The height() method returns the first matched element's height, But the height(value) method sets all matched elements height.

How does jQuery calculate window height?

height() method is recommended when an element's height needs to be used in a mathematical calculation. This method is also able to find the height of the window and document. $( document ). height();

What is height in Javascript?

The height property sets or returns the height of an element. The height property has effect only on block-level elements or on elements with absolute or fixed position. The overflowing content can be manipulated with the overflow property. Tip: Use the width property to set or return the width of an element.


1 Answers

Had the same issue. But if I did a brief setTimeout delay before doing the hight check, chrome started reporting the same height as firefox. It seems chrome triggers the document ready state before fully determining how the content will alter the actual size of the container...!? In any case, my fix was to change my:

$(document).ready( ...

to a:

$(window).load( ...

which doesn't trigger until "all sub-elements have been completely loaded" (from http://api.jquery.com/load-event/).

like image 72
Kory Avatar answered Oct 13 '22 10:10

Kory