div scrollbar width

后端 未结 4 1461
醉酒成梦
醉酒成梦 2020-12-29 15:49

Is there an easy way to get the width of a scrollbar using javascript / jquery ? I need to get the width of a div that overflows + whatever width the scroll bar is.

4条回答
  •  离开以前
    2020-12-29 16:23

    Here is a plain javascript version that is way faster.

    function getScrollbarWidth() {
      var div, body, W = window.browserScrollbarWidth;
      if (W === undefined) {
        body = document.body, div = document.createElement('div');
        div.innerHTML = '
    '; div = div.firstChild; body.appendChild(div); W = window.browserScrollbarWidth = div.offsetWidth - div.clientWidth; body.removeChild(div); } return W; };

提交回复
热议问题