After routing around many other questions I have not found an answer that fixes my problem.
I am writing a script to find out whether the div is overflowing. But when trying to retrieve the visible height with jQuery.height()
, jQuery.innerHeight()
or JavaScripts offsetHeight
. I am given the value of the whole div
(Including the part which is overflowing) i.e: the same value as scrollHeight.
The containing DIVs style:
{
overflow-x: hidden;
overflow-y: auto;
width: 73%;
bottom: 0px;
float: left;
height: 100%;
top: 0px;
}
I've created a mock up of the scenario on jsFiddle: http://jsfiddle.net/Lukedturnbull/L2bxmszv/3/ (Make sure to make the preview screen smaller to create the scroll bar)
Everything seems fine,
jQuery.height()
andjQuery.innerHeight()
has nothing to do with the overflow property. They will return heights, not just the visible part.If you want to know the content height you have to use
scrollHeight
. ThisscrollHeight
is a regular javascript property you don't have to use jQueryOr you can use jQuery selector
See the working jsfiddle: http://jsfiddle.net/scgz7an5/1/
Notice that
returns undefined.
UPDATE
You forgot the most important part of floating elements. You forgot to clear them.
Take a look at this jsfiddle, is an edit of yours but with floating elements cleared. There you see different values for
scrollHeight
andjQuery.height()
. See that.structureContent
is the one that has the scroll bar, not.content
neither.width100
..structureContent
hasoverflow:auto
and the scrollbar you see comes from it.http://jsfiddle.net/L2bxmszv/5/
I added this class to clear your floating elements.
The output was this:
See a great article about floating elements and clearing them here: http://css-tricks.com/all-about-floats/
The scrollbar you are seeing is actually on the
.structureContent
element and not on.content
. This is why.content
returns all the same value..content
isn't truncated.I have now found a solution for my problem, although I do not fully understand why it is doing this.
This isn't HTML and code I have written and I was simply writing a fix to see if the scroll bar appears. But i found that getting the ScrollHeight and Height of the parent of the container solved my problem. Comparing to see if the scrollHeight is greater than the height allowed me to solve the issue.