jQuery Check if Vertical Scroll is Present

By Sam Deering

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;

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.