How can I get the browser's scrollbar sizes?

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

    The way Antiscroll.js does it in it's code is:

    function scrollbarSize () {
      var div = $(
          '
    ' + '
    ' ); $('body').append(div); var w1 = $(div).innerWidth(); var w2 = $('div', div).innerWidth(); $(div).remove(); return w1 - w2; };

    The code is from here: https://github.com/LearnBoost/antiscroll/blob/master/antiscroll.js#L447

提交回复
热议问题