Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Last active November 19, 2015 08:59
Show Gist options
  • Select an option

  • Save Alex1990/af3486075974fa2f76b5 to your computer and use it in GitHub Desktop.

Select an option

Save Alex1990/af3486075974fa2f76b5 to your computer and use it in GitHub Desktop.
Get the scrollbar width.
/**
* Get the scorllbar's width.
* Note: This script only works on the default zoom level.
* Ref: https://github.com/twbs/bootstrap/blob/94b4076dd2efba9af71f0b18d4ee4b163aa9e0dd/js/modal.js#L277-L284
*/
function scrollbarWidth() {
var scrollDiv = document.createElement('div');
scrollDiv.style.cssText = 'position:absolute;left:-9999px;width:50px;height:50px;overflow:scroll;';
document.body.appendChild(scrollDiv);
var width = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment