Stackoverflow: Getting scroll bar width using JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function getScrollbarWidth() { var outer = document.createElement(“div”); outer.style.visibility = “hidden”; outer.style.width = “100px”; 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); return widthNoScroll – widthWithScroll; } document.write(“Scrollbar width is: “+getScrollbarWidth()+“px”); |
Result:
WinXP: 16px;
Win7: 17px;
Win8: 17px;
Mac: 15px;