Determining when an element is scrollable in Gecko
16 October, 2013 § 1 Comment
As a web developer, there are often times where it’s necessary to know if an element on a page is scrollable. One way of checking this would be to find the difference of element.scrollWidth
and element.clientWidth
. If the difference between these two properties is non-zero, then the element is scrollable. However, this doesn’t work for all cases.
In particular, element.scrollWidth
and element.clientWidth
clamp their values to integers. If the difference between the scrollWidth
and clientWidth
is less than zero, then the computed difference between the two will result in zero. This is less than good.
Starting in Firefox 16 [1], there is a new property element.scrollLeftMax
which returns the difference of scrollWidth
and clientWidth
, including the fractional component. Also introduced is the companion element.scrollTopMax
for use in determining vertical scrolling availability.
Hopefully these properties will find their way in to the other layout engines.
[1] These properties were implemented in bug 766937.
scrollLeft/TopMax also solves other problems with scrollWidth/Height and clientWidth/Height not working in all cases, e.g. bug 914251.