JSPM

  • Created
  • Published
  • Downloads 25238
  • Score
    100M100P100Q146083F
  • License MIT

Customize scrollbar in modern browsers with smooth scrolling experience.

Package Exports

  • smooth-scrollbar

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (smooth-scrollbar) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Smooth Scrollbar

npm npm npm Travis

  • If you want smooth scrolling, use smooth-scrollbar;
  • If you want customizable scrollbars, use smooth-scrollbar;
  • If you want high-performance scrollbars, use smooth-scrollbar;
  • If you want incredibly prompt user support, use smooth-scrollbar;
  • If you want perfect scrollbar in modern browsers, use smooth-scrollbar!

So, why would you still use other scrollbar plugins? All you need is a quick setup:

Install

Via npm:

npm install smooth-scrollbar --save

Via bower:

bower install smooth-scrollbar --save

Browser Compatibility

Browser Version
IE 10+
Chrome 22+
Firefox 16+
Safari 8+
Android Browser 4+
Chrome for Android 32+
iOS Safari 7+

Demo

http://idiotwu.github.io/smooth-scrollbar/

Why is native scrolling slow?

As is explained in this article, browser repaint every frame in scrolling. Less painting is better.

To avoid repainting, I use translate3d in scroll content to create composite layers and force hardware accelerating.

Usage

Smooth scrollbar is defined as an UMD module which named Scrollbar, you can use any loader to load it:

import Scrollbar from 'smooth-scrollbar';

Or get it from window object:

const { Scrollbar } = window;

Don't forget to include the stylesheet in your page:

<link rel="stylesheet" href="dist/smooth-scrollbar.css">

Here're three ways to tell the plugin which element should be a smooth scrollbar:

  1. As an element:

    <scrollbar>
        ...
    </scrollbar>
  2. As an attribute:

    <section scrollbar>
        ...
    </section>
  3. As a data attribute

    <section data-scrollbar>
        ...
    </section>

Then init all scrollbars:

Scrollbar.initAll(options);

Or you can call Scrollbar.init(elem, options) to manually init the scrollbar.

Available Options for Scrollbar

parameter type default description
speed Number 1 Scrolling speed scale.
damping Number 0.1 Delta reduction damping, a float value between (0, 1), the lower the value is, the more smooth the scrolling will be.
thumbMinSize Number 20 Minimal size for scrollbar thumb.
syncCallbacks Boolean false Execute listeners in synchronous or asynchronous.
renderByPixels Boolean true Render scrolling by integer pixels, set to true to improve performance.
alwaysShowTracks Boolean false Keep scrollbar tracks visible whether it's scrolling or not.
continuousScrolling Boolean|String 'auto' Whether allow upper scrollable content to continue scrolling when current scrollbar reaches edge. When set to 'auto', it will be enabled on nested scrollbars, and disabled on first-class scrollbars.
overscrollEffect Boolean|String false Experimental overscroll effect, 'bounce' for iOS style effect and 'glow' for Android style effect. Be careful when you enable this feature!
overscrollEffectColor String '#87ceeb' Canvas paint color with 'glow' effect.
overscrollDamping Number 0.2 The same as damping, but for overscrolling.

Confusing with the option field? Try edit tool here!

DOM Structure

Following is the DOM structure that Scrollbar generated:

<scrollbar>
    <article class="scroll-content">
        your contents here...
    </article>
    <aside class="scrollbar-track scrollbar-track-x">
        <div class="scrollbar-thumb scrollbar-thumb-x"></div>
    </aside>
    <aside class="scrollbar-track scrollbar-track-y">
        <div class="scrollbar-thumb scrollbar-thumb-y"></div>
    </aside>
</scrollbar>

APIs

Scrollbar

Instance

Properties

Methods

Todo

  • Overscroll effect.
  • Webpack based workflow.
  • Tests.

Contributing

  • Create an issue with an example on jsbin.com, including latest version of smooth-scrollbar through:

    <link rel="stylesheet" href="https://cdn.rawgit.com/idiotWu/smooth-scrollbar/master/dist/smooth-scrollbar.css">
    
    <script src="https://cdn.rawgit.com/idiotWu/smooth-scrollbar/master/dist/smooth-scrollbar.js"></script>
  • Run your forks locally with following steps:

    1. Clone the repo: git clone https://github.com/idiotWu/smooth-scrollbar.git,
    2. Install dependencies: npm run install,
    3. Start server: npm start.

Before making pull requests, make sure you have run npm test!

License

MIT.