How can I get the browser's scrollbar sizes?

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

    Using jQuery, you can shorten Matthew Vines answer to:

    function getScrollBarWidth () {
        var $outer = $('
    ').css({visibility: 'hidden', width: 100, overflow: 'scroll'}).appendTo('body'), widthWithScroll = $('
    ').css({width: '100%'}).appendTo($outer).outerWidth(); $outer.remove(); return 100 - widthWithScroll; };

提交回复
热议问题