Reputation: 1663
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
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
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