Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active February 4, 2016 18:40
Show Gist options
  • Save radist2s/92d0256f7856ada2e52a to your computer and use it in GitHub Desktop.
Save radist2s/92d0256f7856ada2e52a to your computer and use it in GitHub Desktop.
Scrollbar width detect
var scrollBarWidth
window.getScrollBarWidth = function getScrollBarWidth() {
if (scrollBarWidth !== undefined) {
return scrollBarWidth
}
var scrollDetector = document.createElement('div')
scrollDetector.style.width = '50px'
scrollDetector.style.height = '50px'
scrollDetector.style.overflow = 'visible'
scrollDetector.style.position = 'absolute'
scrollDetector.style.visibility = 'hidden'
document.body.appendChild(scrollDetector)
var widthWithoutScrollBar = scrollDetector.clientWidth
scrollDetector.style.overflow = 'scroll'
var widthWithScrollBar = scrollDetector.clientWidth
document.body.removeChild(scrollDetector)
return scrollBarWidth = widthWithoutScrollBar - widthWithScrollBar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment