jQuery Check if Vertical Scroll is Present

Share this article

Simple jQuery code snippet to return true or false to check if the main window vertical scrollbar is present. Useful for firing off an event when a user scrolls to the bottom of a page such as showing related pages.

//checks if vertical scroll is present
//works in FF8 also
verticalScrollPresent: function()
{
  return (document.documentElement.scrollHeight !== document.documentElement.clientHeight);
}
//a longer winded version of the above
verticalScrollPresent: function()
{
  //return (document.documentElement.scrollHeight !== document.documentElement.clientHeight);

  // Get the computed style of the body element
  var cStyle = document.body.currentStyle||window.getComputedStyle(document.body, "");

  // Check the overflow and overflowY properties for "auto" and "visible" values
  hasVScroll = cStyle.overflow == "visible"
               || cStyle.overflowY == "visible"
               || (hasVScroll && cStyle.overflow == "auto")
               || (hasVScroll && cStyle.overflowY == "auto");

  return !hasVScroll;
}

Frequently Asked Questions (FAQs) about jQuery Vertical Scroll Check

How can I determine if a scrollbar is visible using jQuery?

To determine if a scrollbar is visible using jQuery, you can use the scrollHeight and clientHeight properties of an element. If the scrollHeight (the total height of the content, including the part hidden due to overflow) is greater than the clientHeight (the visible height of the content), it means a scrollbar is present. Here’s a simple code snippet to illustrate this:

var element = document.getElementById('yourElementId');
if (element.scrollHeight > element.clientHeight) {
// Scrollbar is present.
} else {
// Scrollbar is not present.
}

How can I create a vertically scrolling div in jQuery?

To create a vertically scrolling div in jQuery, you need to set the CSS overflow property of the div to ‘auto’ or ‘scroll’. This will add a scrollbar to the div when the content overflows its height. Here’s a simple example:

$('#yourDivId').css('overflow', 'auto');

How can I check if a div has overflowing elements using jQuery?

To check if a div has overflowing elements using jQuery, you can compare the scrollHeight of the div with its clientHeight. If the scrollHeight is greater than the clientHeight, it means the div has overflowing elements. Here’s a simple code snippet to illustrate this:

var div = document.getElementById('yourDivId');
if (div.scrollHeight > div.clientHeight) {
// The div has overflowing elements.
} else {
// The div does not have overflowing elements.
}

How can I use the scrollTop method in jQuery?

The scrollTop method in jQuery is used to get or set the vertical scrollbar position for an element. When called with no arguments, scrollTop returns the current vertical position of the scrollbar. When called with a number argument, scrollTop sets the vertical position of the scrollbar. Here’s an example:

// Get the current vertical position of the scrollbar.
var position = $('#yourElementId').scrollTop();

// Set the vertical position of the scrollbar.
$('#yourElementId').scrollTop(100);

How can I determine if the content of HTML elements overflows or not using jQuery?

To determine if the content of HTML elements overflows or not using jQuery, you can compare the scrollHeight of the element with its clientHeight. If the scrollHeight is greater than the clientHeight, it means the content overflows. Here’s a simple code snippet to illustrate this:

var element = document.getElementById('yourElementId');
if (element.scrollHeight > element.clientHeight) {
// The content overflows.
} else {
// The content does not overflow.
}

How can I scroll to the top of a page using jQuery?

To scroll to the top of a page using jQuery, you can use the animate method along with the scrollTop method. Here’s an example:

$('html, body').animate({ scrollTop: 0 }, 'slow');

How can I scroll to a specific element using jQuery?

To scroll to a specific element using jQuery, you can use the animate method along with the offset method. Here’s an example:

$('html, body').animate({
scrollTop: $('#yourElementId').offset().top
}, 'slow');

How can I detect a scroll event using jQuery?

To detect a scroll event using jQuery, you can use the scroll method. This method attaches a function to run when a scroll event occurs on the element. Here’s an example:

$(window).scroll(function() {
// Code to run when a scroll event occurs.
});

How can I hide the scrollbar using jQuery?

To hide the scrollbar using jQuery, you can set the CSS overflow property of the element to ‘hidden’. This will hide the scrollbar but the content will still be scrollable if it overflows. Here’s an example:

$('#yourElementId').css('overflow', 'hidden');

How can I make a div scrollable using jQuery?

To make a div scrollable using jQuery, you can set the CSS overflow property of the div to ‘auto’ or ‘scroll’. This will add a scrollbar to the div when the content overflows its height. Here’s a simple example:

$('#yourDivId').css('overflow', 'auto');

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week