Categories
JavaScript Answers

How to Detect if a Page has a Vertical Scrollbar with JavaScript?

Spread the love

We can use the document.body.scrollHeight to get the full height of the content, including the content off the screen.

document.body.clientHeight has the height of the content on the screen.

Therefore, we can check if the page has a vertical scrollbar by comparing the 2.

To do this, we write:

const hasVScroll = document.body.scrollHeight > document.body.clientHeight;  
console.log(hasVScroll)

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 *