How can I get the browser's scrollbar sizes?

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

    With jquery (only tested in firefox):

    function getScrollBarHeight() {
        var jTest = $('


    '); $('body').append(jTest); var h = jTest.innerHeight(); jTest.css({ overflow: 'auto', width: '200px' }); var h2 = jTest.innerHeight(); return h - h2; } function getScrollBarWidth() { var jTest = $('
    '); $('body').append(jTest); var w = jTest.innerWidth(); jTest.css({ overflow: 'auto', height: '200px' }); var w2 = jTest.innerWidth(); return w - w2; }

    But I actually like @Steve's answer better.

提交回复
热议问题