Skip to content

Instantly share code, notes, and snippets.

@askd
Last active November 22, 2017 23:58
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 askd/b39d893ed6a5fcfaf1b726c0c3059b1d to your computer and use it in GitHub Desktop.
Save askd/b39d893ed6a5fcfaf1b726c0c3059b1d to your computer and use it in GitHub Desktop.
Calculate Scrollbar Width
function calcScrollbarWidth() {
const outerDiv = document.createElement('div');
const innerDiv = document.createElement('div');
outerDiv.style.overflow = 'scroll';
document.body.insertBefore(outerDiv, null);
outerDiv.insertBefore(innerDiv, null);
window.scrollbarWidth = outerDiv.offsetWidth - innerDiv.offsetWidth;
outerDiv.removeChild(innerDiv);
document.body.removeChild(outerDiv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment