Created
January 22, 2015 16:48
-
-
Save sunify/09d4744d3c11e4468a8a to your computer and use it in GitHub Desktop.
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
function getScrollbarWidth() { | |
if(width !== undefined) { | |
return width; | |
} | |
var outer = document.createElement("div"); | |
outer.style.visibility = "hidden"; | |
outer.style.width = "100px"; | |
outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps | |
document.body.appendChild(outer); | |
var widthNoScroll = outer.offsetWidth; | |
// force scrollbars | |
outer.style.overflow = "scroll"; | |
// add innerdiv | |
var inner = document.createElement("div"); | |
inner.style.width = "100%"; | |
outer.appendChild(inner); | |
var widthWithScroll = inner.offsetWidth; | |
// remove divs | |
outer.parentNode.removeChild(outer); | |
width = widthNoScroll - widthWithScroll; | |
return width; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment