Skip to content

Instantly share code, notes, and snippets.

@snahor
Last active December 10, 2015 14:08
Show Gist options
  • Save snahor/4446092 to your computer and use it in GitHub Desktop.
Save snahor/4446092 to your computer and use it in GitHub Desktop.
Get scrollbars dimensions. Taken from http://stackoverflow.com/a/8221501/94746
function getScrollBarDimensions() {
var elm = document.documentElement.offsetHeight ? document.documentElement : document.body,
curX = elm.clientWidth,
curY = elm.clientHeight,
hasScrollX = elm.scrollWidth > curX,
hasScrollY = elm.scrollHeight > curY,
prev = elm.style.overflow,
r = { vertical: 0, horizontal: 0 };
if ( !hasScrollY && !hasScrollX )
return r;
elm.style.overflow = "hidden";
if ( hasScrollY )
r.vertical = elm.clientWidth - curX;
if ( hasScrollX )
r.horizontal = elm.clientHeight - curY;
elm.style.overflow = prev;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment