jQuery Custom Scrollbar is a jQuery plugin that lets you add fully customizable scrollbars to your sites. With the plugin
you can apply any css styles you want to your scrollbars.
- vertical and horizontal scrollbars you can style your own way
- scrolling by mouse dragging, mouse wheel, keyboard – just as you would with native browser scrollbar
- touch scrolling on mobile devices (Android, iPhone and iPad)
- a couple predefined skins showing you how to style scrollbars
- simple api that lets you scroll programmatically and be notified about scroll events
The plugin supports all major browsers: Chrome, Firefox, IE 7+.
To use the plugin you obviously need jQuery (it should work in jQuery 1.4 and later versions).
You can download the latest version here
In demos
folder of this repo, there are some example usages of custom scrollbar and its api. The demos are also
available online here
First download and add jquery.custom-scrollbar.js
and jquery.custom-scrollbar.css
to your site.
Suppose you have a container on your site with some lengthy content and you want to make it scrollable:
<div class="container">
<!-- Some lengthy content -->
</div>
Define it’s width and height (below some example size is used):
.container {
width: 300px;
height: 400px;
}
Add a skin class to your container:
<div class="container default-skin">
<!-- Some lengthy content -->
</div>
In the example we use default-skin
. Plugin comes with two other predefined skins: gray-skin
and modern-skin
.
You are not limited to that and can style scrollbar your own way.
Finally call this js code:
$(document).ready(function() {
$(".container").customScrollbar();
});
If container content does not fit in those sizes scrollbar will appear.
The above method will add vertical scrollbar only. If you also want to add horizontal scrollbar, there is one more css step required:
.container .overview {
width: 1000px;
}
This defines example total width of the scrolled content (not just the width of the visible part as in previous step).
There are some options you can pass when initializing scrollbar:
Option | Type | Default value | Description |
---|---|---|---|
skin |
String |
undefined |
A css skin class that will be added to the scrolled container. You can define it in html as well as here in options. Note that skin has to be defined in one of those ways. |
hScroll |
Boolean |
true |
Indicates whether or not, horizontal scrollbar should be shown when it’s necessary. |
vScroll |
Boolean |
true |
Same as above but applies to vertical scrollbar. |
updateOnWindowResize |
Boolean |
false |
Indicates whether scrollbar should recalculate thumb size when window is resized. See demos/resize.html for an example. |
For example:
$("#my-container").customScrollbar({skin: "default-skin", hScroll: false, updateOnWindowResize: true})
There are some methods of the plugin you may want to call.
Scrolls viewport to a given element inside scrolled content. An element might be jQuery object or a selector string. Example usage:
$(".container").customScrollbar("scrollTo", "#some-element-inside-container")
Sets horizontal scrollbar position to x
pixels. x
should be in range from 0 to scrolled content width. If it’s outside that range, content will be scrolled to the start or to the end.
$(".container").customScrollbar("scrollToX", 100)
Sets vertical scrollbar position to y
pixels. x
should be in range from 0 to scrolled content height. If it’s outside that range, content will be scrolled to the start or to the end.
$(".container").customScrollbar("scrollToY", 200)
Recalculates and sets sizes of all scrollbar components. Call this whenever your scrolled block changes its size and
scrollbar becomes invalid. After you call it scrollbar is adjusted to new sizes of your block.
$(".container").customScrollbar("resize")
Triggered whenever content is scrolled. Separate events are fired when vertical and horizontal scrollbar is moved.
$(".container").on("customScroll", function(event, scrollData) {});
Handler function takes two arguments. event
is standard jquery event object. scrollData
is an
object with 3 fields holding scroll specific information:
scrollPercent
– integer in range 0..100 indicating percentage position of the scrollbarscrollDirection
– string that can take following 4 values:left
,right
,up
,down
– indicates what direction the scrollbar was moved inscrollAxis
– string indicating which scrollbar was moved:X
for horizontal scrollbar andY
for vertical scrollbar
You can also bind handler to that event when initializing scrollbar:
$(".container").customScrollbar({
onCustomScroll: function(event, scrollData) {}
});