方法1:
function getScrollbarHeight()
{
var spacer = document.createElement("div");
spacer.setAttribute("style", "position: fixed; margin: 0px; padding: 0px; border: none; visibility: hidden; top: 0px; left: 0px; width: 1px; height: 100%; z-index: -1;");
document.body.appendChild(spacer);
var sbHeight = window.innerHeight - spacer.offsetHeight;
document.body.removeChild(spacer);
return sbHeight > 0 && sbHeight < 40 ? sbHeight : 0;
}
方法2:
function getScrollbarWidth()
{
var spacer = document.createElement("div");
spacer.style.cssText = 'position:absolute;top:-999px;width:100px;height:100px;overflow:scroll;';
document.body.appendChild(spacer);
var sbW = spacer.offsetWidth - spacer.clientWidth;
document.body.removeChild(spacer);
return sbW;
}