Skip to content

Instantly share code, notes, and snippets.

@Lukewh
Created October 24, 2012 15:04
Show Gist options
  • Select an option

  • Save Lukewh/3946615 to your computer and use it in GitHub Desktop.

Select an option

Save Lukewh/3946615 to your computer and use it in GitHub Desktop.
Get the width / height of the browsers scroll bar
function getScrollWidth(){
// Create a div
var scrollEle = document.createElement("div"),
scrollWidth = 0;
// Set it's styling
scrollEle.style.width = "100px";
scrollEle.style.height = "100px";
scrollEle.style.overflow = "scroll";
scrollEle.style.position = "absolute";
scrollEle.style.top = "-9999px";
// Append element to the page body
document.body.appendChild(scrollEle);
// Get the width
scrollWidth = scrollEle.offsetWidth - scrollEle.clientWidth;
// Remove the Element
document.body.removeChild(scrollEle);
return scrollWidth;
}
document.write(getScrollWidth());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment