I am interested to know if there is any way i can check from javascript code if an html element has overflow in it's content (but with the overflow property set to visible - so no scrollbars on the element). Can i check irrespective of scrollbars for overflow content (in a div tag for instance)? Thanks in advance.
views:
1445answers:
3
+1
A:
Try comparing element.scrollHeight / element.scrollWidth to element.offsetHeight / element.offsetWidth
http://developer.mozilla.org/en/DOM/element.offsetWidth
http://developer.mozilla.org/en/DOM/element.offsetHeight
http://developer.mozilla.org/en/DOM/element.scrollWidth
http://developer.mozilla.org/en/DOM/element.scrollHeight
Chris MacDonald
2008-09-27 15:37:20
+18
A:
Normally, you can compare the client[Height|Width]
with scroll[Height|Width]
in order to detect this... but they'll be the same when overflow is visible. So a detection routine must account for this:
// Determines if the passed element is overflowing its bounds,
// either vertically or horizontally.
// Will temporarily modify the "overflow" style to detect this
// if necessary.
function checkOverflow(el)
{
var curOverflow = el.style.overflow;
if ( !curOverflow || curOverflow === "visible" )
el.style.overflow = "hidden";
var isOverflowing = el.clientWidth < el.scrollWidth
|| el.clientHeight < el.scrollHeight;
el.style.overflow = curOverflow;
return isOverflowing;
}
Tested in FF3, IE6, Chrome 0.2.149.30.
Shog9
2008-09-27 15:55:03
very thorough, nice answer
Chris MacDonald
2008-09-27 16:19:33
thank you Shog9... the detection routine is, i think, what i needed, because i play with overflow (hidden/visible)
2008-09-27 17:49:18
I have a similar question over at http://stackoverflow.com/questions/2023787/how-to-determine-if-hidden-overflow-text-is-at-top-or-bottom-of-element where I am trying to figure out what parts of the containing element have hidden overflow.
slolife
2010-01-07 21:27:41