Skip to content

Instantly share code, notes, and snippets.

@SubZane
Last active August 17, 2016 11:29
Show Gist options
  • Save SubZane/d05345f02c209ce168f261aac36e9d77 to your computer and use it in GitHub Desktop.
Save SubZane/d05345f02c209ce168f261aac36e9d77 to your computer and use it in GitHub Desktop.
detects the width of the scrollbar. Different depending on OS and browser.
function detectScrollbarWidth() {
var outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.width = '100px';
outer.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps
document.body.appendChild(outer);
var widthNoScroll = outer.offsetWidth;
// force scrollbars
outer.style.overflow = 'scroll';
// add innerdiv
var inner = document.createElement('div');
inner.style.width = '100%';
outer.appendChild(inner);
var widthWithScroll = inner.offsetWidth;
// remove divs
outer.parentNode.removeChild(outer);
scrollbarWidth = widthNoScroll - widthWithScroll;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment