Perfect-Scrollbar

Minimalistic but perfect custom scrollbar plugin http://utatti.github.io/perfect-scrol…

perfect-scrollbar

Minimalistic but perfect custom scrollbar plugin

To read documentation for versions < 1.0, please visit v0.8.1.

Why perfect-scrollbar?

perfect-scrollbar is minimalistic but perfect (for me, and maybe for most developers) scrollbar plugin.

  • No change on design layout
  • No manipulation on DOM tree
  • Use plain scrollTop and scrollLeft
  • Scrollbar style is fully customizable
  • Efficient update on layout change

I hope you love it!

Demo

It’s on the GitHub Pages.

Table of Contents

Install

npm

The best way to install and use perfect-scrollbar is with npm. It’s registered as perfect-scrollbar.

$ npm install perfect-scrollbar

Manual download

You can manually download perfect-scrollbar from Releases.

From sources

If you want to use the development version of the plugin, build from source manually. The development version may be unstable.

$ git clone https://github.com/utatti/perfect-scrollbar.git
$ cd perfect-scrollbar
$ npm install
$ npm run build

JSFiddle

You can fork the following JSFiddles for testing and experimenting purposes:

Unofficial sources

Sources not mentioned above are not maintained officially. If there are issues of the following sources, please ask and resolve in each repository.

Before using perfect-scrollbar

The following requirements should meet.

  • the container must have a position style.
  • the container must be a normal container element.

The following requirements are included in the basic CSS, but please keep in mind when you’d like to change the CSS files.

  • the container must have an overflow: hidden css style.
  • the scrollbar’s position must be absolute.
  • the scrollbar-x must have bottom or top, and the scrollbar-y must have right or left.

Finally, scroll hooking is generally considered as a bad practice, and perfect-scrollbar should be used carefully. Unless custom scroll is really needed, using browser-native scroll is always recommended.

Caveats

perfect-scrollbar emulates some scrolls, but not all of the kinds. It also does not work in some situations. You can find these cases in Caveats. Basically, items listed in the caveats are hacky to implement and may not be implemented in the future. If the features are really needed, please consider using browser-native scroll.

How to use

First of all, please check if the container element meets the requirements and the main CSS is imported.


<style>
  #container {
    position: relative;
    width: 600px;
    height: 400px;
  }
</style>

 

Import via ES modules:

import PerfectScrollbar from 'perfect-scrollbar';

Or in browser:

<script src="dist/perfect-scrollbar.js"></script>

To initialise:

const container = document.querySelector('#container');
const ps = new PerfectScrollbar(container);

// or just with selector string
const ps = new PerfectScrollbar('#container');

It can be initialised with options.

const ps = new PerfectScrollbar('#container', {
  wheelSpeed: 2,
  wheelPropagation: true,
  minScrollbarLength: 20
});

If the size of your container or content changes, call update.

ps.update();

If you want to destroy the scrollbar, use destroy.

ps.destroy();
ps = null; // to make sure garbages are collected

If you want to scroll to somewhere, just update scrollTop.

const container = document.querySelector('#container');
container.scrollTop = 0;

You can also get information about how to use the plugin from code in examples/.

Subscribe to the Newsletter

Get our latest news,tutorials,guides,tips & deals delivered to your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

shares