Over a million developers have joined DZone.

Detect Whether a DOM Element Has a Visible Scrollbar

DZone's Guide to

Detect Whether a DOM Element Has a Visible Scrollbar

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

Webkit browsers, especially in Mac OSX, hide the scrollbar intelligently when not in use. This feature improves the look and feel of the browser. But for the designers, sometimes this can be a pain because they may need to alter the design based on the visibility of the scrollbar.

Here are two simple jQuery plugins that can detect the visibility of the scrollbar for a given DOM element:

For Horizontal Scrollbars

(function($) {
	$.fn.hasHorizontalScrollBar = function() {
		return this.get(0) ? this.get(0).scrollWidth > this.innerWidth() : false;

For Vertical Scrollbars

(function($) {
	$.fn.hasVerticalScrollBar = function() {
		return this.get(0) ? this.get(0).scrollHeight > this.innerheight() : false;

You can use it like this:

	//do whatever you'd like to

Source and idea: this StackOverflow discussion. :)

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}