How can I get the browser's scrollbar sizes?

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

    This is only script I've found, which is working in webkit browsers ... :)

    $.scrollbarWidth = function() {
      var parent, child, width;
    
      if(width===undefined) {
        parent = $('
    ').appendTo('body'); child=parent.children(); width=child.innerWidth()-child.height(99).innerWidth(); parent.remove(); } return width; };

    Minimized version:

    $.scrollbarWidth=function(){var a,b,c;if(c===undefined){a=$('
    ').appendTo('body');b=a.children();c=b.innerWidth()-b.height(99).innerWidth();a.remove()}return c};

    And you have to call it when document is ready ... so

    $(function(){ console.log($.scrollbarWidth()); });
    

    Tested 2012-03-28 on Windows 7 in latest FF, Chrome, IE & Safari and 100% working.

    source: http://benalman.com/projects/jquery-misc-plugins/#scrollbarwidth

提交回复
热议问题