How can I get the browser's scrollbar sizes?

后端 未结 23 2215
孤街浪徒
孤街浪徒 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:25

    function getWindowScrollBarHeight() {
        let bodyStyle = window.getComputedStyle(document.body);
        let fullHeight = document.body.scrollHeight;
        let contentsHeight = document.body.getBoundingClientRect().height;
        let marginTop = parseInt(bodyStyle.getPropertyValue('margin-top'), 10);
        let marginBottom = parseInt(bodyStyle.getPropertyValue('margin-bottom'), 10);
        return fullHeight - contentHeight - marginTop - marginBottom;
      }
    

提交回复
热议问题