New Revised CSS Library Forums Web Tools
FAQs Awards Usage Terms Contact
Categories
Other Sections
Sweet Ads
Compatibility
Bookmark online:

FF3+ IE7+ Opera8+

jQuery Custom Scrollbar

Author: Dynamic Drive

Description:  jQuery custom scrollbar script lets you replace the default scrollbar of overflowing content with a custom image based one instead. Both horizontal and vertical scrollbars are supported. The script once initialized also exposes several public methods for dynamically scrolling to particular points within the content. With an image as the scrollbar interface, the sky's the limit as far as how it could look!

Demo: Look below!

Down 20px Up 20px Top Bottom To Element

Authors such as Charles Dickens, Kurt Vonnegut, Jr., Nathaniel Hawthorne, Virginia Woolf, Bolesław Prus, Dino Buzzati, Rudyard Kipling, William Faulkner, F. Scott Fitzgerald, James Joyce, P. G. Wodehouse, H. P. Lovecraft and Ernest Hemingway were highly accomplished writers of both short stories and novels. Short stories have their face in oral story-telling traditions and the prose anecdote, a swiftly sketched situation that quickly comes to its point. With the rise of the comparatively realistic novel, the short story evolved as a miniature version, with some of its first perfectly independent examples in the tales of E. T. A. Hoffmann. Other 19th-century writers well known for their short stories include Nikolai Gogol, Guy de Maupassant, and Bolesław Prus. Some authors are known almost entirely for their short stories, either by choice (they wrote nothing else) or by critical regard (short-story writing is thought of as a challenging art). An example is Jorge Luis Borges, who won American fame with "The Garden of Forking Paths", published in the August 1948 Ellery Queen's Mystery Magazine. Another example is O. Henry (author of "Gift of the Magi"), for whom the O. Henry Award is named. American examples include Flannery O'Connor, John Cheever, and Raymond Carver. Short stories have often been adapted for half-hour and hour radio dramas, as on NBC Presents: Short Story (1951–52). The art of storytelling is doubtlessly older than record of civilization. Even the so-called modern short story, which was the latest of the major literary types to evolve, has an ancient lineage. Perhaps the oldest and most direct ancestor of the short story is the anecdote and illustrative story, straight to the point. The ancient parable and fable, starkly brief narrative used to enforce some moral or spiritual truth, anticipate the severe brevity and unity of some short stories written today. Short stories tend to be less complex test element than novels. Usually a short story focuses on one incident; has a single plot, a single setting, and a small number of characters; and covers a short period of time. In longer forms of fiction, stories tend to contain certain core elements of dramatic structure: exposition (the introduction of setting, situation and main characters); complication (the event that introduces the conflict); rising action, crisis (the decisive moment for the protagonist and his commitment to a course of action); climax (the point of highest interest in terms of the conflict and the point with the most action); resolution (the point when the conflict is resolved); and moral. Because of their length, short stories may or may not follow this pattern. Some do not follow patterns at all. For example, modern short stories only occasionally have an exposition. More typical, though, is an abrupt beginning, with the story starting in the middle of the action (in medias res). As with longer stories, plots of short stories also have a climax, crisis, or turning point. However, the endings of many short stories are abrupt and open and may or may not have a moral or practical lesson. As with any art forms, the exact characteristics of a short story will vary by creator. When short stories intend to convey a specific ethical or moral perspective, they fall into a more specific sub-category called Parables (or Fables). This specific kind of short story has been used by spiritual and religious leaders worldwide to inspire, enlighten, and educate their followers.


Authors such as Charles Dickens, Kurt Vonnegut, Jr., Nathaniel Hawthorne, Virginia Woolf, Bolesław Prus, Dino Buzzati, Rudyard Kipling, William Faulkner, F. Scott Fitzgerald, James Joyce, P. G. Wodehouse, H. P. Lovecraft and Ernest Hemingway were highly accomplished writers of both short stories and novels. Short stories have their face in oral story-telling traditions and the prose anecdote, a swiftly sketched situation that quickly comes to its point. With the rise of the comparatively realistic novel, the short story evolved as a miniature version, with some of its first perfectly independent examples in the tales of E. T. A. Hoffmann. Other 19th-century writers well known for their short stories include Nikolai Gogol, Guy de Maupassant, and Bolesław Prus. Some authors are known almost entirely for their short stories, either by choice (they wrote nothing else) or by critical regard (short-story writing is thought of as a challenging art). An example is Jorge Luis Borges, who won American fame with "The Garden of Forking Paths", published in the August 1948 Ellery Queen's Mystery Magazine. Another example is O. Henry (author of "Gift of the Magi"), for whom the O. Henry Award is named. American examples include Flannery O'Connor, John Cheever, and Raymond Carver. Short stories have often been adapted for half-hour and hour radio dramas, as on NBC Presents: Short Story (1951–52). The art of storytelling is doubtlessly older than record of civilization. Even the so-called modern short story, which was the latest of the major literary types to evolve, has an ancient lineage. Perhaps the oldest and most direct ancestor of the short story is the anecdote and illustrative story, straight to the point. The ancient parable and fable, starkly brief narrative used to enforce some moral or spiritual truth, anticipate the severe brevity and unity of some short stories written today. Short stories tend to be less complex than novels. Usually a short test element 2 story focuses on one incident; has a single plot, a single setting, and a small number of characters; and covers a short period of time. In longer forms of fiction, stories tend to contain certain core elements of dramatic structure: exposition (the introduction of setting, situation and main characters); complication (the event that introduces the conflict); rising action, crisis (the decisive moment for the protagonist and his commitment to a course of action); climax (the point of highest interest in terms of the conflict and the point with the most action); resolution (the point when the conflict is resolved); and moral. Because of their length, short stories may or may not follow this pattern. Some do not follow patterns at all. For example, modern short stories only occasionally have an exposition. More typical, though, is an abrupt beginning, with the story starting in the middle of the action (in medias res). As with longer stories, plots of short stories also have a climax, crisis, or turning point. However, the endings of many short stories are abrupt and open and may or may not have a moral or practical lesson. As with any art forms, the exact characteristics of a short story will vary by creator. When short stories intend to convey a specific ethical or moral perspective, they fall into a more specific sub-category called Parables (or Fables). This specific kind of short story has been used by spiritual and religious leaders worldwide to inspire, enlighten, and educate their followers.


