jQuery – Using Slider as a Scrollbar
Last week I introduced the jQuery UI element known as a slider and indicated that this control could easily be used as a custom scrollbar.
Today I’m going to show you exactly how that’s done.

First we need to adjust our HTML. We need to add a content area to the HTML we had last week. I’m going to put this content area to the right of the scrollbar, but you could put it to the left by simply changing the CSS.
<div id="slider"></div>
<div id="scroller"><div id="content">
here is lots of text<br />
</div></div>
The CSS to position this all correctly looks like this:
#slider
{
height: 242px;
width: 13px;
margin:0px 10px 0px 10px;
float:left;
}
#scroller
{
width: 1159px;
height: 243px;
overflow:hidden;
}
#content
{
width: 215px;
}
Not much exciting here. Slider is basically the same as last week. The area we want to scroll is composed of two elements, a div we’ve given the ID "content" that is nested inside another div we’ve named "scroller." The scroller is a fixed height and set so that if the content is longer than the scroller, it will not be displayed.
The rest is simple jQuery magic:
$(function() {
$("#slider").slider(
{ change: handleChange,
slide: handleSlide,
min: -100,
max: 0 });
});
function handleChange(e, ui) {
var maxScroll = $("#scroller")
.attr("scrollHeight") -
$("#scroller").height();
$("#scroller")
.animate({ scrollTop: -ui.value *
(maxScroll / 100)
}, 1000);
}
function handleSlide(e, ui) {
var maxScroll = $("#scroller")
.attr("scrollHeight") -
$("#scroller").height();
$("#scroller")
.attr({ scrollTop: -ui.value
* (maxScroll / 100) });
}
At the top of our Javascript, we are simply wiring up our slider control. The most notable thing you’ll want to pay attention to here is that I’ve set the minimum height and maximum height so that zero is at the top of the slider. If you don’t do this, the slider will start with the thumb at the bottom. Not exactly what we have in mind.
We need to wire up two event handlers, slide and change, so that the content area will scroll as we move the thumb. We’ve done that in the document-ready function.
The two event handlers first determine the maximum scroll height and then position the content area within the scroller area. Again, to make this work correctly, we needed to negate the value that was passed in in the ui parameter.
Obviously, it would not take a whole lot of work to package this all up as a plugin that does all the work for you, but it helps to know how to do it if you need to.
Other post in jQuery
- jQuery - The Man, The Myth, The Legend - October 8th, 2008
- Getting started with jQuery and ASP.NET - October 15th, 2008
- jQuery - Explaining Last Week's Code - October 21st, 2008
- Friday Books - "Learning jQuery" - October 24th, 2008
- jQuery Simple Selectors - October 28th, 2008
- Friday Books - "jQuery in Action" - October 31st, 2008
- jQuery Selectors - Looks just like CSS - November 6th, 2008
- VS2008 SP1 Hotfix to Support "-vsdoc.js" IntelliSense Doc Files - November 11th, 2008
- jQuery Looks like XPath - November 12th, 2008
- jQuery - class manipulation - November 19th, 2008
- jQuery - Events - December 2nd, 2008
- Host jQuery at Google (with Intellisense support) - December 10th, 2008
- jQuery - Calling Your Own Functions - December 16th, 2008
- Friday Books - jQuery Reference Guide - December 19th, 2008
- jQuery - Creating Plug-ins - December 23rd, 2008
- jQuery - Loading Partial Content - December 30th, 2008
- jQuery - Positioning Elements - January 6th, 2009
- AjaxToolKit TabControl Disabled Tab - January 12th, 2009
- jQuery, JSON, and ASP.NET - January 15th, 2009
- Review of the MDC at NYC - January 21st, 2009
- jQuery - Retrieving HTML Fragments - January 22nd, 2009
- jQuery GUI - Drag - February 3rd, 2009
- jQuery - Drop - February 12th, 2009
- jQuery UI - Resizable w/ ASP.NET Themes - February 18th, 2009
- jQuery, bgiframe and IE6 z-order hacks - February 19th, 2009
- jQuery - Sliders (scrollbars to the rest of us) - March 4th, 2009
- jQuery - Using Slider as a Scrollbar - March 12th, 2009
- jQuery - Auto Scrolling the Slider - March 23rd, 2009
- Live Presentation of jQuery - March 23rd, 2009
- Just a Week Away! - April 7th, 2009
- jQuery Tabs - April 9th, 2009
- jQuery Demos From Last Tuesday’s Presentation - April 16th, 2009
- jQuery – Accordion - May 6th, 2009
- CustomValidationControl and jQuery - May 11th, 2009
- Mixing ASP.NET, jQuery and JSON - May 12th, 2009
- jQuery Progressbar - May 20th, 2009
- jQuery – Dialog - June 2nd, 2009
- jQuery – Modal Dialog - June 9th, 2009
- Does jQuery Make Us Lazy? - June 18th, 2009
- jQuery Dialog – With Validation Controls - June 25th, 2009
- jQuery – Date Picker - July 2nd, 2009
- jQuery Splitter - July 21st, 2009
- jQuery Expand/Collapse Using Head Tags - October 15th, 2009
- Do you Need My Help? - November 18th, 2009
- Flash to jQuery - November 30th, 2009
- JQuery, CuFon, and Dynamic Content - December 1st, 2009
- jQuery, Each() and Async Gets - December 2nd, 2009
- jQuery and ASP.NET UpdatePanel - January 6th, 2010
- jQuery 1.4 Released - January 15th, 2010
Other Related Items:
Drinkwell Aqua Garden for Pet FountainsHydroponic cat grass garden is a natural treat of 100% organic wheat grass. Aqua garden uses no soil or additives, just the continuously circulating w... Read More >
Eagle 2 Gallon Type 1 Gasoline Safety Can With Funnel #UI20FSSafety Plunger Bench Cans - Spill Control, Containment & Storage Type: Type I Safety Can Volume Capacity: 2 Gal. Certifications: UL, ULC, FM Material: Galvanized Steel Color: Red w/Yellow Height: 9-1/2
Resident Evil 4In Resident Evil 4 you'll know a new type of horror, as the classic survival-horror action returns with all-new characters, controls and storylines. W... Read More >
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!










Online demo is helpfull….
[...] this question is looking for the content to auto scroll as well. So, let’s pull up the code from last week and take a look at this [...]
[...] 2. jQuery – Using Slider as a Scrollbar [...]
Without an online demo, numpty “hacks” like me (who cut and paste code snippets without fully understanding the underlying code but who somehow manage to piece it together) aren’t even sure if this is the effect that we’re looking for.
Since you’re not selling anything, who am I to complain? But without a live demo, I’m lost!
Greg
It just so happens that I uploaded demos on Thursday. One of them has a demo of this code.
http://blog.dmbcllc.com/2009/04/16/jquery-demos-from-last-tuesdays-presentation/