Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the height of horizontal scrollbar

Tags:

I encountered a problem where I need to know of the height of horizontal scrollbar.

This Q&A suggests that you should use clientHeight property and calculate difference. Unfortunately this does not work anymore as is evident here https://jsfiddle.net/fn8naww8/

So how can I get the height of scrollbar?

EDIT: OSX does not differentiate between offsetHeight and clientHeight.

html:

<div id="wrapper">
  <div id="content"></div>
</div>

css:

#wrapper{
  height:100px;
  width:100%;
  overflow-x:auto;
}
#content{
  height:100%;
  width:200%;
  background:linear-gradient(to right, red , yellow);
}
like image 552
sanjihan Avatar asked Apr 28 '18 11:04

sanjihan


People also ask

How do I customize my horizontal scroll bar?

Scrollbar Selectors For webkit browsers, you can use the following pseudo elements to customize the browser's scrollbar: ::-webkit-scrollbar the scrollbar. ::-webkit-scrollbar-button the buttons on the scrollbar (arrows pointing upwards and downwards). ::-webkit-scrollbar-thumb the draggable scrolling handle.

What is scroll height?

The scroll height refers to the entire height of that element in the HTML page, even if it is not viewable due to overflow property. The element's scroll height includes the height assigned in CSS property plus the padding, but it does not include the margin, border, or scrollbar.

How do I resize the horizontal scroll bar in CSS?

This can be done using a customisation of the end and start scrollbar track pieces. Effectively, you make the start and end piece invisible and set a large margin on it, and it will reduce the width of the available scrollbar to the size you want.

How do you style a horizontal scroll bar?

For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.


1 Answers

Try with:

var horizontalScrollbarHeight = wrapper.offsetHeight - wrapper.clientHeight; 

or like:

var horizontalScrollbarHeight = wrapper.offsetHeight - parseInt(window.getComputedStyle(wrapper, null).getPropertyValue("height"), 10); 

Both will return ~17 if the scrollbar size was not altered by CSS like by using ::-webkit-scrollbar

https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

like image 62
Roko C. Buljan Avatar answered Oct 13 '22 01:10

Roko C. Buljan