user3828771
user3828771

Reputation: 1663

How can I get the browser scrollbar height in javascript

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.

Upvotes: 15

Views: 37283

Answers (3)

Irfan Ahmad
Irfan Ahmad

Reputation: 61

'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)

Upvotes: 5

alansiqueira27
alansiqueira27

Reputation: 8506

Does this help?

this.document.body.scrollHeight

Upvotes: -5

techfoobar
techfoobar

Reputation: 66663

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.

Upvotes: 16

Related Questions