Skip to main content

jQuery Check if Vertical Scroll is Present

By Sam Deering
JavaScript
Share:

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;
}

Tags

jQuery

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.

Integromat Tower Ad