Skip to content

Instantly share code, notes, and snippets.

@smashercosmo
Created November 27, 2013 13:15
Show Gist options
  • Save smashercosmo/7675442 to your computer and use it in GitHub Desktop.
Save smashercosmo/7675442 to your computer and use it in GitHub Desktop.
Snippet to determine scrollbar width
/**
* Simple function to determine scrollbar width.
* Extracted from JqueryUI source and changed to get rid
* of jQuery dependency.
*/
function getScrollBarWidth() {
var w1, w2,
div = document.createElement('div'),
inner = document.createElement('div');
div.style = { position: 'absolute', width: '50px', height: '50px', overflow: 'hidden' };
inner.style = { height: '100px' };
div.appendChild(inner);
document.body.append(div);
w1 = inner.offsetWidth;
div.style.overflow = 'scroll';
w2 = inner.offsetWidth;
if ( w1 === w2 ) w2 = div.clientWidth;
div.parentNode.removeChild(div);
return w1 - w2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment