Last active
February 4, 2016 18:40
-
-
Save radist2s/92d0256f7856ada2e52a to your computer and use it in GitHub Desktop.
Scrollbar width detect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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