Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Detect if element height is bigger than window height and do something about it

The tittle really says it all.

Basically i want to detect if this div's height is bigger than window height and do something about it..

I have done this but i cant get it to work http://jsfiddle.net/dhkCa/3 Why wont it work?

Edit: Fixed a little error in the css code. Jsfiddle link updated.

like image 537
Joonas Avatar asked Aug 31 '11 16:08

Joonas


1 Answers

The document's contains all the elements within itself, and its height is a sum of the heights of all those elements (all the display:block elements anyway, plus margin and padding); therefore no contained element can be taller than the document itself. What you need to do is compare the window's height, not the document's:

var div = $("div").height();
var win = $(window).height();

if (div > win ) {
    $("div").addClass('red');
}

JS Fiddle demo.

like image 121
David Thomas Avatar answered Sep 28 '22 05:09

David Thomas