How can I get the browser's scrollbar sizes?

后端 未结 23 2220
孤街浪徒
孤街浪徒 2020-11-22 06:08

How can I determine the height of a horizontal scrollbar, or the width of a vertical one, in JavaScript?

23条回答
  •  既然无缘
    2020-11-22 06:16

    detectScrollbarWidthHeight: function() {
        var div = document.createElement("div");
        div.style.overflow = "scroll";
        div.style.visibility = "hidden";
        div.style.position = 'absolute';
        div.style.width = '100px';
        div.style.height = '100px';
        document.body.appendChild(div);
    
        return {
            width: div.offsetWidth - div.clientWidth,
            height: div.offsetHeight - div.clientHeight
        };
    },
    

    Tested in Chrome, FF, IE8, IE11.

提交回复
热议问题