Skip to content

Instantly share code, notes, and snippets.

@skhilko
Last active August 29, 2015 14:08
Show Gist options
  • Save skhilko/0a2b95c3588bf4abd2f8 to your computer and use it in GitHub Desktop.
Save skhilko/0a2b95c3588bf4abd2f8 to your computer and use it in GitHub Desktop.
Detect scrollbar width with JavaScript. Based on a technique by David Walsh http://davidwalsh.name/detect-scrollbar-width
var scrollWidth = (function() {
var scrollDiv = document.createElement("div");
scrollDiv.className = "scrollbar-measure";
document.body.appendChild(scrollDiv);
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
return scrollbarWidth;
})();
.scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment