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.
Asked
Active
Viewed 3.3k times
14
-
Please see this answer http://stackoverflow.com/questions/986937/how-can-i-get-the-browsers-scrollbar-sizes – varu Jul 17 '14 at 07:19
-
@varu This is about scrollbar width. – user3828771 Jul 17 '14 at 07:22
-
Look at this answer http://stackoverflow.com/questions/13382516/getting-scroll-bar-width-using-javascript/22510143#22510143 – Gromo Jul 18 '14 at 06:28
3 Answers
16
There isn't a built-in method for this. However, the scrollbar's height is supposed to 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.offsetHeight
is the percentage of content visible currently. We multiple that with the available viewport height to get the approximate scrollbar height.
Note: Different browsers may add/reduce some pixels to/from the scrolbar height.

techfoobar
- 65,616
- 14
- 114
- 135
-
2I suppose there is a mistake here. For example, I've got a window with innerHeight = 500 and body.offsetHeight = 2000 so I get scrollbar's height = 500 * (500 / 2000) = 125 px. And if body.offsetHeight = 100 than I get scrollbar's height = 2500. It is too much for a scrollbar, isn't it? – SergeyT Sep 07 '18 at 13:08
-
4
'window.pageYOffset'
returns the current height of the scrollbar concerning the full height of the document. A simple example of usage is like
alert('Current scroll from the top: ' + window.pageYOffset)

Irfan Ahmad
- 51
- 3
-
Tested your code it shows "driver is not defined", can please check your code ? – phoenixstudio Jan 28 '21 at 14:17