3 Minimalistic but perfect custom scrollbar plugin
5 [![Travis CI](https://travis-ci.org/noraesae/perfect-scrollbar.svg?branch=master)](https://travis-ci.org/noraesae/perfect-scrollbar)
7 If you want information of old versions<0.6.0, please refer to
8 [an old documentation](https://github.com/noraesae/perfect-scrollbar/tree/0.5.9).
10 ## Why perfect-scrollbar?
12 I was once working on a personal project, and trying to find the jQuery
13 scrollbar plugin that's *perfect*. But there was no *perfect* one.
14 That's why I decided to make one.
16 perfect-scrollbar is minimalistic but *perfect* (for me, and maybe for most developers)
17 scrollbar plugin working with jQuery or vanilla JavaScript as well.
21 ## [Demo](http://noraesae.github.com/perfect-scrollbar/)
23 [It's on the GitHub Pages](http://noraesae.github.com/perfect-scrollbar/).
25 ## What does *perfect* mean?
29 * There should be no css change on any original element.
30 * The scrollbar should not affect the original design layout.
31 * The design of the scrollbar should be (nearly) fully customizable.
32 * If the size of the container or the content changes, the scrollbar
33 size and position should be able to change.
34 * *New!* It should work with vanilla JavaScript and major tools like
37 ## Then perfect-scrollbar is really *perfect*?
39 * perfect-scrollbar has some requirements, but doesn't change or add
40 any style on original elements.
41 * perfect-scrollbar is designed not to have width or height. It's fixed
42 on the right and bottom side of the container.
43 * You can change nearly all css styles of the scrollbar. The scrollbar
44 design has no dependency on scripts.
45 * perfect-scrollbar supports an 'update' function. Whenever you need
46 to update the size or position of the scrollbar, just update.
47 * Additionally, perfect-scrollbar uses 'scrollTop' and 'scrollLeft',
48 not absolute positioning or something messy.
49 * perfect-scrollbar supports RTL perfectly on both WebKit and Gecko based browsers.
55 The best way to install and use perfect-scrollbar is with NPM.
56 It's registered on [npm](https://www.npmjs.org/package/perfect-scrollbar) as `perfect-scrollbar`.
59 $ npm install perfect-scrollbar
62 You can download the latest stable version with download links [here](http://noraesae.github.io/perfect-scrollbar/).
63 You also can find all releases on [Releases](https://github.com/noraesae/perfect-scrollbar/releases).
65 If you want to use the development version of the plugin, use the
66 source files which are not minified. They're in the `src` directory.
67 The development version may be unstable, but some known bugs may
71 $ git clone https://github.com/noraesae/perfect-scrollbar.git
72 $ cd perfect-scrollbar/src
74 $ gulp # will lint and build the source code.
78 There is a Bower package for perfect-scrollbar as well. It is managed
79 under the [perfect-scrollbar-bower](https://github.com/noraesae/perfect-scrollbar-bower)
80 repository. The plugin is registered as `perfect-scrollbar`.
83 $ bower install perfect-scrollbar
87 You can also load it from [cdnjs](http://cdnjs.com/).
88 It is registered as [`jquery.perfect-scrollbar`](http://www.cdnjs.com/libraries/jquery.perfect-scrollbar).
92 To make this plugin *perfect*, some requirements were unavoidable.
93 But, they're all very trivial and there is nothing to worry about.
95 The following requirements should meet.
97 * the container must have a 'position' css style.
99 The following requirements are included in the basic CSS, but please
100 keep in mind when you'd like to change the CSS files.
102 * the container must have an 'overflow:hidden' css style.
103 * the scrollbar's position must be 'absolute'.
104 * the scrollbar-x must have a 'bottom' css style, and the scrollbar-y
105 must have a 'right' css style.
109 First of all, please check if the container element meets the
113 <link rel='stylesheet' href='dist/css/perfect-scrollbar.css' />
117 height: 100%; /* Or whatever you want (eg. 400px) */
122 I would recommend using the plugin with NPM and CommonJS module system
126 var Ps = require('perfect-scrollbar');
129 Or you can just load the script file as usual.
132 <script src='dist/js/perfect-scrollbar.js'></script>
135 To initialise the plugin, `Ps.initialize` is used.
138 var container = document.getElementById('container');
139 Ps.initialize(container);
142 It can be initialised with optional parameters.
145 Ps.initialize(container, {
147 wheelPropagation: true,
148 minScrollbarLength: 20
152 If the size of your container or content changes, call `update`.
155 Ps.update(container);
158 If you want to destory the scrollbar, use `destroy`.
161 Ps.destroy(container);
164 If you want to scroll to somewhere, just use a `scrollTop`
168 container.scrollTop = 0;
169 Ps.update(container);
172 You can also get information about how to use the plugin
173 from code in the `examples` directory of the source tree.
177 As you may already know, perfect-scrollbar was a jQuery plugin.
178 And it *is* as well. There's a jQuery adaptor and the plugin can
179 be used in the same way it used to be used before.
181 I also recommend using NPM and CommonJS here, but it's not mandatory.
184 var $ = require('jquery');
185 require('perfect-scrollbar/jquery')($);
188 For sure, you can just import a built script.
191 <script src='dist/js/perfect-scrollbar.jquery.js'></script>
194 After importing it, you can use the plugin in the usual way.
197 $('#container').perfectScrollbar(); // Initialize
198 $('#container').perfectScrollbar({ ... }); // with options
199 $('#container').perfectScrollbar('update'); // Update
200 $('#container').perfectScrollbar('destroy'); // Destroy
205 For RequireJS loader, no need to write shim, simply import two libs:
210 perfectScrollbarJQuery: '.../perfect-scrollbar.jquery',
211 perfectScrollbar: '.../perfect-scrollbar',
218 and load `perfectScrollbar` in the initialiser of your app:
222 window.Ps = require('perfectScrollbar');
225 require('perfectScrollbarJQuery');
229 ## AngularJS + RequireJS usage
231 With the require.config settings above, at the beginning of your app module
232 definition, you can have following code:
238 'perfectScrollbarJquery'
241 var myApp = angular.module('myApp', [])
243 window.Ps = require('perfectScrollbar');
244 require('perfectScrollbarJQuery');
250 And initialise perfectScrollbar in a controller:
254 var container = document.getElementById('imgLoader');
255 Ps.initialize(container);
256 Ps.update(container);
259 var imgLoader = $("#imgLoader")
260 imgLoader.perfectScrollbar();
263 ## Optional parameters
265 perfect-scrollbar supports optional parameters.
268 The scroll speed applied to mousewheel event.
272 If this option is true, when the scroll reaches the end of the side, mousewheel event will be propagated to parent element.
276 If this option is true, when the scroll reaches the end of the side, touch scrolling will be propagated to parent element.
279 ### minScrollbarLength
280 When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels.
283 ### maxScrollbarLength
284 When set to an integer value, the thumb part of the scrollbar will not expand over that number of pixels.
288 When set to true, and only one (vertical or horizontal) scrollbar is visible then both vertical and horizontal scrolling will affect the scrollbar.
292 When set to true, the scroll works with arrow keys on the keyboard. The element is scrolled only when the mouse cursor hovers the element.
296 When set to true, the scroll bar in X axis will not be available, regardless of the content width.
300 When set to true, the scroll bar in Y axis will not be available, regardless of the content height.
303 ### scrollXMarginOffset
304 The number of pixels the content width can surpass the container width without enabling the X axis scroll bar. Allows some "wiggle room" or "offset break", so that X axis scroll bar is not enabled just because of a few pixels.
307 ### scrollYMarginOffset
308 The number of pixels the content height can surpass the container height without enabling the Y axis scroll bar. Allows some "wiggle room" or "offset break", so that Y axis scroll bar is not enabled just because of a few pixels.
311 ### stopPropagationOnClick
312 When set to false, when clicking on a rail, the click event will be allowed to propagate.
315 ### useSelectionScroll
316 When set to true, you can scroll the container by selecting text and move the cursor.
321 perfect-scrollbar dispatches custom events.
324 This event fires when the y-axis is scrolled in either direction.
327 This event fires when the x-axis is scrolled in either direction.
330 This event fires when scrolling upwards.
333 This event fires when scrolling downwards.
336 This event fires when scrolling to the left.
339 This event fires when scrolling to the right.
342 This event fires when scrolling reaches the start of the y-axis.
345 This event fires when scrolling reaches the end of the y-axis (useful for infinite scroll).
348 This event fires when scrolling reaches the start of the x-axis.
351 This event fires when scrolling reaches the end of the x-axis.
353 You can listen to these events either with vanilla JavaScript
355 document.addEventListener('ps-scroll-x', function () {
361 $(document).on('ps-scroll-x', function () {
368 #### Please read [Contributing](https://github.com/noraesae/perfect-scrollbar/wiki/Contributing) in the wiki before making any contribution.
371 I *really* welcome contributions! Please feel free to fork and issue pull requests when...
373 * You have a very nice idea to improve this plugin!
375 * You're good at English and can help my bad English!
377 For IE problems, please refer to [IE Support](https://github.com/noraesae/perfect-scrollbar#ie-support).
381 The plugin would work in IEs >= IE9 (not well, though).
383 **The patches to fix problems in IE<=8 won't be accepted.**
385 When old IEs should be supported, please fork the project and
386 make patches personally.
390 If you have any idea to improve this project or any problem
391 using this, please feel free to upload an
392 [issue](https://github.com/noraesae/perfect-scrollbar/issues).
394 For common problems there is a
395 [FAQ](https://github.com/noraesae/perfect-scrollbar/wiki/FAQ) wiki page. Please check the page before uploading an issue.
399 The MIT License (MIT) Copyright (c) 2015 Hyunje Alex Jun and other contributors.
401 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
403 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
405 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.