How can I get the browser's scrollbar sizes?

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

    This life-hack decision will give you opportunity to find browser scrollY width (vanilla JavaScript). Using this example you can get scrollY width on any element including those elements that shouldn't have to have scroll according to your current design conception,:

    getComputedScrollYWidth     (el)  {
    
      let displayCSSValue  ; // CSS value
      let overflowYCSSValue; // CSS value
    
      // SAVE current original STYLES values
      {
        displayCSSValue   = el.style.display;
        overflowYCSSValue = el.style.overflowY;
      }
    
      // SET TEMPORALLY styles values
      {
        el.style.display   = 'block';
        el.style.overflowY = 'scroll';
      }
    
      // SAVE SCROLL WIDTH of the current browser.
      const scrollWidth = el.offsetWidth - el.clientWidth;
    
      // REPLACE temporally STYLES values by original
      {
        el.style.display   = displayCSSValue;
        el.style.overflowY = overflowYCSSValue;
      }
    
      return scrollWidth;
    }
    
    0 讨论(0)
  • 2020-11-22 06:13

    This is a great answer: https://stackoverflow.com/a/986977/5914609

    However in my case it did not work. And i spent hours searching for the solution.
    Finally i've returned to above code and added !important to each style. And it worked.
    I can not add comments below the original answer. So here is the fix:

    function getScrollBarWidth () {
      var inner = document.createElement('p');
      inner.style.width = "100% !important";
      inner.style.height = "200px !important";
    
      var outer = document.createElement('div');
      outer.style.position = "absolute !important";
      outer.style.top = "0px !important";
      outer.style.left = "0px !important";
      outer.style.visibility = "hidden !important";
      outer.style.width = "200px !important";
      outer.style.height = "150px !important";
      outer.style.overflow = "hidden !important";
      outer.appendChild (inner);
    
      document.body.appendChild (outer);
      var w1 = inner.offsetWidth;
      outer.style.overflow = 'scroll !important';
      var w2 = inner.offsetWidth;
      if (w1 == w2) w2 = outer.clientWidth;
    
      document.body.removeChild (outer);
    
      return (w1 - w2);
    };
    
    0 讨论(0)
  • 2020-11-22 06:13

    Already coded in my library so here it is:

    var vScrollWidth = window.screen.width - window.document.documentElement.clientWidth;
    

    I should mention that jQuery $(window).width() can also be used instead of window.document.documentElement.clientWidth.

    It doesn't work if you open developer tools in firefox on the right but it overcomes it if the devs window is opened at bottom!

    window.screen is supported quirksmode.org!

    Have fun!

    0 讨论(0)
  • 2020-11-22 06:14

    Here's the more concise and easy to read solution based on offset width difference:

    function getScrollbarWidth(): number {
    
      // Creating invisible container
      const outer = document.createElement('div');
      outer.style.visibility = 'hidden';
      outer.style.overflow = 'scroll'; // forcing scrollbar to appear
      outer.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps
      document.body.appendChild(outer);
    
      // Creating inner element and placing it in the container
      const inner = document.createElement('div');
      outer.appendChild(inner);
    
      // Calculating difference between container's full width and the child width
      const scrollbarWidth = (outer.offsetWidth - inner.offsetWidth);
    
      // Removing temporary elements from the DOM
      outer.parentNode.removeChild(outer);
    
      return scrollbarWidth;
    
    }
    

    See the JSFiddle.

    0 讨论(0)
  • 2020-11-22 06:15

    From Alexandre Gomes Blog I have not tried it. Let me know if it works for you.

    function getScrollBarWidth () {
      var inner = document.createElement('p');
      inner.style.width = "100%";
      inner.style.height = "200px";
    
      var outer = document.createElement('div');
      outer.style.position = "absolute";
      outer.style.top = "0px";
      outer.style.left = "0px";
      outer.style.visibility = "hidden";
      outer.style.width = "200px";
      outer.style.height = "150px";
      outer.style.overflow = "hidden";
      outer.appendChild (inner);
    
      document.body.appendChild (outer);
      var w1 = inner.offsetWidth;
      outer.style.overflow = 'scroll';
      var w2 = inner.offsetWidth;
      if (w1 == w2) w2 = outer.clientWidth;
    
      document.body.removeChild (outer);
    
      return (w1 - w2);
    };
    
    0 讨论(0)
  • 2020-11-22 06:15

    For me, the most useful way was

    (window.innerWidth - document.getElementsByTagName('html')[0].clientWidth)
    

    with vanilla JavaScript.

    0 讨论(0)
提交回复
热议问题