2015-02-12 00:40:18 +08:00
# `perfect-scrollbar`
2012-11-06 20:17:47 +08:00
2015-02-12 00:40:18 +08:00
Minimalistic but perfect custom scrollbar plugin
2012-11-06 20:59:36 +08:00
2015-02-12 00:40:18 +08:00
[![Travis CI ](https://travis-ci.org/noraesae/perfect-scrollbar.svg?branch=master )](https://travis-ci.org/noraesae/perfect-scrollbar)
2012-11-06 20:59:36 +08:00
2015-02-12 00:40:18 +08:00
If you want information of old versions< 0.6.0, please refer to
[an old documentation ](https://github.com/noraesae/perfect-scrollbar/tree/0.5.9 ).
## Why perfect-scrollbar?
I was once working on a personal project, and trying to find the jQuery
scrollbar plugin that's *perfect* . But there was no *perfect* one.
That's why I decided to make one.
perfect-scrollbar is minimalistic but *perfect* (for me, and maybe for most developers)
scrollbar plugin working with jQuery or vanilla JavaScript as well.
2012-11-10 03:22:33 +08:00
2014-12-03 01:47:34 +08:00
I hope you love it!
2012-11-10 03:22:33 +08:00
2015-02-12 00:40:18 +08:00
## [Demo](http://noraesae.github.com/perfect-scrollbar/)
[It's on the GitHub Pages ](http://noraesae.github.com/perfect-scrollbar/ ).
2012-11-10 03:22:33 +08:00
2015-02-12 00:40:18 +08:00
## What does *perfect* mean?
2012-11-10 03:22:33 +08:00
*perfect* means...
* There should be no css change on any original element.
* The scrollbar should not affect the original design layout.
* The design of the scrollbar should be (nearly) fully customizable.
2015-02-12 00:40:18 +08:00
* If the size of the container or the content changes, the scrollbar
size and position should be able to change.
* *New!* It should work with vanilla JavaScript and major tools like
NPM or Browserify.
## Then perfect-scrollbar is really *perfect*?
* perfect-scrollbar has some requirements, but doesn't change or add
any style on original elements.
* perfect-scrollbar is designed not to have width or height. It's fixed
on the right and bottom side of the container.
* You can change nearly all css styles of the scrollbar. The scrollbar
design has no dependency on scripts.
* perfect-scrollbar supports an 'update' function. Whenever you need
to update the size or position of the scrollbar, just update.
* Additionally, perfect-scrollbar uses 'scrollTop' and 'scrollLeft',
not absolute positioning or something messy.
2015-05-08 19:06:55 +08:00
* perfect-scrollbar supports RTL perfectly on both WebKit and Gecko based browsers.
2012-11-10 03:22:33 +08:00
It's cool, isn't it?
2015-02-12 00:40:18 +08:00
## Install
2014-03-10 15:26:50 +08:00
2016-01-26 23:26:52 +08:00
#### NPM
2015-02-13 06:50:29 +08:00
The best way to install and use perfect-scrollbar is with NPM.
2016-03-05 15:42:29 +08:00
It's registered on [npm ](https://www.npmjs.com/package/perfect-scrollbar ) as `perfect-scrollbar` .
2015-02-13 06:50:29 +08:00
```
$ npm install perfect-scrollbar
```
2016-01-26 23:26:52 +08:00
#### Rails
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 ).
#### Manually
2015-02-12 00:40:18 +08:00
You can download the latest stable version with download links [here ](http://noraesae.github.io/perfect-scrollbar/ ).
You also can find all releases on [Releases ](https://github.com/noraesae/perfect-scrollbar/releases ).
2014-03-10 15:26:50 +08:00
2016-01-26 23:26:52 +08:00
#### From sources
2015-02-12 00:40:18 +08:00
If you want to use the development version of the plugin, use the
source files which are not minified. They're in the `src` directory.
The development version may be unstable, but some known bugs may
have been fixed.
2014-03-10 15:26:50 +08:00
```
2014-10-22 04:43:07 +08:00
$ git clone https://github.com/noraesae/perfect-scrollbar.git
$ cd perfect-scrollbar/src
2015-02-12 00:40:18 +08:00
$ npm install
$ gulp # will lint and build the source code.
2014-03-10 15:26:50 +08:00
```
2016-01-26 23:26:52 +08:00
#### Bower
2015-02-13 06:50:29 +08:00
There is a Bower package for perfect-scrollbar as well. It is managed
under the [perfect-scrollbar-bower ](https://github.com/noraesae/perfect-scrollbar-bower )
repository. The plugin is registered as `perfect-scrollbar` .
2014-03-10 15:26:50 +08:00
```
2014-10-22 04:43:07 +08:00
$ bower install perfect-scrollbar
```
2016-02-27 01:54:19 +08:00
#### CDNs
2014-03-10 15:26:50 +08:00
2016-02-27 01:54:19 +08:00
* [cdnjs ](http://www.cdnjs.com/libraries/jquery.perfect-scrollbar )
* [JSDelivr ](https://www.jsdelivr.com/projects/perfect-scrollbar )
2015-02-12 00:40:18 +08:00
2016-02-06 01:55:16 +08:00
#### JSFiddle
You can fork the following JSFiddles for testing and experimenting purposes:
* [Perfect Scrollbar ](https://jsfiddle.net/DanielApt/xv0rrxv3/ )
* [Perfect Scrollbar (jQuery) ](https://jsfiddle.net/DanielApt/gbfLazpx/ )
2016-11-06 21:41:15 +08:00
## Before using perfect-scrollbar
2012-11-10 03:22:33 +08:00
2015-02-12 00:40:18 +08:00
The following requirements should meet.
2012-11-10 03:22:33 +08:00
* the container must have a 'position' css style.
2016-11-06 21:41:15 +08:00
* the container must be a normal container element.
* PS may not work well in `body` , `textarea` , `iframe` or flexbox.
2015-02-12 00:40:18 +08:00
The following requirements are included in the basic CSS, but please
keep in mind when you'd like to change the CSS files.
2016-11-06 21:41:15 +08:00
* the container must have an 'overflow: hidden' css style.
2012-11-10 03:22:33 +08:00
* the scrollbar's position must be 'absolute'.
2015-02-12 00:40:18 +08:00
* the scrollbar-x must have a 'bottom' css style, and the scrollbar-y
must have a 'right' css style.
2017-01-19 03:14:23 +08:00
2016-11-06 21:41:15 +08:00
Please keep in mind that perfect-scrollbar won't completely emulate native
scrolls. Scroll hooking is generally considered as bad practice, and
perfect-scrollbar should be used with care. Unless custom scroll is really needed,
please consider using native scrolls.
2013-06-18 15:02:01 +08:00
2015-02-12 00:40:18 +08:00
## How to use
2013-06-18 15:02:01 +08:00
2015-02-12 00:40:18 +08:00
First of all, please check if the container element meets the
requirements.
2014-07-06 01:01:24 +08:00
2015-02-12 00:40:18 +08:00
```html
2015-02-13 03:56:27 +08:00
< link rel = 'stylesheet' href = 'dist/css/perfect-scrollbar.css' / >
2015-02-12 00:40:18 +08:00
< style >
#container {
position: relative;
height: 100%; /* Or whatever you want (eg. 400px) */
}
< / style >
```
I would recommend using the plugin with NPM and CommonJS module system
like Browserify.
```javascript
var Ps = require('perfect-scrollbar');
```
Or you can just load the script file as usual.
```html
2015-02-13 03:56:27 +08:00
< script src = 'dist/js/perfect-scrollbar.js' > < / script >
2015-02-12 00:40:18 +08:00
```
To initialise the plugin, `Ps.initialize` is used.
```javascript
var container = document.getElementById('container');
Ps.initialize(container);
```
It can be initialised with optional parameters.
```javascript
Ps.initialize(container, {
wheelSpeed: 2,
wheelPropagation: true,
minScrollbarLength: 20
});
```
If the size of your container or content changes, call `update` .
```javascript
Ps.update(container);
```
2016-02-21 21:41:10 +08:00
If you want to destroy the scrollbar, use `destroy` .
2015-02-12 00:40:18 +08:00
```javascript
Ps.destroy(container);
```
If you want to scroll to somewhere, just use a `scrollTop`
property and update.
```javascript
container.scrollTop = 0;
Ps.update(container);
```
You can also get information about how to use the plugin
from code in the `examples` directory of the source tree.
## jQuery
As you may already know, perfect-scrollbar was a jQuery plugin.
And it *is* as well. There's a jQuery adaptor and the plugin can
be used in the same way it used to be used before.
2015-02-13 02:24:05 +08:00
I also recommend using NPM and CommonJS here, but it's not mandatory.
```javascript
var $ = require('jquery');
require('perfect-scrollbar/jquery')($);
```
For sure, you can just import a built script.
2015-02-12 00:40:18 +08:00
```html
2015-02-13 03:56:27 +08:00
< script src = 'dist/js/perfect-scrollbar.jquery.js' > < / script >
2015-02-12 00:40:18 +08:00
```
2015-02-13 02:24:05 +08:00
After importing it, you can use the plugin in the usual way.
2015-02-12 00:40:18 +08:00
```javascript
$('#container').perfectScrollbar(); // Initialize
$('#container').perfectScrollbar({ ... }); // with options
$('#container').perfectScrollbar('update'); // Update
$('#container').perfectScrollbar('destroy'); // Destroy
```
2015-06-14 17:31:53 +08:00
## RequireJS usage
For RequireJS loader, no need to write shim, simply import two libs:
```javascript
require.config({
paths: {
perfectScrollbarJQuery: '.../perfect-scrollbar.jquery',
perfectScrollbar: '.../perfect-scrollbar',
}
...
})
```
and load `perfectScrollbar` in the initialiser of your app:
```javascript
2015-12-27 19:11:43 +08:00
// for vanilla JS:
2015-06-14 17:31:53 +08:00
window.Ps = require('perfectScrollbar');
2015-12-27 19:11:43 +08:00
// for jQuery:
2015-06-14 17:31:53 +08:00
require('perfectScrollbarJQuery');
```
## AngularJS + RequireJS usage
With the require.config settings above, at the beginning of your app module
definition, you can have following code:
```javascript
define([
'angular',
'perfectScrollbar',
'perfectScrollbarJquery'
],
function (angular) {
2016-03-05 15:42:29 +08:00
var app = angular.module('myApp', []);
app.run(function () {
2015-06-14 17:31:53 +08:00
window.Ps = require('perfectScrollbar');
require('perfectScrollbarJQuery');
2016-03-05 15:42:29 +08:00
});
return app;
2015-06-14 17:31:53 +08:00
});
```
And initialise perfectScrollbar in a controller:
```javascript
2015-12-27 19:11:43 +08:00
// by vanilla JS:
2015-06-14 17:31:53 +08:00
var container = document.getElementById('imgLoader');
Ps.initialize(container);
Ps.update(container);
2015-12-27 19:11:43 +08:00
// or by jQuery:
2016-03-05 15:42:29 +08:00
var imgLoader = $('#imgLoader')
2015-06-14 17:31:53 +08:00
imgLoader.perfectScrollbar();
```
2015-02-12 00:40:18 +08:00
## Optional parameters
2013-03-14 12:44:51 +08:00
perfect-scrollbar supports optional parameters.
2016-02-22 19:53:19 +08:00
### handlers
2017-01-19 03:14:23 +08:00
It is a list of handlers to use to scroll the element.
**Default**: `['click-rail', 'drag-scrollbar', 'keyboard', 'wheel', 'touch']`
2016-02-22 19:53:19 +08:00
**Disabled by default**: `'selection'`
2013-03-14 12:44:51 +08:00
### wheelSpeed
2017-01-19 03:14:23 +08:00
The scroll speed applied to mousewheel event.
2016-02-22 19:53:19 +08:00
**Default**: `1`
2013-03-14 12:44:51 +08:00
### wheelPropagation
2017-01-19 03:14:23 +08:00
If this option is true, when the scroll reaches the end of the side, mousewheel event will be propagated to parent element.
2016-02-22 19:53:19 +08:00
**Default**: `false`
2013-03-14 12:44:51 +08:00
2014-12-02 19:38:54 +08:00
### swipePropagation
2017-01-19 03:14:23 +08:00
If this option is true, when the scroll reaches the end of the side, touch scrolling will be propagated to parent element.
2016-02-22 19:53:19 +08:00
**Default**: `true`
2014-07-06 01:01:24 +08:00
2013-07-18 00:53:02 +08:00
### minScrollbarLength
2017-01-19 03:14:23 +08:00
When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels.
2016-02-22 19:53:19 +08:00
**Default**: `null`
2014-07-17 01:55:58 +08:00
### maxScrollbarLength
2017-01-19 03:14:23 +08:00
When set to an integer value, the thumb part of the scrollbar will not expand over that number of pixels.
2016-02-22 19:53:19 +08:00
**Default**: `null`
2013-09-28 06:53:57 +08:00
### useBothWheelAxes
2017-01-19 03:14:23 +08:00
When set to true, and only one (vertical or horizontal) scrollbar is visible then both vertical and horizontal scrolling will affect the scrollbar.
2016-02-22 19:53:19 +08:00
**Default**: `false`
2013-09-30 16:24:12 +08:00
2013-11-07 04:20:30 +08:00
### suppressScrollX
2017-01-19 03:14:23 +08:00
When set to true, the scroll bar in X axis will not be available, regardless of the content width.
2016-02-22 19:53:19 +08:00
**Default**: `false`
2013-11-07 04:20:30 +08:00
### suppressScrollY
2017-01-19 03:14:23 +08:00
When set to true, the scroll bar in Y axis will not be available, regardless of the content height.
2016-02-22 19:53:19 +08:00
**Default**: `false`
2013-11-07 04:20:30 +08:00
### scrollXMarginOffset
2017-01-19 03:14:23 +08:00
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.
2016-02-22 19:53:19 +08:00
**Default**: `0`
2013-11-07 04:20:30 +08:00
### scrollYMarginOffset
2017-01-19 03:14:23 +08:00
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.
2016-02-22 19:53:19 +08:00
**Default**: `0`
2013-11-07 04:20:30 +08:00
2017-01-19 03:14:23 +08:00
### autoupdate
When set to true, the scroll will be updated when an element is added or removed from the content.
**Default**: `true`
2015-12-10 05:01:10 +08:00
### theme
2017-01-19 03:14:23 +08:00
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.
2016-02-22 19:53:19 +08:00
**Default**: `'default'`
2015-12-10 05:01:10 +08:00
2015-12-10 17:21:44 +08:00
**Example 1:**
2015-12-10 05:08:48 +08:00
2015-12-10 17:21:44 +08:00
Add `theme` parameter:
2015-12-10 05:08:48 +08:00
```javascript
Ps.initialize(container, {
2015-12-10 17:21:44 +08:00
theme: 'my-theme-name'
2015-12-10 05:08:48 +08:00
});
```
2015-12-10 17:21:44 +08:00
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.
2016-03-05 15:42:29 +08:00
```scss
2015-12-10 05:08:48 +08:00
.ps-theme-my-theme-name {
@include ps-container(map-merge($ps-theme-default, (
2015-12-10 17:21:44 +08:00
border-radius: 0,
scrollbar-x-rail-height: 20px,
scrollbar-x-height: 20px,
scrollbar-y-rail-width: 20px,
2016-03-05 15:42:29 +08:00
scrollbar-y-width: 20px
)));
2015-12-10 05:08:48 +08:00
}
```
2015-12-10 17:21:44 +08:00
**Example 2:**
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.
2015-09-24 04:05:28 +08:00
## Events
perfect-scrollbar dispatches custom events.
### ps-scroll-y
This event fires when the y-axis is scrolled in either direction.
### ps-scroll-x
This event fires when the x-axis is scrolled in either direction.
### ps-scroll-up
This event fires when scrolling upwards.
### ps-scroll-down
This event fires when scrolling downwards.
### ps-scroll-left
This event fires when scrolling to the left.
### ps-scroll-right
This event fires when scrolling to the right.
2015-09-28 18:13:25 +08:00
### ps-y-reach-start
This event fires when scrolling reaches the start of the y-axis.
### ps-y-reach-end
This event fires when scrolling reaches the end of the y-axis (useful for infinite scroll).
### ps-x-reach-start
This event fires when scrolling reaches the start of the x-axis.
### ps-x-reach-end
This event fires when scrolling reaches the end of the x-axis.
2017-01-19 03:14:23 +08:00
### ps-dom-change
This event fires when dom elements have been added to or removed from the content.
2015-09-24 04:05:28 +08:00
You can listen to these events either with vanilla JavaScript
```javascript
document.addEventListener('ps-scroll-x', function () {
// ...
2015-09-24 16:53:59 +08:00
})
2015-09-24 04:05:28 +08:00
```
or with jQuery
```javascript
$(document).on('ps-scroll-x', function () {
// ...
2015-09-24 16:53:59 +08:00
})
2015-09-24 04:05:28 +08:00
```
2016-02-23 06:36:20 +08:00
## Tips
### Scrolling children inside perfect-scrollbar
2017-01-19 03:14:23 +08:00
You can natively scroll children inside `perfect-scrollbar` with the mouse-wheel. Scrolling automatically works if
2016-02-23 06:36:20 +08:00
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 )
2015-02-12 00:40:18 +08:00
## IE Support
2013-06-16 17:28:37 +08:00
2016-02-22 20:35:29 +08:00
The plugin is designed to work in modern browsers, including the very latest
version of IE and Edge. Specifically, it should work in IEs >= IE10. If there
is any issue in the browsers, please [file it ](https://github.com/noraesae/perfect-scrollbar/issues ).
2013-06-16 17:28:37 +08:00
2016-02-22 20:35:29 +08:00
**The patches to fix problems in IE< =9 won't be accepted.**
2013-06-16 17:28:37 +08:00
2015-02-12 00:40:18 +08:00
When old IEs should be supported, please fork the project and
make patches personally.
2013-06-16 17:28:37 +08:00
2015-02-12 00:40:18 +08:00
## Helpdesk
2014-03-10 15:43:04 +08:00
2015-02-12 00:40:18 +08:00
If you have any idea to improve this project or any problem
using this, please feel free to upload an
[issue ](https://github.com/noraesae/perfect-scrollbar/issues ).
2014-03-10 15:43:04 +08:00
2015-02-12 00:40:18 +08:00
For common problems there is a
[FAQ ](https://github.com/noraesae/perfect-scrollbar/wiki/FAQ ) wiki page. Please check the page before uploading an issue.
2014-10-31 10:49:07 +08:00
2015-02-12 00:40:18 +08:00
## License
2012-11-10 03:22:33 +08:00
2016-01-05 10:29:24 +08:00
The MIT License (MIT) Copyright (c) 2016 Hyunje Alex Jun and other contributors.
2012-11-10 03:22:33 +08:00
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.