javascript 如何在javascript中获取浏览器滚动条高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24797160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:21:23  来源:igfitidea点击:

How can I get the browser scrollbar height in javascript

javascriptscrollbar

提问by user3828771

How can I get the browser scrollbar height? Does JS have a built-in function for this case? Somebody please help me out of this.

如何获取浏览器滚动条的高度?JS 有没有针对这种情况的内置函数?有人请帮我解决这个问题。

回答by techfoobar

There isn't a built-in method for this. However, the scrollbar's height is supposedto give an indication of how much of the available content fits within the available viewport. Going by that, we can determine it like:

没有为此提供内置方法。但是,滚动条的高度应该表示可用视口中适合多少可用内容。据此,我们可以确定它如下:

var sbHeight = window.innerHeight * (window.innerHeight / document.body.offsetHeight);

Where window.innerHeight / document.body.offsetHeightis the percentage of content visible currently. We multiple that with the available viewport height to get the approximate scrollbar height.

window.innerHeight / document.body.offsetHeight当前可见的内容百分比在哪里。我们将其与可用的视口高度相乘以获得近似的滚动条高度。

Note: Different browsers may add/reduce some pixels to/from the scrolbar height.

注意:不同的浏览器可能会在滚动条高度上增加/减少一些像素。

回答by Nick Tsai

I'm using the difference width between the window and the client:

我正在使用窗口和客户端之间的差异宽度:

function getScrollbarWidth() {
    return window.innerWidth - document.documentElement.clientWidth;
}

回答by alansiqueira27

Does this help?

这有帮助吗?

this.document.body.scrollHeight