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.
57 The best way to install and use perfect-scrollbar is with NPM.
58 It's registered on [npm](https://www.npmjs.com/package/perfect-scrollbar) as `perfect-scrollbar`.
61 $ npm install perfect-scrollbar
66 In the case you would like to have perfect-scrollbar in your Rails application, there is the [perfect-scrollbar-rails gem](https://github.com/YourCursus/perfect-scrollbar-rails).
70 You can download the latest stable version with download links [here](http://noraesae.github.io/perfect-scrollbar/).
71 You also can find all releases on [Releases](https://github.com/noraesae/perfect-scrollbar/releases).
75 If you want to use the development version of the plugin, use the
76 source files which are not minified. They're in the `src` directory.
77 The development version may be unstable, but some known bugs may
81 $ git clone https://github.com/noraesae/perfect-scrollbar.git
82 $ cd perfect-scrollbar/src
84 $ gulp # will lint and build the source code.
89 There is a Bower package for perfect-scrollbar as well. It is managed
90 under the [perfect-scrollbar-bower](https://github.com/noraesae/perfect-scrollbar-bower)
91 repository. The plugin is registered as `perfect-scrollbar`.
94 $ bower install perfect-scrollbar
99 * [cdnjs](http://www.cdnjs.com/libraries/jquery.perfect-scrollbar)
100 * [JSDelivr](https://www.jsdelivr.com/projects/perfect-scrollbar)
104 You can fork the following JSFiddles for testing and experimenting purposes:
106 * [Perfect Scrollbar](https://jsfiddle.net/DanielApt/xv0rrxv3/)
107 * [Perfect Scrollbar (jQuery)](https://jsfiddle.net/DanielApt/gbfLazpx/)
109 ## Before using perfect-scrollbar
111 The following requirements should meet.
113 * the container must have a 'position' css style.
114 * the container must be a normal container element.
115 * PS may not work well in `body`, `textarea`, `iframe` or flexbox.
117 The following requirements are included in the basic CSS, but please
118 keep in mind when you'd like to change the CSS files.
120 * the container must have an 'overflow: hidden' css style.
121 * the scrollbar's position must be 'absolute'.
122 * the scrollbar-x must have a 'bottom' css style, and the scrollbar-y
123 must have a 'right' css style.
125 Please keep in mind that perfect-scrollbar won't completely emulate native
126 scrolls. Scroll hooking is generally considered as bad practice, and
127 perfect-scrollbar should be used with care. Unless custom scroll is really needed,
128 please consider using native scrolls.
132 First of all, please check if the container element meets the
136 <link rel='stylesheet' href='dist/css/perfect-scrollbar.css' />
140 height: 100%; /* Or whatever you want (eg. 400px) */
145 I would recommend using the plugin with NPM and CommonJS module system
149 var Ps = require('perfect-scrollbar');
152 Or you can just load the script file as usual.
155 <script src='dist/js/perfect-scrollbar.js'></script>
158 To initialise the plugin, `Ps.initialize` is used.
161 var container = document.getElementById('container');
162 Ps.initialize(container);
165 It can be initialised with optional parameters.
168 Ps.initialize(container, {
170 wheelPropagation: true,
171 minScrollbarLength: 20
175 If the size of your container or content changes, call `update`.
178 Ps.update(container);
181 If you want to destroy the scrollbar, use `destroy`.
184 Ps.destroy(container);
187 If you want to scroll to somewhere, just use a `scrollTop`
191 container.scrollTop = 0;
192 Ps.update(container);
195 You can also get information about how to use the plugin
196 from code in the `examples` directory of the source tree.
200 As you may already know, perfect-scrollbar was a jQuery plugin.
201 And it *is* as well. There's a jQuery adaptor and the plugin can
202 be used in the same way it used to be used before.
204 I also recommend using NPM and CommonJS here, but it's not mandatory.
207 var $ = require('jquery');
208 require('perfect-scrollbar/jquery')($);
211 For sure, you can just import a built script.
214 <script src='dist/js/perfect-scrollbar.jquery.js'></script>
217 After importing it, you can use the plugin in the usual way.
220 $('#container').perfectScrollbar(); // Initialize
221 $('#container').perfectScrollbar({ ... }); // with options
222 $('#container').perfectScrollbar('update'); // Update
223 $('#container').perfectScrollbar('destroy'); // Destroy
228 For RequireJS loader, no need to write shim, simply import two libs:
233 perfectScrollbarJQuery: '.../perfect-scrollbar.jquery',
234 perfectScrollbar: '.../perfect-scrollbar',
241 and load `perfectScrollbar` in the initialiser of your app:
245 window.Ps = require('perfectScrollbar');
248 require('perfectScrollbarJQuery');
252 ## AngularJS + RequireJS usage
254 With the require.config settings above, at the beginning of your app module
255 definition, you can have following code:
261 'perfectScrollbarJquery'
264 var app = angular.module('myApp', []);
265 app.run(function () {
266 window.Ps = require('perfectScrollbar');
267 require('perfectScrollbarJQuery');
273 And initialise perfectScrollbar in a controller:
277 var container = document.getElementById('imgLoader');
278 Ps.initialize(container);
279 Ps.update(container);
282 var imgLoader = $('#imgLoader')
283 imgLoader.perfectScrollbar();
286 ## Optional parameters
288 perfect-scrollbar supports optional parameters.
291 It is a list of handlers to use to scroll the element.
292 **Default**: `['click-rail', 'drag-scrollbar', 'keyboard', 'wheel', 'touch']`
293 **Disabled by default**: `'selection'`
296 The scroll speed applied to mousewheel event.
300 If this option is true, when the scroll reaches the end of the side, mousewheel event will be propagated to parent element.
304 If this option is true, when the scroll reaches the end of the side, touch scrolling will be propagated to parent element.
307 ### minScrollbarLength
308 When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels.
311 ### maxScrollbarLength
312 When set to an integer value, the thumb part of the scrollbar will not expand over that number of pixels.
316 When set to true, and only one (vertical or horizontal) scrollbar is visible then both vertical and horizontal scrolling will affect the scrollbar.
320 When set to true, the scroll bar in X axis will not be available, regardless of the content width.
324 When set to true, the scroll bar in Y axis will not be available, regardless of the content height.
327 ### scrollXMarginOffset
328 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.
331 ### scrollYMarginOffset
332 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.
336 A string. It's a class name added to the container element. The class name is prepended with `ps-theme-`. So default theme class name is `ps-theme-default`. In order to create custom themes with scss use `ps-container($theme)` mixin, where `$theme` is a scss map.
337 **Default**: `'default'`
341 Add `theme` parameter:
343 Ps.initialize(container, {
344 theme: 'my-theme-name'
347 Create a class name prefixed with `.ps-theme-`. Include `ps-container()` mixin. It's recommended to use `map-merge()` to extend `$ps-theme-default` map with your custom styles.
349 .ps-theme-my-theme-name {
350 @include ps-container(map-merge($ps-theme-default, (
352 scrollbar-x-rail-height: 20px,
353 scrollbar-x-height: 20px,
354 scrollbar-y-rail-width: 20px,
355 scrollbar-y-width: 20px
362 Alternatively, if you don't want to create your own themes, but only modify the default one, you could simply overwrite `$ps-*` variables with your own values. In this case `theme` parameter is not required when calling `.initialize()` method. Remember do define your own variables before the `theme.scss` file is imported.
367 perfect-scrollbar dispatches custom events.
370 This event fires when the y-axis is scrolled in either direction.
373 This event fires when the x-axis is scrolled in either direction.
376 This event fires when scrolling upwards.
379 This event fires when scrolling downwards.
382 This event fires when scrolling to the left.
385 This event fires when scrolling to the right.
388 This event fires when scrolling reaches the start of the y-axis.
391 This event fires when scrolling reaches the end of the y-axis (useful for infinite scroll).
394 This event fires when scrolling reaches the start of the x-axis.
397 This event fires when scrolling reaches the end of the x-axis.
399 You can listen to these events either with vanilla JavaScript
401 document.addEventListener('ps-scroll-x', function () {
407 $(document).on('ps-scroll-x', function () {
414 ### Scrolling children inside perfect-scrollbar
416 You can natively scroll children inside `perfect-scrollbar` with the mouse-wheel. Scrolling automatically works if
417 the child is a `textarea`. All other elements need to have the `ps-child` class. This is demonstrated in [`/examples/children-native-scroll.html`](examples/children-native-scroll.html)
421 The plugin is designed to work in modern browsers, including the very latest
422 version of IE and Edge. Specifically, it should work in IEs >= IE10. If there
423 is any issue in the browsers, please [file it](https://github.com/noraesae/perfect-scrollbar/issues).
425 **The patches to fix problems in IE<=9 won't be accepted.**
427 When old IEs should be supported, please fork the project and
428 make patches personally.
432 If you have any idea to improve this project or any problem
433 using this, please feel free to upload an
434 [issue](https://github.com/noraesae/perfect-scrollbar/issues).
436 For common problems there is a
437 [FAQ](https://github.com/noraesae/perfect-scrollbar/wiki/FAQ) wiki page. Please check the page before uploading an issue.
441 The MIT License (MIT) Copyright (c) 2016 Hyunje Alex Jun and other contributors.
443 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:
445 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
447 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.