smooth-scrollbar

repository·develop·Indexed 25 days ago

https://github.com/dolphin-wood/smooth-scrollbar

A customizable, flexible, and high-performance library for implementing smooth scrolling effects in modern web browsers. Version 8.8.4 provides tools to initialize scrollbars via Scrollbar.init() or Scrollbar.initAll(), manage scroll positions with easing, and extend functionality through a plugin-based architecture. It supports a wide range of browsers including IE 10+, Chrome 22+, Firefox 16+, and Safari 8+.

Tokens
9.6K
Snippets
26
Records
77
Agent score
84%

What's inside smooth-scrollbar

  1. Handle Synchronous Callbacks in 8.x

    develop

    In 8.x, options.syncCallbacks has been removed because scrolling listeners are always invoked synchronously. To perform asynchronous actions within a listener, wrap your logic in requestAnimationFrame.

    // equivalent in 8.x
    scrollbar.addListener(() => {
      requestAnimationFrame(() => {
        // do something
      });
    });
  2. Manage plugin execution order

    develop

    Plugins are invoked in the order they are registered via Scrollbar.use() (First-In-First-Out).

    If you have multiple plugins that modify the delta, the output of the first plugin becomes the input for the second. Be mindful of this order, especially when using plugins that modify layout (like OverscrollPlugin), which should typically be registered last.

    // PluginA's transformDelta runs, then PluginB's transformDelta runs
    Scrollbar.use(PluginA, PluginB);
  3. Use the Overscroll Plugin in 8.x

    develop

    In version 8.x, the overscroll effect is no longer bundled with the main package. You must import and use the OverscrollPlugin manually.

    import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll';
    
    Scrollbar.use(OverscrollPlugin);
    
    Scrollbar.init(elem, {
      plugins: {
        overscroll: options | false,
      },
    });
  4. Use the Overscroll Plugin

    develop

    The Overscroll plugin provides macOS-style bouncing effects or Android-style glow effects during overscrolling.

    To use it with a module bundler:

    1. Import OverscrollPlugin from smooth-scrollbar/plugins/overscroll.
    2. Register it using Scrollbar.use(OverscrollPlugin).
    3. Enable it in the Scrollbar.init configuration under plugins.overscroll.

    To use it via <script> tags:

    1. Include dist/smooth-scrollbar.js and dist/plugins/overscroll.js.
    2. Register it using Scrollbar.use(window.OverscrollPlugin).
    3. Enable it in the Scrollbar.init configuration.
    import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll';
    
    Scrollbar.use(OverscrollPlugin);
    
    Scrollbar.init(elem, {
      plugins: {
        overscroll: {
          effect: 'bounce'
        },
      },
    });
  5. Initialize smooth-scrollbar using ES6 modules

    develop

    If you are using a bundler like Webpack or Rollup, import Scrollbar as an ES6 module and call Scrollbar.init() passing the DOM element you want to use as the scroll container.

    import Scrollbar from 'smooth-scrollbar';
    
    Scrollbar.init(document.querySelector('#my-scrollbar'));
  6. Initialize smooth-scrollbar

    develop

    To use smooth-scrollbar, initialize it on a container element using Scrollbar.init().

    Important: The container element must have a defined width or height and be natively scrollable (e.g., overflow: auto) before initialization. This ensures the content inside is larger than the container itself.

    Use this method with bundlers like Webpack or Rollup:

    import Scrollbar from 'smooth-scrollbar';
    
    Scrollbar.init(document.querySelector('#my-scrollbar'), options);

    Using UMD Bundle

    If you are not using a bundler, load the script via a <script> tag:

    <script src="dist/smooth-scrollbar.js"></script>
    
    <script>
      var Scrollbar = window.Scrollbar;
    
      Scrollbar.init(document.querySelector('#my-scrollbar'), options);
    </script>
  7. Initialize smooth-scrollbar using UMD bundle

    develop

    If you are not using a bundler, load the UMD bundle via a <script> tag. The Scrollbar instance will be available on the global window object.

    <script src="dist/smooth-scrollbar.js"></script>
    
    <script>
      var Scrollbar = window.Scrollbar;
    
      Scrollbar.init(document.querySelector('#my-scrollbar'));
    </script>
  8. Migrate from 7.x to 8.x

    develop

    When upgrading from version 7.x to 8.x, note the following major changes:

    • CSS: smooth-scrollbar.css has been removed. You only need to import the main JS module/bundle.
    • Plugin System: Many features previously included in the core (like overscroll and speed control) have been moved to a plugin-based architecture.
    • Options & Methods: Several core options and methods have been removed or changed in signature to favor the plugin system or more explicit configuration objects.
  9. Understand limitations of smooth-scrollbar emulation

    develop

    Using smooth-scrollbar involves emulating scrollbars via JavaScript, which can lead to several user experience and technical trade-offs compared to native browser scrolling:

    • Input Interpolation: On trackpads or touch screens, scrolling deltas may be smoothed twice (once by the native input and once by the plugin), leading to unexpected behavior.
    • Performance & GPU Usage: The plugin uses translate3d for performance, but as the scrollable area increases, it can consume significant GPU resources, potentially causing jittery scrolling.
    • Pointer Event Incompatibility: The plugin calls event.preventDefault() on touchmove events to suppress native scrolling. This breaks pointer event streams and can cause unexpected side effects in applications relying on the Pointer Event API.

    If your primary goal is purely visual customization without emulating the scroll mechanism, consider using OverlayScrollbars instead, as it follows native scrolling behavior.