Directions:

Simply download jquerycustomscrollbar.zip, and refer to demo.htm inside for the required code you should add to your page. The zip file consists of, among other files, the following:

Set up Information

To set up a vertical scrollbar on a content DIV, simply give the DIV a unique ID attribute to identify it using:

<div id="divMain1" class="slickscroll">
long content...
long content...
long content...
"
"
</div>

This DIV should have its dimensions explicitly set via CSS, plus have the overflowing content set to hidden, using CSS's overflow:hidden property. Then, just call the jQuery function slickscroll(options) on it to add a vertical scrollbar to it:

$(document).ready(function () {
 scroll1 = $('#divMain1').slickscroll({ "verticalscrollbar": true });
}

where scroll1 should be an arbitrary but unique variable name (to reference this scrollbar in the future), and "#divMain1" is simply the ID of the scrollable content DIV.

To add a horizontal scrollbar to a content instead, the process is the same except that you need to define an extra container inside the scroll DIV that forces the content to flow horizontally first:

<div id="divMain1" class="slickscroll">
<div style="width:3000px">
long content...
long content...
long content...
"
"
</div>
</div>

The below describes the different supported options you can pass into slickscroll():

$(selector).slickscroll(options) function

Separate each of the options below with a comma (,) when entering them into slickscroll():

setting Description
verticalscrollbar

Defaults to: false

Boolean on whether to enable vertical scrollbar on content.
horizontalscrollbar

Defaults to: false

Boolean on whether to enable horizontal scrollbar on content.
container_class_name

Defaults to: "slickscrollcontainer|

CSS class added to the scrollbar container's track (the grey area).

vertical_scrollbar_class_name

Defaults to: "slickscroll_vertical_scrollbar"

CSS class added to the vertical bar within the scrollbar container.
horizontal_scrollbar_class_name

Defaults to: "slickscroll_horizontal_scrollbar"

CSS class added to the horizontal bar within the scrollbar container.
min_scrollbar_size

Defaults to: 25

A value indicating the minimum width the scrollbar will take when page is resized.
mousewheel_scroll_speed

Defaults to: 5

A value indicating the scroll speed of the content when the mouse wheel is used.

So as an example:

$(document).ready(function () {
 scroll1 = $('#divMain1').slickscroll({ "verticalscrollbar": true, mousewheel_scroll_speed: 10 });
}

Once you've added a scrollbar to a DIV, you can continue to reference it using the variable scroll1 to scroll to different parts of the content dynamically, by calling one of the following methods:
 

Method Description
scrollBy(n, speed) Scrolls the div by n with the specified animation speed. N should be an integer denoting pixels, and speed an integer representing duration of action in milliseconds (so larger number translates into slower speed).
scrollTo(element, speed) Scrolls the div to a specified HTML element within the scroll content with the specified animation speed. Reference the element using a jQuery selector (ie: "#testelement"). For example:

<a onclick="scroll1.scrollTo($('#testelement'),200)" href="#">Scroll To Element</a>

scrollTop(speed) Scrolls the div to the top with the specified animation speed.
scrollBottom(speed)

Scrolls the div to the bottom with the specified animation speed

Creating the image scrollbar

The custom scrollbar consists of a single image that's sliced into an upper and lower portion, in the case of the default vertical scrollbar:

Then inside slickscroll.css, specify the paths to these two images plus their dimensions:

.slickscroll_vertical_scrollbar
{
position: absolute;
width: 14px;
height: 87px;
background-image: url('../Images/scrollbar_vertical_toppart.png');
background-repeat: no-repeat;
cursor:move;
}

.slickscroll_vertical_scrollbar div
{
background-position: bottom;
background-image: url('../Images/scrollbar_vertical_bottompart.png');
background-repeat: no-repeat;
width: 14px;
height: 15px;
bottom: 0px;
position:absolute;
}

When designing your scrollbar image, make sure both portions' background is set to the scrollbar's track color, which is a light gray by default inside slickscroll.css:

.slickscrollcontainer
{
padding: 0px;
margin: 0px;
background-color: #CCCCCC;
}

Do the same for the corresponding down portion of the scrollbar image.