其实只需要一行 JS 就可以,测试兼容 IE7
function hasScrollbar() {
return document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight);
}
一般情况下,使用 document.body.scrollHeight > window.innerHeight 就可以判断。但是在 IE7,IE8 中 window.innerHeight 为 underfined,所以为了兼容 IE7、IE8,需要使用 document.documentElement.clientHeight 属性计算窗口高度。