[英]get the height of horizontal scrollbar
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. 该问答建议您应该使用
clientHeight
属性并计算差值。 Unfortunately this does not work anymore as is evident here https://jsfiddle.net/fn8naww8/ 不幸的是,这不再起作用了,在这里https://jsfiddle.net/fn8naww8/
So how can I get the height of scrollbar? 那么如何获得滚动条的高度?
EDIT : OSX does not differentiate between offsetHeight and clientHeight. 编辑 :OSX不区分offsetHeight和clientHeight。
html: HTML:
<div id="wrapper">
<div id="content"></div>
</div>
css: CSS:
#wrapper{
height:100px;
width:100%;
overflow-x:auto;
}
#content{
height:100%;
width:200%;
background:linear-gradient(to right, red , yellow);
}
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
双方将返回
~17
,如果滚动条的大小不是由CSS通过改变像::-webkit-scrollbar
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
console.log(window.getComputedStyle(document.querySelector("#wrapper")).height); console.log(window.getComputedStyle(document.querySelector("#content")).height);
*{ box-sizing: border-box; } #wrapper{ height:100px; width:100%; overflow-x:auto; } #content{ height:100%; width:200%; background:linear-gradient(to right, red , yellow); }
<div id="wrapper"> <div id="content"></div> </div>
This will return 100px for wrapper and 83px for inner. 包装器返回100px,内部返回83px。
There is a magic number: “17px”. 有一个魔术数字:“ 17px”。 Seems this height/width does not change even if you resize the browser window.
即使您调整浏览器窗口的大小,此高度/宽度似乎也不会改变。 And it works on Chrome fine with my test.
经过我的测试,它可以在Chrome上正常运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.