Sometimes, we want to detect if a page has a vertical scrollbar with JavaScript.
In this article, we’ll look at how to detect if a page has a vertical scrollbar with JavaScript.
How to detect if a page has a vertical scrollbar with JavaScript?
To detect if a page has a vertical scrollbar with JavaScript, we check if the scrollHeight
of the element is bigger than its clientHeight
.
For instance, we write
const hasVScroll = document.body.scrollHeight > document.body.clientHeight;
to check if the body element’s scrollHeight
is bigger than its clientHeight
to check if the body element has a vertical scrollbar.
Conclusion
To detect if a page has a vertical scrollbar with JavaScript, we check if the scrollHeight
of the element is bigger than its clientHeight
.