Sometimes, we want to get the browser’s scrollbar sizes with JavaScript.
In this article, we’ll look at how to get the browser’s scrollbar sizes with JavaScript.
How to get the browser’s scrollbar sizes with JavaScript?
To get the browser’s scrollbar sizes with JavaScript, we can subtract the window’s width by the html element’s width.
For instance, we write
const scrollBarWidth =
window.innerWidth - document.getElementsByTagName("html")[0].clientWidth;
to get the window’s width with window.innerWidth
.
And then we subtract that by the html element’s width which we get by
document.getElementsByTagName("html")[0].clientWidth
to get the scrollbar’s width.
Conclusion
To get the browser’s scrollbar sizes with JavaScript, we can subtract the window’s width by the html element’s width.