Skip to content

Instantly share code, notes, and snippets.

@kocoten1992
Created November 7, 2016 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kocoten1992/c7b7775ec705a8db96cf1902d91bf049 to your computer and use it in GitHub Desktop.
Save kocoten1992/c7b7775ec705a8db96cf1902d91bf049 to your computer and use it in GitHub Desktop.
getScrollbarWidth
function getScrollbarWidth() {
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);
return widthNoScroll - widthWithScroll;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment