{"id":2004,"date":"2011-11-09T15:03:25","date_gmt":"2011-11-09T14:03:25","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?page_id=2004"},"modified":"2013-07-18T14:05:08","modified_gmt":"2013-07-18T13:05:08","slug":"vertical-scrollbar-plugin-using-jquery-ui-slider","status":"publish","type":"page","link":"https:\/\/www.simonbattersby.com\/blog\/vertical-scrollbar-plugin-using-jquery-ui-slider\/","title":{"rendered":"Vertical scrollbar plugin using jQuery UI slider"},"content":{"rendered":"<p>This plugin wraps up a number of the variants of my <a href=\"\/blog\/vertical-scrollbar-using-jquery-ui-slider\">jQuery vertical scrollbar<\/a>. Specifically this plugin supports:<\/p>\r\n\r\n<ul class=\"bulletlist\">\r\n<li>Multiple sliders on one page<\/li>\r\n<li>Scrolling with a mousewheel<\/li>\r\n<li>The use of images for scrollbar handles<\/li>\r\n<li>The size of the scrolled content changing after load (through AJAX for example)<\/li>\r\n<li>Customisation via css<\/li>\r\n<\/ul>\r\n\r\n<p>I&#8217;ve tested this in FF7, IE6, IE7, IE8, Chrome, Safari 5 and Opera 11, all on Windows 7, using jQuery 1.7.1 and jQuery UI 1.8.14. <\/p>\r\n<p><a href=\"\/demos\/vertical_scrollbar_demo_plugin.htm\">View a demo here<\/a>.<\/p>\r\n\r\n<h3>Setup<\/h3>\r\n\r\n<p>This plugin requires jQuery and jQuery UI, so you need to load these and the plugin into in your head, plus the css file. Optionally, you can load Brendan Aaron&#8217;s <a href=\"http:\/\/archive.plugins.jquery.com\/project\/mousewheel\">mousewheel plugin<\/a> as well. Then, assuming you have stored the javascript files in a folder in your root file called javascript, and the css file in a folder in your root file called css, then you should have something like this:<\/p>\r\n\r\n<pre>&lt;script type=\"text\/javascript\" src=\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script type=\"text\/javascript\" src=\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jqueryui\/1.8.14\/jquery-ui.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script type=\"text\/javascript\" src=\"\/javascript\/jquery.sbscroller.js\"&gt;&lt;\/script&gt;\r\n&lt;script type=\"text\/javascript\" src=\"\/javascript\/jquery.mousewheel.min.3.0.6.js\"&gt;&lt;\/script&gt;\r\n&lt;link rel=\"stylesheet\" href=\"\/css\/jquery.sbscroller.css\" \/&gt;\r\n<\/pre>\r\n\r\n<p>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 <code>position:absolute<\/code>. 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&#8230;<\/p>\r\n\r\n<h3>Configuration<\/h3>\r\n<p>All you need for the basic scrollbar is this, placed before the closing <code>&lt;\/body><\/code> tag &#8211; where <code>#myelement<\/code> is replaced by your element selector, obviously:<\/p>\r\n<pre>&lt;script type=\"text\/javascript\"&gt;\r\n$('#myelement').sbscroller();\r\n&lt;\/script&gt;<\/pre>\r\n<h4>Options<\/h4>\r\n<p>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.<\/p>\r\n<table class=\"displaytable\">\r\n<tr>\r\n<th>Option<\/th>\r\n<th>Description<\/th>\r\n<th>Example values<\/th>\r\n<th>Default value<\/th>\r\n<\/tr>\r\n<tr>\r\n<td>handleImage<\/td>\r\n<td>A string setting the path of the image to be used as a background-image on the slider handle, which will be set to <code>repeat-y<\/code>.<\/td>\r\n<td>&#8216;\/images\/handleimage.jpg&#8217;<\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>handleTopImage<\/td>\r\n<td>A string setting the path of the image to be added to the slider handle, and positioned at the top of the handle.<\/td>\r\n<td>&#8216;\/images\/handletop.jpg&#8217;<\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>handleBottomImage<\/td>\r\n<td>A string setting the path of the image to be added to the slider handle, and positioned at the bottom of the handle.<\/td>\r\n<td>&#8216;\/images\/handlebottom.jpg&#8217;<\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>handleGripImage<\/td>\r\n<td>A string setting the path of the image to be added to the slider handle, and positioned at the vertical centre of the handle.<\/td>\r\n<td>&#8216;\/images\/handlegrip.jpg&#8217;<\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>autohide<\/td>\r\n<td>A Boolean setting the scrollbar to dispay only when the scrolling element is hovered.<\/td>\r\n<td>true (note no quotes, this is a Boolean not a string)<\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<\/table>\r\n<p>An example with all options set:<\/p>\r\n<pre>$('#myelement').sbscroller({\r\n\t\t\t\thandleImage:'\/images\/misc\/scrollbar-handle-middle.png',\r\n\t\t\t\thandleTopImage:'\/images\/misc\/scrollbar-handle-top.png',\r\n\t\t\t\thandleBottomImage:'\/images\/misc\/scrollbar-handle-bottom.png',\r\n\t\t\t\thandleGripImage:'\/images\/misc\/scrollbar-handle-grip.png',\r\n\t\t\t\tautohide:true\r\n});<\/pre>\r\n<h4>Mousewheel support<\/h4>\r\n<p>The plugin automatically supports Brandon Aaron\u2019s <a title=\"Mousewheel plugin\" href=\"http:\/\/plugins.jquery.com\/project\/mousewheel\">mousewheel plugin<\/a>. If the plugin is present, the mousewheel can be used to scroll. Thanks to <a href=\"#comment-7845\">Dimitris<\/a> for this suggestion.<\/p>\r\n<h4>Methods<\/h4>\r\n<p>The plugin supports two methods:<\/p> \r\n<table class=\"displaytable\">\r\n<tr>\r\n<th>Method<\/th>\r\n<th>Description<\/th>\r\n<th>Usage<\/th>\r\n<\/tr>\r\n<tr>\r\n<td>refresh<\/td>\r\n<td>Refreshes the slider. This can be called after content has been added or removed from the scrolling element, after a hidden scrolling element is displayed, or for any other reason.<\/td>\r\n<td><code>$('#myelement').sbscroller('refresh');<\/code><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>reset<\/td>\r\n<td>Resets the slider to the initial state, with the scrollbar at the top<\/td>\r\n<td><code>$('#myelement').sbscroller('reset');<\/code><\/td>\r\n<\/tr>\r\n<\/table>\r\n\r\n<h3>Customisation<\/h3>\r\n<p>The background to the slider and slider handle are set as a default in the css as a light grey and dark grey respectively &#8211; you can change these using css, and also change the width of the slider if you wish.<\/p>\r\n<h3>Changelog<\/h3>\r\n\r\n<p><strong>V1.2 &#8211; 9 January 2012<\/strong> &#8211; 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).<\/p>\r\n<p><strong>V1.3 &#8211; 9 January 2012<\/strong> &#8211; Mousewheel code amended to run automatically if mousewheel plugin is present, mousewheel option removed.<\/p>\r\n<p><strong>V1.4 &#8211; 17 January 2012<\/strong> &#8211; Amended to cope with the height of the scroll pane being changed &#8211; thanks to <a href=\"#comment-8911\">JustPie<\/a> for pointing this omission out.<\/p>\r\n<p><strong>V1.5 &#8211; 12 February 2012<\/strong> &#8211; Amended to cater for the &#8216;refresh&#8217; being called when the scroller has not previously been set &#8211; the code now tests this and sets up the scroller if it doesn&#8217;t exist.<\/p>\r\n<p><strong>V1.6 &#8211; 6 March 2012<\/strong> &#8211; Removed the unnecessary wrap with .scroll-container (this was a legacy of floating the scrollbar instead of using absolute position).<\/p>\r\n<p><strong>V1.7 &#8211; 5 September 2012<\/strong> &#8211; Added a reset method.<\/p>\r\n<p><strong>V1.8 &#8211; 12 November 2012<\/strong> &#8211; Corrected a typo in the script affecting the <code>handleImage<\/code> option.<\/p>\r\n<p><strong>V1.9 &#8211; 29 January 2013<\/strong> &#8211; Amended code to cope with non-html content in the scrolled element<\/code> option.<\/p>\r\n\r\n<h3>Download<\/h3>\r\n<p><a href=\"\/demos\/downloads\/sbscroller.zip\">Zip file containing javascript and css files<\/a> (4kB)<\/p>\r\n<h3>Can I use this on my site?<\/h3>\r\n<p>Probably. See my <a href=\"\/blog\/use-of-code-from-this-site\/\">policies on using code from this site<\/a>.<\/p>\r\n<p class=\"orange\">Don&#8217;t link directly to the files on this site &#8211; that eats up my bandwidth. Create a copy on your own server.<\/p>\r\n<h3>Using the plugin with touchscreens<\/h3>\r\n<p>As it stands, this will not work with touch screens as jQuery UI does not itself support touch events. I have done only limited testing on an iPad, but <a href=\"http:\/\/touchpunch.furf.com\/\">TouchPunch<\/a> seems to address this nicely. Just include it on your page. Don&#8217;t currently have access to a touchscreen device, so if you do use TouchPunch, let me know how you get on.<\/p>\r\n<h3 id=\"ui_css\">Using the plugin with jQuery UI css<\/h3>\r\n<p>This plugin cannot be styled directly using the jQuery UI css themes &#8211; 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 <a href=\"\/css\/jquery.sbscroller.ui-lightness.css\">here<\/a>.<\/p>\r\n<p>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 <a href=\"\/css\/jquery.sbscroller_negate_ui.css\">here<\/a> (1kB). If you&#8217;re not using images for the handle you also need to set the background-image to none on <code>.ui-slider-handle<\/code>.<\/p>\r\n<h3>Using the plugin with WordPress<\/h3>\r\n<p>If you want to use this in WordPress, then the recommended way of adding all the scripts is to add the following to your functions.php:<\/p>\r\n<pre>function sb_scroller_scripts() {\r\n    wp_enqueue_script( 'jquery' );\r\n\twp_register_script( 'jqueryui', 'http:\/\/ajax.googleapis.com\/ajax\/libs\/jqueryui\/1.8.14\/jquery-ui.min.js');\r\n    wp_enqueue_script( 'jqueryui' );\r\n\twp_register_script( 'sbscroller', get_template_directory_uri().'\/js\/jquery.sbscroller.js');\r\n    wp_enqueue_script( 'sbscroller' );\r\n\twp_register_style('sbscrollercss',get_template_directory_uri().'\/jquery.sbscroller.css');\r\n\twp_enqueue_style('sbscrollercss');\r\n}    \r\n\r\nadd_action('wp_enqueue_scripts', 'sb_scroller_scripts');<\/pre>\r\n<p>This assumes you have jquery.sbscroller.js in your templates file and jquery.sbscroller.js in a subfolder called js. If you&#8217;ve already got jQuery loaded you don&#8217;t need the first line. This will load all the jQuery files and the css file in your header. You can also manually edit your header.php to include the files.<\/p>\r\n<p>Then on the page you want the scroller to appear, add the following via the html tab:<\/p>\r\n<pre>&lt;script type=\"text\/javascript\"&gt;\r\n<span class=\"code_highlight\">jQuery<\/span>('#myelement').sbscroller();\r\n&lt;\/script&gt;<\/pre>\r\n<p>Note that you have to use jQuery in full here since WordPress applies <a href=\"http:\/\/api.jquery.com\/jQuery.noConflict\/\">jQuery.noConflict()<\/a> by default, which means the $ shortcut won&#8217;t work.<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ve tested this in FF7, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2004","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/2004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=2004"}],"version-history":[{"count":19,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/2004\/revisions"}],"predecessor-version":[{"id":2859,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/2004\/revisions\/2859"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}