Created
October 6, 2017 08:03
-
-
Save zry656565/2e9462e0776f46f6e7d0116287f04fef to your computer and use it in GitHub Desktop.
getScrollBarSize
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
// From: https://github.com/react-component/util | |
let cached; | |
export default function getScrollBarSize(fresh) { | |
if (fresh || cached === undefined) { | |
const inner = document.createElement('div'); | |
inner.style.width = '100%'; | |
inner.style.height = '200px'; | |
const outer = document.createElement('div'); | |
const outerStyle = outer.style; | |
outerStyle.position = 'absolute'; | |
outerStyle.top = 0; | |
outerStyle.left = 0; | |
outerStyle.pointerEvents = 'none'; | |
outerStyle.visibility = 'hidden'; | |
outerStyle.width = '200px'; | |
outerStyle.height = '150px'; | |
outerStyle.overflow = 'hidden'; | |
outer.appendChild(inner); | |
document.body.appendChild(outer); | |
const widthContained = inner.offsetWidth; | |
outer.style.overflow = 'scroll'; | |
let widthScroll = inner.offsetWidth; | |
if (widthContained === widthScroll) { | |
widthScroll = outer.clientWidth; | |
} | |
document.body.removeChild(outer); | |
cached = widthContained - widthScroll; | |
} | |
return cached; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment