Categories
JavaScript Answers

How to detect if a page has a vertical scrollbar with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *