jQuery Horizontal Scrollbars
We all know that horizontal scrollbars are hideous. So, wouldn’t it be nice if we had a simple way to represent horizontal overflow? Well, thanks to a few javascript functions and some dinking around, I have put together a jQuery plug-in that will show a nice horizontal scrollbar to the user. Feel free to check out the Demo. Additionally, you can click the image below to be taken to the demo:
The jQuery horizontal scrollbar is very easy to implement. Feel free to check out the demo and download the source code. Additionally, checkout below on how to use the plug-in.
How To Use:
- Add the necessary JS and CSS files
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <script src="jquery.horizontal.scroll.js" type="text/javascript"></script>
- In the jQuery loop call the horizontalScroll function from the outer div object
<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
$('#horiz_container_outer').horizontalScroll();
});
// ]]></script>- Finally add the necessary HTML
<ul id="horiz_container_outer"> <li id="horiz_container_inner"> <ul id="horiz_container"> <li>Image 1</li> <li>Image 2</li> <li>Image 3</li> <li>Image 4</li> <li>Image 5</li> </ul> </li> </ul>
Tips:
- Make sure that you give an ‘ID’ name for each of the three ‘container outer’, ‘container inner’, and the ‘container’.
- The outer container will be the viewing space, if you only want the user to view 800px then set this width to 800px. You will have to set the #scrollbar and #track to the same width. The container will have to be the full width of the long list you wish to scroll.
If you have any questions or any problems with this plug-in please feel free to share. Thanks


53 Responses
6.24.2011
Hey Tony, this is really cool! I was wondering if you could write a post on how to create a jQuery plugin. The available options are too scary! Maybe you could write a simplified version so that one can get on with plugin dev real fast
6.24.2011
Hey Walmik
Thanks for the comment. I think I will create a real basic ‘Create your first jQuery Plug-in tutorial’ Obviously the plug-in won’t do too much, but the whole intent will be to create something very easy to learn.
Thanks again. Appreciate the comment. Talk to ya soon
6.24.2011
really cool and useful
6.24.2011
Thanks Man
6.30.2011
Hey Tony,
I was wondering if you could add “touch” functionality for this amazing plugin. It works perfectly in desktop browsers but doesn’t do too well on an iPad or Android phone. With jQuery touch functionality this plug-in can be awesomely slick on all devices!
Do let me know if you plan to do this as I am really looking to get a plugin something between this and http://www.zackgrossbart.com/hackito/touchslider/
Thanks!
7.10.2011
Dont know if you mentioned it but IE8 has problems with this scrollbar. It says that init_dw_Scroll is undefined. It seems to work alright except its not showing the inner draghandle.
Another thing…is it possible somehow to let the scrolling stop when the content does instead of having a 4000px area?
7.11.2011
Hi, nice scroll bar, is it an MIT license? OK to use in projects?
thanks
7.13.2011
hey man, that piece of code really rocks…does it work smoothly on MAC though??? I mean the different browsers like firefox, safari etc? Can u please confirm/
7.14.2011
This is an awesome scroll bar but I’m finding that the thumb component is not working in any windows browser – is there a work around for that – or do you know the reason why?
Thanks so much for your design and build!
7.14.2011
UPDATE: I replaced the Drag Bar Gradient with an image the exact same size for IE. Works great but the Drag Bar doesn’t scroll to the right end of the track. Any ideas as to why? Thx!!
7.14.2011
Getting init_dw_Scroll undefined on line 18 error in IE, scroll button not showing. Any ideas how to fix this? Thx
7.14.2011
ok – One more update and then I’ll promise not to bother you anymore.
To get the DragBar working in IE I created an image with a width of 340px and had to give the scrollbar a width of 813px (would of preferred 820px for the design) to work properly.
The image wouldn’t scroll to the far right of the scrollbar if it was shorter and anything longer than 340 cut off the edge.
again thx for your plugin, works great now….
7.15.2011
Great job.
Unfortunately, the horizontal scrollbar does not work with IE.
Den
7.17.2011
It looks like an greate plugin but it just needs something… well at least to fullfill my needs. I need to make an div 100% width and it has dynamic content (images). So sometimes it can only have one image and sometimes it can have 100 images.
The point is that the div which holds the images should be 100% width and the scrollbar controlls the content in the div.
I tried edditten your plug-in but the horizontal container keeps messing it up,
#horiz_container
{
width:4040px;
Any idears?
7.25.2011
Hey, nice work!
I don`t understand where do i have to fill in step 2?
I`m a newbie, can u help me?
7.26.2011
Nice script, you have some code to auto play?
thanks
8.9.2011
Work good.
How to shrink or expand scroll bar depending on the amount of content?
If i have a single image, i may not need a scroll.
Second, if I have 40-50 images, I will have a shrink scrollbar.
Urgent reply appreciated.
8.16.2011
How would you do this with dynamically populated images? If I try to set the width to auto on the #horiz_container it only shows whatever the width of the outer container is. I need the scrollbar to adjust according to how many images are populated in the horiz_container div. Thanks!
8.16.2011
Why doesn’t this work with IE? How can you make it compatible with IE7+
8.19.2011
In IE9 scroller handle is invisible… how to fix?
8.19.2011
Thanks, no I don’t have any code to do that. You mean for it to auto-scroll correct? That would be really cool to implement
8.19.2011
Yeah, it does have some problems in older version of IE, but you could always detect if they are on IE and just use the default scrollbars
8.19.2011
Yeah, actually this should work really well on a MAC
8.19.2011
Of course
Use this plug-in as you wish.
8.23.2011
Nice plugin! Is it possible to have multiple scroll containers on a page? When I attempt to change the names of the containers, it stops working. Thanks.
8.31.2011
Yo awesome plugin, great job.
Im having the the hardest time figure out how i can have the outer_container to auto expand, height wise(y-axis), I have content that expands and contracts when clicked inside of it.
I have tried multiple solutions and none have reallly worked out. Best I have done is it expands and inserts a scroll bar, but i would like the whole element to expand.
Was hoping maybe you had some insight to it.
9.18.2011
Awesome! This seems to be exactly what I was looking for, thanks so much!
One question tho… is this easy to set at 100% width? Can you only have a set amount of images or is there a way to use the same code over multiple pages with different amounts?
Great work man!
10.18.2011
Quick question, is it possible to use this on multiple pages with different widths for the container, I can’t get it to work for some reason
?
Thanks very much.
10.21.2011
hey tony,
your “jQuery Horizontal Scrollbars” is really awesome thing. I have applied for first time and i could easily implement it. Thanks for your greatest post.
Best Regards
The W3 Freelancer
thew3freelancer.com
10.26.2011
Great plugin! Is it possible to change the hover function of the arrows to onClick?
10.27.2011
Hi,
first thanks for tutorial.
But i have a firebug error when i browse your demo page or browse download code
“init_dw_Scroll is not defined”
can you help me?
Thanks!
10.27.2011
What can I change in the code to make the scrolling go faster, and onclick, when the user uses the arrows instead of the scrollbar?
11.5.2011
Pretty good. I wrote a little function to resize the container based on the size of it’s contents. Wish it worked better in IE out of the box, then it would be pretty solid.
11.8.2011
This plugin looks really good. Is there a minimum jQuery version this needs?
Also, I saw there are some escape sequences in the code. I was wondering why they were there.
11.9.2011
Not sure what the backwards compatibility is at the moment. I would just be safe and stick with the latest jQuery version
11.13.2011
how to get rid of hardcoded width for #horiz_container ? as if the content inside might be dynamic?
11.13.2011
One possible solution =>
$(document).ready(function(){
autoWidth( ‘#horiz_container’);
})
function autoWidth(x){
var countOne = 0;
$(x).children().each(function(){
countOne = countOne + $(this).outerWidth(true);
}),
$(x).css(‘width’, countOne)
}
11.14.2011
Thanks for this great plugin Tony!
I made a few tweaks so that it plays nice with IE7/8.
First off, this line of code in jquery.horizontal.scroll.js was causing JavaScript errors, preventing the content/plugin to work properly (it was saying init_dw_Scroll was undefined).
dw_Event.add( window, ‘load’, init_dw_Scroll);
I changed this to:
dw_Event.add( window, ‘load’, null);
Which seemed to get rid of that problem. Secondly, the dragBar on the track was hidden. The scrolling still worked, you just couldn’t visibly see where the dragBar was. I did a few CSS tweaks, getting rid of the moz/web kit properties on track and dragBar. I then used solid background colors for these instead, which allowed IE7/8 to view the scroller properly.
11.14.2011
Is there a way to have the slider start in the middle of the content. I started something like this….
$(‘#dragBar’).animate({left: ’50%’});
var midWidthContainer = $(‘#horiz_container_inner’).width()/2 * -1; $(‘#horiz_container_inner’).animate({left: midWidthContainer});
But as soon as you click to engage the drag bar it resets to the left again.
12.6.2011
Thanks! Great Post!
12.6.2011
It’s great, if you only need it to work on modern browsers. Unfortunatley, too much of the world is IE7+.
I have to move onto this one: http://jscrollpane.kelvinluck.com/
12.8.2011
Hi,
The scroll bar is good-looking; I would like to use on my website. Just it doesn’t work in Internet Explorer 9 on widows Seven!
Could you see it please?
Regards Marianna
12.15.2011
hey there, i used the following code to fill the screen width-wise, but i can’t get this to work for the scrollbar track:
#horiz_container_outer
{
width:window.innerWidth;
overflow-x:hidden;
any idea how i could go about doing this?
thanks.
12.28.2011
I want to start in middle both scroll and content. when first load the slider its show the middle content.
1.4.2012
Hi,
is there a way to scroll to a specific id. I want to have links at the bottom that will take you to a specific place in the line?
thanks
1.9.2012
Hey Tony how hard would it be to get this scroller to work vertically? Thanks!
1.12.2012
H!, this great work, but can you put example or tell me how to add Jquery easing
1.17.2012
Thank Tony, great script.. been looking around for some simple & working horizontal scroller for days.
I initially faced problem showing drag bar in IE but later found that there’s no background color / gradient defined for IE in CSS for #dragBar
Used http://www.colorzilla.com/gradient-editor/ to generate multi-browser compatible gradient background code.
1.20.2012
awesome plugin .but when i am using it multiple times on a single page .scroller is not working can you please help me
2.4.2012
Doesn’t work at all on IE9 – or at least on my IE9…