js 判斷是否有滾動條以及獲取滾動條寬度的方法


document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight);

一般情況下,使用 document.body.scrollHeight > window.innerHeight 就可以判斷。

但是在 IE7,IE8 中 window.innerHeight 為 underfined,所以為了兼容 IE7、IE8,需要使用 document.documentElement.clientHeight 屬性計算窗口高度。

計算滾動條寬度的方法

function getScrollbarWidth() {

    var scrollDiv = document.createElement("div");
    scrollDiv.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
    document.body.appendChild(scrollDiv);
    var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
    document.body.removeChild(scrollDiv);

    return scrollbarWidth;

}




參考:https://www.cnblogs.com/nzbin/p/8117535.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM