Vertical scrollbar plugin using jQuery UI slider
This plugin wraps up a number of the variants of my jQuery vertical scrollbar. Specifically this plugin supports:
- Multiple sliders on one page
- Scrolling with a mousewheel
- The use of images for scrollbar handles
- The size of the scrolled content changing after load (through AJAX for example)
- Customisation via css
I’ve tested this in FF7, IE6, IE7, IE8, Chrome, Safari 5 and Opera 11, all on Windows 7, using jQuery 1.6.4 and jQuery UI 1.8.14.
Setup
This plugin requires jQuery and jQuery UI, so you need to load these and the plugin into in your head, plus the css file:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script> <script type="text/javascript" src="/javascript/jquery.mousewheel.min.3.0.6.js"></script> <link rel="stylesheet" href="/css/jquery.sbscroller.css" />
The plugin will add a scrollbar to a selected element which has a fixed height and content which exceeds that height. The scrollbar will be placed on the right hand side of the scrolled element using position:absolute. At the moment this means the scrollbar must be within the parent scrolling element, as I thought this might be more convenient. If anyone is interested it would be possible to add an option to position the scrollbar outside the scrolling element, let me know…
Configuration
All you need for the basic scrollbar is this, placed before the closing </body> tag – where #myelement is replaced by your element selector, obviously:
<script type="text/javascript">
$('#myelement').sbscroller();
</script>
Options
To set an option, the name, followed by a colon, followed by the required value as shown below, all wrapped in curly brackets {}, needs to be entered in the round brackets after sbscroller. Separate multiple settings with a comma. See below for some examples. Different scrollers on the same page may be set up with different options.
| Option | Description | Example values | Default value |
|---|---|---|---|
| handleImage | A string setting the path of the image to be used as a background-image on the slider handle, which will be set to repeat-y. |
‘/images/handleimage.jpg’ | false |
| handleTopImage | A string setting the path of the image to be added to the slider handle, and positioned at the top of the handle. | ‘/images/handletop.jpg’ | false |
| handleBottomImage | A string setting the path of the image to be added to the slider handle, and positioned at the bottom of the handle. | ‘/images/handlebottom.jpg’ | false |
| handleGripImage | A string setting the path of the image to be added to the slider handle, and positioned at the vertical centre of the handle. | ‘/images/handlegrip.jpg’ | false |
| autohide | A Boolean setting the scrollbar to dispay only when the scrolling element is hovered. | true (note no quotes, this is a Boolean not a string) | false |
An example with all options set:
$('#myelement').sbscroller({
handleImage:'/images/misc/scrollbar-handle-middle.png',
handleTopImage:'/images/misc/scrollbar-handle-top.png',
handleBottomImage:'/images/misc/scrollbar-handle-bottom.png',
handleGripImage:'/images/misc/scrollbar-handle-grip.png',
autohide:true
});
Mousewheel support
The plugin automatically supports Brandon Aaron’s mousewheel plugin. If the plugin is present, the mousewheel can be used to scroll. Thanks to Dimitris for this suggestion.
Methods
The plugin supports a single method which can be used to refresh the slider after content has been added or removed from the scrolling element, after a hidden scrolling element is displayed, or for any other reason. This is called via:
$('#myelement').sbscroller('refresh');
Customisation
The background to the slider and slider handle are set as a default in the css as a light grey and dark grey respectively – you can change these using css, and also change the width of the slider if you wish.
Changelog
V1.2 – 9 January 2012 – Mousewheel code amended to allow for repeated show/hide of hidden elements, and to alter the scroll speed dependent on the height of the scrolled element (as opposed to moving a fixed percentage).
V1.3 – 9 January 2012 – Mousewheel code amended to run automatically if mousewheel plugin is present, mousewheel option removed.
V1.4 – 17 January 2012 – Amended to cope with the height of the scroll pane being changed – thanks to JustPie for pointing this omission out.
V1.5 – 12 February 2012 – Amended to cater for the ‘refresh’ being called when the scroller has not previously been set – the code now tests this and sets up the scroller if it doesn’t exist.
V1.6 – 6 March 2012 – Removed the unnecessary wrap with .scroll-container (this was a legacy of floating the scrollbar instead of using absolute position).
Download
Javascript file (8kB) | CSS file (1kB)
Can I use it on my site?
Yes. Leave me some feedback below. If you’re using this on a commercial project for which you’re getting paid then a small donation would be appreciated.
Using the plugin with jQuery UI css
This plugin cannot be styled directly using the jQuery UI css themes – the images supplied do not work well with a vertical, as opposed to horizontal slider, and the vertical slider requires an additional enclosing element for the slider, with the result that the wrong element is styled. However, it should be pretty straightforward to adapt your css to pick up the jQuery UI css styles if required. You can find an example which replicates the ui-lightness theme here.
Linked to this, if you are using jQuery UI css on your page as well as sbscroller.css then there is some interference between the two. This can be overcome by some additions to sbscroller.css which you can download here (1kB). If you’re not using images for the handle you also need to set the background-image to none on .ui-slider-handle.
hello,
I want to use your jquery plugin to customize the scroll-bars of a div. but I can’t get it to work. What do I surely have to put in the css code of my div ‘updates’ where I want scroll bars??
Thanks
Jeroen
Not sure if that is a real email address otherwise I would have sent you a mail. You shouldn’t need to add any css other than the file that comes with the plugin.
If it’s still not working can you send me a link, because otherwise I’m only guessing….
What should I write in my HTML, and what kind of ID should I use to the HTML element in order to use this plugin?
Your html can be pretty much anything you want – your container element (normally a div) needs to have a fixed height, and the content that you want to scroll obviously needs to be within that html element. It doesn’t matter what id or class you apply to your chosen element – you can use that to identify the element and apply the
. sbscrollermethod in the same way as any other jQuery method.i was seriously going nuts trying to make this work (ui slider as vertical scrollbar), nice work, thanks…
it even fits my need to scroll a previously hidden element:)
one suggestion… you could tweak so you don’t need a mousewheel option very easily, just replace
if(settings.mousewheel){
with
if($.fn.mousewheel){
and when mousewheel is available it is “auto-detected”
The mouse scrolling speed is dependant on the content. So if there is a lot of content the scrolling is incredibly fast, and if the content is just a little bigger than the container than the scrolling is very slow.
Can you help me find a way to make the mouse scrolling speed constant (independent of the content length)?
This amendment is included in V1.2.
How can I position the scrollbar at the bottom after a ‘refresh’?
You could do this by using
.find('.slider-vertical').slider("value", 0);on the scrolled element.I noticed that the scrollbar doesn’t resize correctly if you resize the parent element. This can be fixed by removing moving line 67: ” $scrollpane.find(‘.slider-wrap’).height(Math.round($scrollpane.height())); ” outside of the scope of the if statment that its currently in.
Hope this helps someone out.
Thanks for pointing this out. I have amended the plugin to cope with this – version 1.4.
I am wondering if this plugin has support for touch devices ?
I haven’t tested it myself on a mobile device. The issue is really whether jQuery UI works consistently on mobile devices – I suspect the answer is no at present.
hey there!
I bet you use Chrome to test because it works well on Chrome.
But on Firefox 10.0 (and previous versions), when you do a fast scroll-up on the touchpad (Macbook) when the scrollbar is at the top-most position then you should see some very crazy jittering… Normally. jQuery slider doesn’t work with mousewheel events so I assume you added that and so the bug is either in the mousewheel plugin or your code, but I’d think in the former.
Try it out yourself and see..
Marc
Marc
I tested on FF, Chrome, Opera, Safari and IE6+, as it says on the plugin page. I don’t see the behaviour you describe on FF10 on W7, so it may be restricted to Mac.
@John:
Jquery-ui doesn’t support touch devices as of the latest version, but there are libraries out there such as jquery.ui.touch-punch.js which add the compatibility into jquery-ui. However, I think it’d be better to use Jquery Mobile since that seems to be what is better supported on touch-capable devices.
If you have a trouble with mousewheel event (jQuery 1.7.+), you can fix it. Just replace:
if ( event.detail ) { delta = -event.detail/3; }
to
if ( event.originalEvent.detail ) { delta = -event.originalEvent.detail/3; }
I don’t see a problem with the mousewheel myself.
Is it possible to get your plugin to use the jQuery UI theme? It would be nice if your scrollbar looked like the ui-slider.
I’ll have a look at this sometime in the next week or so.I’ve had a quick look at this. There are two major issues with using the jQuery UI css. First, the images that jQuery UI css uses for the slider handle background are designed for a horizontal slider, not a vertical one, so they don’t work well. Secondly, jQuery UI css applies images to the background of the
.ui-sliderelement, which does not extend to the top and bottom of my scrollbar (instead you would need to style its parent element.slider-wrap.Major changes would be required to resolve these, which frankly don’t seem worthwhile to me. It’s simple enough to amend your css to replicate the style of the jQuery UI css.
Related to this, if the jQuery UI css is used on the same page as sbscroller.css then there is some interference. The following amendments to sbscroller.css should overcome this:
.scroll-content {position:absolute;top:0;left:0;padding-right:20px} .scroll-pane{position:relative;} .slider-wrap{position:absolute;right:0;top:0;background-color:lightgrey;width:20px;} .slider-vertical{position:relative;height:100%;background:none !important;border:none !important} .ui-slider-handle{background-color:darkgray;width:20px !important;height:10px;margin:0 auto;display:block;position:absolute;border:none !important;left:0 !important} .ui-slider-handle img{border:none} .scrollbar-top{position:absolute;top:0;} .scrollbar-bottom{position:absolute;bottom:0;} .scrollbar-grip{position:absolute;top:50%;}Hi Simon,
Love your script it was so easy to plugin! One question: Is there a way to change the height of the scrollbar so that it is shorter than the element that is being scrolled?
Thanks,
Ann
Hi Simon,
Got the scroller to fit where I wanted it to! Thanks so much, I donated a couple of drinks for you!
Cheers,
Ann
I have made a few improvements / bug fixes to the plugin. I commented all changes.
http://pastebin.com/yhEYQDk8
JustPie
Thanks for your interest (again). Re the mousewheel issues if the slider is refreshed, I’ve already fixed this in version 1.2 by simply removing the mousewheel and re-instantiating it. Moving the mousewheel code outside setSlider() will work, but the mousewheel scrolling will not change to reflect any change in height of the scroll pane.
How do I make the content in the plugin autoscroll on page load and reset to the first element when I reach the last.
Julian
The plugin doesn’t support autoscroll – but if you have a look here I have a non-plugin version that might do what you want.
View the source to see how it’s done.