### Install project dependencies Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/CONTRIBUTING.md Before starting development, install all necessary project dependencies using npm. ```bash $ npm install ``` -------------------------------- ### Start the development server Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/CONTRIBUTING.md Run this command to start a local development server for testing your changes. ```bash $ npm start ``` -------------------------------- ### Install Smooth Scrollbar via NPM Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/README.md Use this command to install the library using NPM. It is the recommended installation method. ```bash npm install smooth-scrollbar --save ``` -------------------------------- ### Install Smooth Scrollbar via Bower Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/README.md Use this command to install the smooth-scrollbar package using Bower. This is an alternative installation method. ```shell bower install smooth-scrollbar --save ``` -------------------------------- ### Plugin Execution Order Example Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Demonstrates how the order of plugin registration affects the transformation of scroll deltas. Plugins are invoked from left to right (FIFO). ```javascript class ScaleDeltaPlugin extends ScrollbarPlugin { static pluginName = 'scaleDelta'; transformDelta(delta, fromEvent) { return { x: delta.x * 2, y: delta.y * 2, } } } class NoopPlugin extends ScrollbarPlugin { static pluginName = 'noop'; transformDelta(delta, fromEvent) { console.log(delta); return { ...delta }; } } ``` ```javascript Scrollbar.use(ScaleDeltaPlugin, NoopPlugin); // apply delta... // > { x: 200, y: 200 } ``` ```javascript Scrollbar.use(NoopPlugin, ScaleDeltaPlugin); // apply delta... // > { x: 100, y: 100 } ``` -------------------------------- ### Plugin Initialization Hook (onInit) Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md The onInit() hook is called immediately after a scrollbar instance is created. Use this for initial setup tasks. ```javascript class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; onInit() { console.log('hello world!'); this._mount(); } } ``` -------------------------------- ### Invert Delta Plugin Implementation Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md An example plugin that inverts the delta (swaps x and y) for specific events. It uses `defaultOptions` to configure which events trigger the inversion. ```typescript import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar'; class InvertDeltaPlugin extends ScrollbarPlugin { static pluginName = 'invertDelta'; static defaultOptions = { events: [], }; transformDelta(delta, fromEvent) { if (this.shouldInvertDelta(fromEvent)) { return { x: delta.y, y: delta.x, }; } return delta; } shouldInvertDelta(fromEvent) { return this.options.events.some(rule => fromEvent.type.match(rule)); } } ``` ```typescript Scrollbar.use(InvertDeltaPlugin); const scrollbar = Scrollbar.init(elem, { plugins: { invertDelta: { events: [/wheel/], }, }, }); ``` -------------------------------- ### Instance Property: `scrollbar.size` Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Get the dimensions of the scrollbar container and its content. ```APIDOC ## Instance Property: `scrollbar.size` ### Description Returns the measured pixel dimensions of the scrollbar container and its content wrapper. ### Method `scrollbar.getSize()` ### Returns - **`size`** (object) - An object containing `container` and `content` dimensions. - **`container`** (object) - `{ width: number, height: number }` - **`content`** (object) - `{ width: number, height: number }` ### Usage ```js const scrollbar = Scrollbar.init(document.querySelector('#panel')); const size = scrollbar.getSize(); // { // container: { width: 600, height: 400 }, // content: { width: 600, height: 3000 } // } console.log(size.content.height - size.container.height); // scrollable distance: 2600 ``` ``` -------------------------------- ### Register Custom Event Filter Plugin Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/migration.md Register a custom plugin to filter events. This example shows how to block 'wheel' and 'touch' events using a 'blacklist' option, demonstrating event registration and update methods. ```javascript import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar'; class FilterEventPlugin extends ScrollbarPlugin { static pluginName = 'filterEvent'; static defaultOptions = { blacklist: [], }; transformDelta(delta, fromEvent) { if (this.shouldBlockEvent(fromEvent)) { return { x: 0, y: 0 }; } return delta; } shouldBlockEvent(fromEvent) { return this.options.blacklist.some(rule => fromEvent.type.match(rule)); } } Scrollbar.use(FilterEventPlugin); const scrollbar = Scrollbar.init(elem); // block events // 8.0.x scrollbar.options.plugins.filterEvent.blacklist = [/wheel/, /touch/]; // 8.1.x scrollbar.updatePluginOptions('filterEvent', { blacklist: [/wheel/, /touch/], }); ``` -------------------------------- ### Scrollbar.has() / Scrollbar.get() / Scrollbar.getAll() Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Provides methods to check for the existence of a scrollbar on an element, retrieve a specific scrollbar instance by its element, or get a list of all currently active scrollbar instances. ```APIDOC ## Scrollbar.has() / Scrollbar.get() / Scrollbar.getAll() ### Description These static methods allow for checking the presence of a scrollbar on a DOM element, retrieving a specific scrollbar instance associated with an element, and obtaining an array of all currently active scrollbar instances. ### Methods - `Scrollbar.has(elem)`: Checks if a scrollbar is attached to the given element. - `Scrollbar.get(elem)`: Retrieves the scrollbar instance attached to the given element. Returns `undefined` if no scrollbar is found. - `Scrollbar.getAll()`: Returns an array of all currently active scrollbar instances. ### Parameters #### Path Parameters - **elem** (DOMElement) - Required - The DOM element to check or retrieve the scrollbar for. ### Request Example ```javascript import Scrollbar from 'smooth-scrollbar'; const elem = document.querySelector('#panel'); const scrollbar = Scrollbar.init(elem); // Check if a scrollbar exists on 'elem' Scrollbar.has(elem); // true Scrollbar.has(document.body); // false // Get the scrollbar instance for 'elem' Scrollbar.get(elem) === scrollbar; // true Scrollbar.get(document.body); // undefined // Get all active scrollbar instances const all = Scrollbar.getAll(); // => [ SmoothScrollbar, ... ] all.forEach(sb => console.log(sb.offset)); ``` ### Response #### Success Response - `Scrollbar.has(elem)`: Returns `true` if a scrollbar is attached, `false` otherwise. - `Scrollbar.get(elem)`: Returns the `Scrollbar` instance or `undefined`. - `Scrollbar.getAll()`: Returns an array of `Scrollbar` instances. #### Response Example ```javascript const elem = document.querySelector('#my-element'); const scrollbarInstance = Scrollbar.get(elem); if (scrollbarInstance) { console.log('Scrollbar found:', scrollbarInstance.offset); } else { console.log('No scrollbar found on this element.'); } const allScrollbars = Scrollbar.getAll(); console.log('Total active scrollbars:', allScrollbars.length); ``` ``` -------------------------------- ### Read Scroll Position and Limits Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Access `offset` and `limit` properties to get the current scroll position and maximum scrollable bounds. `scrollLeft` and `scrollTop` offer readable and writable shorthand. ```javascript const scrollbar = Scrollbar.init(document.querySelector('#panel')); // Read state console.log(scrollbar.offset); // { x: 0, y: 0 } console.log(scrollbar.limit); // { x: 0, y: 2400 } // Read/write shorthands console.log(scrollbar.scrollTop); // 0 scrollbar.scrollTop = 500; // immediately jumps to y=500 (no easing) console.log(scrollbar.offset.y); // 500 console.log(scrollbar.scrollLeft); // 0 scrollbar.scrollLeft = 100; console.log(scrollbar.offset.x); // 100 ``` -------------------------------- ### Instance Lookup Methods for Smooth Scrollbar Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Check if a scrollbar exists on an element, retrieve a specific instance, or get a list of all active scrollbar instances. Useful for managing scrollbars programmatically. ```javascript import Scrollbar from 'smooth-scrollbar'; const elem = document.querySelector('#panel'); const scrollbar = Scrollbar.init(elem); Scrollbar.has(elem); // true Scrollbar.has(document.body); // false Scrollbar.get(elem) === scrollbar; // true Scrollbar.get(document.body); // undefined const all = Scrollbar.getAll(); // => [ SmoothScrollbar, ... ] all.forEach(sb => console.log(sb.offset)); ``` -------------------------------- ### Get All Scrollbar Instances Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Returns an array containing all currently active scrollbar instances in the document. ```javascript Scrollbar.getAll(); // [ SmoothScrollbar, SmoothScrollbar, ... ] ``` -------------------------------- ### Get Scrollbar Geometry Information Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md The `getSize()` method returns the current dimensions of both the scrollbar's container element and its content wrapper. ```javascript scrollbar.getSize(): ScrollbarSize ``` ```javascript { container: { width: 600, height: 400 }, content: { width: 1000, height: 3000 } } ``` -------------------------------- ### Initialize Scrollbar with Plugin Options Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Use a custom plugin by importing it and registering it with `Scrollbar.use()`. Then, initialize the scrollbar with specific options for the plugin under the `plugins` key. ```typescript import Scrollbar from 'smooth-scrollbar'; import MeowPlugin from 'meow-plugin'; Scrollbar.use(MeowPlugin); Scrollbar.init(elem, { plugins: { meow: { age: '10m', }, }, }); // > 'meow' { age: '10m' } ``` -------------------------------- ### Initialize Smooth Scrollbar with UMD Bundle Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/README.md Load the UMD bundle and initialize Smooth Scrollbar. This method is suitable for projects not using module bundlers. ```html ``` -------------------------------- ### Get or Set Scroll Top Position Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md The `scrollTop` property allows you to get the current vertical scroll offset or set a new one. Setting this value updates `scrollbar.offset.y`. ```javascript console.log(scrollbar.scrollTop); // 456 console.log(scrollbar.scrollTop === scrollbar.offset.y); // true scrollbar.scrollTop = 2048; // setPosition(offset.x, 2048); console.log(scrollbar.offset.y); // 2048 ``` -------------------------------- ### Get or Set Scroll Left Position Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md The `scrollLeft` property allows you to get the current horizontal scroll offset or set a new one. Setting this value updates `scrollbar.offset.x`. ```javascript console.log(scrollbar.scrollLeft); // 123 console.log(scrollbar.scrollLeft === scrollbar.offset.x); // true scrollbar.scrollLeft = 1024; // setPosition(1024, offset.y); console.log(scrollbar.offset.x); // 1024 ``` -------------------------------- ### Creating a Basic Plugin Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Demonstrates the basic structure for creating a custom plugin by extending the `ScrollbarPlugin` class and defining a `pluginName`. ```APIDOC ## Plugin Structure To create a plugin, you need to extend the abstract `ScrollbarPlugin` class and define a static `pluginName` property. ### `static pluginName: string` - **Description**: A unique string identifier for your plugin. This name is used internally by smooth-scrollbar, for example, to retrieve plugin-specific options. ### Example: Basic Plugin Definition ```js import { ScrollbarPlugin } from 'smooth-scrollbar'; class MyPlugin extends ScrollbarPlugin { // Required: A unique name for your plugin static pluginName = 'myPlugin'; // Optional: Define default options for your plugin static defaultOptions = { someOption: 'defaultValue' }; // Constructor (optional, if you need to do something before onInit) constructor(scrollbar, options) { super(scrollbar, options); // Initialize plugin-specific properties here if needed } // Implement lifecycle hooks as needed (e.g., onInit, onUpdate, etc.) onInit() { console.log(`MyPlugin '${MyPlugin.pluginName}' initialized with options:`, this.options); } } // To use the plugin with a scrollbar instance: // import Scrollbar from 'smooth-scrollbar'; // Scrollbar.use(MyPlugin); // const scrollbar = Scrollbar.init(document.querySelector('#my-scrollbar'), { // plugins: { // myPlugin: { // someOption: 'customValue' // } // } // }); ``` -------------------------------- ### Initializing Scrollbar with Overscroll Plugin Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/overscroll.md Demonstrates how to import and use the OverscrollPlugin with Scrollbar initialization, including basic usage with JavaScript and HTML script tags. ```APIDOC ## Initialize Scrollbar with Overscroll Plugin ### Description This snippet shows how to integrate the OverscrollPlugin into your smooth-scrollbar instance. You can use it with ES modules or traditional script tags. ### Usage **ES Modules:** ```javascript import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; Scrollbar.use(OverscrollPlugin); Scrollbar.init(elem, { plugins: { overscroll: options | false, }, }); ``` **HTML Script Tags:** ```html ``` ### Options Refer to the 'Available Options' section for details on configuring the `overscroll` plugin. ``` -------------------------------- ### scrollbar.scrollTop Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Gets or sets the vertical scroll position of the scrollbar, equivalent to `scrollbar.offset.y`. ```APIDOC ## scrollbar.scrollTop ### Description **Gets or sets** `scrollbar.offset.y`. ### Type `number` ### Example ```js console.log(scrollbar.scrollTop); // 456 console.log(scrollbar.scrollTop === scrollbar.offset.y); // true scrollbar.scrollTop = 2048; // setPosition(offset.x, 2048); console.log(scrollbar.offset.y); // 2048 ``` ``` -------------------------------- ### scrollbar.scrollLeft Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Gets or sets the horizontal scroll position of the scrollbar, equivalent to `scrollbar.offset.x`. ```APIDOC ## scrollbar.scrollLeft ### Description **Gets or sets** `scrollbar.offset.x`. ### Type `number` ### Example ```js console.log(scrollbar.scrollLeft); // 123 console.log(scrollbar.scrollLeft === scrollbar.offset.x); // true scrollbar.scrollLeft = 1024; // setPosition(1024, offset.y); console.log(scrollbar.offset.x); // 1024 ``` ``` -------------------------------- ### Initialize Scrollbar with UMD Bundle Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/README.md Load the UMD bundle script and initialize the Scrollbar. This method is suitable for projects not using module bundlers. ```html ``` -------------------------------- ### Import and Use Standalone Overscroll Plugin (Script Tags) Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/migration.md Include smooth-scrollbar.js and plugins/overscroll.js via script tags. Then, use window.Scrollbar and window.OverscrollPlugin to initialize the scrollbar with overscroll plugin options. ```html ``` -------------------------------- ### Scrollbar.use() Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Registers one or more plugin classes globally. Registered plugins will be available for use in subsequent `Scrollbar.init()` calls. Plugins are executed in the order they are registered (FIFO). ```APIDOC ## Scrollbar.use() ### Description Attaches one or more plugin classes globally before any `Scrollbar.init()` calls are made. Plugins are executed in the order they are registered (First-In, First-Out). ### Method `Scrollbar.use(PluginClass1, PluginClass2, ...)` ### Parameters #### Path Parameters - **PluginClass** (Class) - Required - One or more plugin classes to register. ### Request Example ```javascript import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar'; import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; // Define a custom plugin class SpeedPlugin extends ScrollbarPlugin { static pluginName = 'speed'; static defaultOptions = { factor: 1 }; transformDelta(delta) { const { factor } = this.options; return { x: delta.x * factor, y: delta.y * factor }; } } // Register plugins globally before initialization Scrollbar.use(SpeedPlugin, OverscrollPlugin); // Initialize scrollbar with plugin options const scrollbar = Scrollbar.init(document.querySelector('#panel'), { plugins: { speed: { factor: 2 }, // Options for SpeedPlugin overscroll: { effect: 'bounce', maxOverscroll: 150 }, // Options for OverscrollPlugin }, }); ``` ### Response #### Success Response This method does not return a value. Its effect is the global registration of plugins. #### Response Example ```javascript // After Scrollbar.use() is called, the plugins are available for use in Scrollbar.init() // The example above demonstrates how to use the registered plugins. ``` ``` -------------------------------- ### Get Scrollbar Instance from an Element Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Retrieves the scrollbar instance associated with a given DOM element. Returns `undefined` if no scrollbar is found on the element. ```javascript const scrollbar = Scrollbar.init(document.body); Scrollbar.get(document.body) === scrollbar; // true Scrollbar.get(document.documentElement); // undefined ``` -------------------------------- ### Import and Use Standalone Overscroll Plugin (ES Modules) Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/migration.md Import the OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll' and use it with Scrollbar.use(). Initialize the scrollbar with overscroll plugin options. ```javascript import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; Scrollbar.use(OverscrollPlugin); Scrollbar.init(elem, { plugins: { overscroll: options | false, }, }); ``` -------------------------------- ### Plugin Options Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Explains how to define and update options for plugins, allowing for customization of plugin behavior. ```APIDOC ## Plugin Options Plugins can be configured with options when the scrollbar is initialized. These options are passed to the plugin's constructor and are accessible via `this.options` within the plugin instance. ### Defining Default Options Plugins can define a static `defaultOptions` property to specify default values for their configuration. ```js class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; static defaultOptions = { color: 'blue', size: 10 }; onInit() { console.log('Plugin options:', this.options); // Access options like: this.options.color, this.options.size } } ``` ### Providing Options During Initialization When initializing a scrollbar, you can provide plugin-specific options within the `plugins` object of the initialization configuration. ```js import Scrollbar from 'smooth-scrollbar'; Scrollbar.use(MyPlugin); const scrollbar = Scrollbar.init(document.getElementById('my-scrollbar'), { plugins: { myPlugin: { // Override default options color: 'red', size: 15 } } }); ``` ### Updating Plugin Options Plugin options can be updated after initialization using the `updatePlugin` method on the scrollbar instance. ```js // Assuming 'scrollbar' is an initialized Scrollbar instance scrollbar.updatePlugin('myPlugin', { color: 'green' }); ``` This method will merge the provided options with the existing ones and trigger the `onUpdate` hook in the plugin if the options have changed. ``` -------------------------------- ### Basic Scrollbar Initialization Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/demo/index.html Initialize all elements with the 'data-scrollbar' attribute to enable smooth scrolling. Ensure the container has defined dimensions. ```html
``` -------------------------------- ### Register Plugins with Smooth Scrollbar Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Use `Scrollbar.use()` to register custom or built-in plugins globally before initializing any scrollbars. The order of registration determines the execution order (FIFO). ```javascript import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar'; import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; class SpeedPlugin extends ScrollbarPlugin { static pluginName = 'speed'; static defaultOptions = { factor: 1 }; transformDelta(delta) { const { factor } = this.options; return { x: delta.x * factor, y: delta.y * factor }; } } // Register plugins before init — order matters (FIFO execution) Scrollbar.use(SpeedPlugin, OverscrollPlugin); const scrollbar = Scrollbar.init(document.querySelector('#panel'), { plugins: { speed: { factor: 2 }, overscroll: { effect: 'bounce', maxOverscroll: 150 }, }, }); ``` -------------------------------- ### Initialize Smooth Scrollbar with ES6 Modules Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/README.md Import and initialize Smooth Scrollbar using ES6 modules. This is recommended when using bundlers like webpack or rollup. ```javascript import Scrollbar from 'smooth-scrollbar'; Scrollbar.init(document.querySelector('#my-scrollbar')); ``` -------------------------------- ### Register Multiple Plugins Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Register multiple plugins by passing them as arguments to `Scrollbar.use()`. The order of registration determines the execution order of plugin hooks. ```javascript Scrollbar.use(PluginA, PluginB, PluginC); ``` -------------------------------- ### Include Smooth Scrollbar via UMD Bundle Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt For projects not using a module bundler, include the UMD bundle directly in your HTML. ```html ``` -------------------------------- ### Plugin Order Recommendation Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md For plugins that modify layout, such as `OverscrollPlugin`, it is generally recommended to register them last to ensure they are applied after other transformations. ```javascript Scrollbar.use(PluginA, PluginB, PluginC, ..., OverscrollPlugin); ``` -------------------------------- ### Scrollbar.init() Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Initializes a scrollbar on a given DOM element. It accepts an optional options object for customization. ```APIDOC ## Scrollbar.init() ### Description Initializes a scrollbar on the given element and returns scrollbar instance. ### Method `Scrollbar.init(elem, options?)` ### Parameters #### Path Parameters - **elem** (Element) - The DOM element that you want to initialize scrollbar to. - **options** (object, optional) - Initial options, see [Available Options for Scrollbar](README.md#available-options-for-scrollbar) section above. ### Request Example ```js const scrollbar = Scrollbar.init(document.body, { damping: 0.2, }); ``` ``` -------------------------------- ### Scrollbar.initAll() Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Automatically initializes scrollbars on all elements that have the `data-scrollbar` attribute. Returns an array of all created scrollbar instances. ```APIDOC ## Scrollbar.initAll() ### Description Automatically init scrollbar on all elements base on the selector `[data-scrollbar]`, returns an array of scrollbars. ### Method `Scrollbar.init(options?)` ### Parameters #### Path Parameters - **options** (object, optional) - Initial options, see [Available Options for Scrollbar](README.md#available-options-for-scrollbar) section above. ### Request Example ```html
...
...
``` ```js Scrollbar.initAll(); // [ SmoothScrollbar, SmoothScrollbar ] ``` ``` -------------------------------- ### scrollbar.options Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Accesses the configuration options used for the current scrollbar instance. ```APIDOC ## scrollbar.options ### Description Options for current scrollbar instance. ### Type `ScrollbarOptions` ```ts type ScrollbarOptions = { damping: number, thumbMinSize: number, renderByPixels: boolean, alwaysShowTracks: boolean, continuousScrolling: boolean, wheelEventTarget: EventTarget | null, plugins: any, }; ``` ``` -------------------------------- ### Instance Property: `scrollbar.track` Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Provides direct access to scrollbar tracks and thumbs for programmatic control. ```APIDOC ## Instance Property: `scrollbar.track` ### Description Provides programmatic control over the X and Y scrollbar tracks and their thumb elements. ### Properties - **`track.xAxis`** (object) - Access to the horizontal track. - **`track.yAxis`** (object) - Access to the vertical track. - **`element`** (HTMLElement) - The DOM element of the track. - **`thumb.element`** (HTMLElement) - The DOM element of the thumb. - **`thumb.displaySize`** (number) - The current display size of the thumb in pixels. ### Methods - **`track.show()`** - Force-show the track. - **`track.hide()`** - Force-hide the track. - **`track.update()`** - Trigger a manual track update. ### Usage ```js const scrollbar = Scrollbar.init(document.querySelector('#panel')); // Force-show or hide individual tracks scrollbar.track.xAxis.show(); scrollbar.track.yAxis.hide(); // Access raw DOM elements console.log(scrollbar.track.xAxis.element); //
console.log(scrollbar.track.yAxis.thumb.element); //
console.log(scrollbar.track.yAxis.thumb.displaySize); // current thumb size in px // Trigger a manual track update scrollbar.track.update(); ``` ``` -------------------------------- ### Scrollbar.init() Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Initializes a smooth-scrollbar instance on a given DOM element. Returns the existing instance if one is already attached, or throws a TypeError if the provided element is not a DOM element. ```APIDOC ## Scrollbar.init() ### Description Initializes a smooth-scrollbar instance on a given DOM element and returns it. If a scrollbar already exists on the element, the existing instance is returned. Throws `TypeError` if `elem` is not a DOM element. ### Method `Scrollbar.init(elem, options?)` ### Parameters #### Path Parameters - **elem** (DOMElement) - Required - The DOM element to attach the scrollbar to. - **options** (Object) - Optional - Configuration options for the scrollbar. - **damping** (number) - Lower values result in smoother scrolling with more frames (default: 0.1). - **thumbMinSize** (number) - Minimum size of the scrollbar thumb in pixels (default: 20). - **renderByPixels** (boolean) - Enables integer pixel snapping for performance (default: true). - **alwaysShowTracks** (boolean) - Keeps scrollbar tracks visible at all times (default: false). - **continuousScrolling** (boolean) - Propagates scroll events to parent elements at scroll edges (default: true). - **delegateTo** (DOMElement) - The element to which wheel/touch events should be delegated. - **plugins** (Object) - Plugin-specific options. ### Request Example ```javascript import Scrollbar from 'smooth-scrollbar'; // Basic initialization const scrollbar = Scrollbar.init(document.querySelector('#my-scrollbar')); // With options const scrollbarWithOptions = Scrollbar.init(document.querySelector('#panel'), { damping: 0.05, thumbMinSize: 20, renderByPixels: true, alwaysShowTracks: false, continuousScrolling: true, delegateTo: document, plugins: {}, }); ``` ### Response #### Success Response (Scrollbar Instance) - **containerEl** (DOMElement) - The container element of the scrollbar. - **offset** (Object) - The current scroll offset { x, y }. - **limit** (Object) - The maximum scrollable offset { x, y }. #### Response Example ```javascript console.log(scrollbar.containerEl); //
console.log(scrollbar.offset); // { x: 0, y: 0 } console.log(scrollbar.limit); // { x: 0, y: 2400 } ``` ``` -------------------------------- ### Initialize Scrollbar Instance Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Initialize a new scrollbar instance on a given HTML element. This is the primary way to enable smooth scrolling. ```javascript const scrollbar = Scrollbar.init(elem); ``` -------------------------------- ### Initialize Scrollbar with ES6 Module Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/README.md Import and initialize the Scrollbar using ES6 modules. This method is recommended when using bundlers like webpack or rollup. ```javascript import Scrollbar from 'smooth-scrollbar'; Scrollbar.init(document.querySelector('#my-scrollbar'), options); ``` -------------------------------- ### Set Default Plugin Options Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Provide default options for your plugin by defining the static `defaultOptions` property within the plugin class. These options are used if no specific options are provided during initialization. ```typescript class MeowPlugin extends ScrollbarPlugin { static pluginName = 'meow'; static defaultOptions = { age: '0d', }; } ``` -------------------------------- ### Attach Plugins to Scrollbars Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Use this method to attach custom or built-in plugins to your scrollbar instances. Ensure plugins are imported correctly before use. ```javascript Scrollbar.use(...Plugins: ScrollbarPluginClass[]): void ``` ```javascript import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar'; import OverscrollPlugin from 'smooth-scrollbar/plugin/overscroll'; class MyPlugin extends ScrollbarPlugin { ... } Scrollbar.use(MyPlugin, OverscrollPlugin); ``` -------------------------------- ### Initialize Scrollbars on Multiple Elements Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Automatically initializes scrollbars on all elements with the `data-scrollbar` attribute. Returns an array of all created scrollbar instances. ```html
...
...
``` ```javascript Scrollbar.initAll(); // [ SmoothScrollbar, SmoothScrollbar ] ``` -------------------------------- ### Implement Speed Option with Custom Plugin Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/migration.md The 'speed' option is removed. Replicate its functionality by creating a custom plugin that transforms scroll deltas based on a 'speed' property. ```javascript // 7.x speed scale options.speed = 10; // equivalent in 8.x class ScalePlugin { static pluginName = 'scale'; static defaultOptions = { speed: 1, }; transformDelta(delta) { const { speed } = this.options; return { x: delta.x * speed, y: delta.y * speed, }; } } Scrollbar.use(ScalePlugin); Scrollbar.init(elem, { plugins: { scale: { speed: 10, }, }, }); ``` -------------------------------- ### scrollbar.containerEl Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Provides access to the HTML element to which the scrollbar was initialized. ```APIDOC ## scrollbar.containerEl ### Description The element that you initialized scrollbar to. ### Type `HTMLElement` ### Example ```js const scrollbar = Scrollbar.init(elem); console.log(scrollbar.containerEl === elem); // true ``` ``` -------------------------------- ### Initialize Smooth Scrollbar on a DOM Element Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Attach a smooth-scrollbar instance to a specific DOM element. You can customize behavior with options like damping and thumb size. Returns the instance or throws an error if the element is invalid. ```javascript import Scrollbar from 'smooth-scrollbar'; // Basic initialization const scrollbar = Scrollbar.init(document.querySelector('#my-scrollbar')); // With options const scrollbar = Scrollbar.init(document.querySelector('#panel'), { damping: 0.05, // lower = smoother, more frames (default: 0.1) thumbMinSize: 20, // px (default: 20) renderByPixels: true, // integer pixel snapping for perf (default: true) alwaysShowTracks: false, // keep tracks visible at all times (default: false) continuousScrolling: true, // propagate scroll to parent at edges (default: true) delegateTo: document, // delegate wheel/touch events to another element plugins: {}, // plugin-specific options }); console.log(scrollbar.containerEl); //
console.log(scrollbar.offset); // { x: 0, y: 0 } console.log(scrollbar.limit); // { x: 0, y: 2400 } ``` -------------------------------- ### Overscroll Plugin Options Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/overscroll.md Details the available configuration options for the Overscroll plugin, including effect type, damping, maximum overscroll distance, glow color, and scroll event callbacks. ```APIDOC ## Overscroll Plugin Options ### Description Customize the overscroll behavior and appearance using the following options when initializing the plugin. ### Available Options | parameter | type | default | description | | -------------- | ---------------------------------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | `effect` | `'bounce'` | `'glow'` | `'bounce'` | Overscroll effect, `'bounce'` for iOS style effect and `'glow'` for Android style effect. | | `damping` | `number` | `0.2` | Momentum reduction damping factor, a float value between `(0, 1)`. The lower the value is, the more smooth the overscrolling will be. | | `maxOverscroll`| `number` | `150` | Max-allowed overscroll distance. | | `glowColor` | `string` | `'#87ceeb'`| Canvas paint color for `'glow'` effect. | | `onScroll` | `function` | `null` | Callback function triggered on overscroll. Available since `8.2.0`. | ### `options.onScroll` Details #### Signature ```typescript onScroll(this: OverscrollPlugin, position: Position): void type Position = { x: number, y: number, }; ``` #### Usage Example ```javascript { plugins: { overscroll: { onScroll(position) { console.log(position); // Example: { x: 12, y: 34 } } } } } ``` #### Position Coordinate System * `MAX` stands for `options.maxOverscroll` ``` y: [-MAX, 0] ↑ +--------------+ | scrollable | x: [-MAX, 0] ← | + | → x: [0, MAX] | area | +--------------+ ↓ y: [0, MAX] ``` ``` -------------------------------- ### scrollbar.addListener() Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Registers a listener function to be called when scrolling occurs. The listener receives the scroll status, including offset and limit information. Be cautious with time-consuming listeners as they can impact performance. ```APIDOC ## scrollbar.addListener() ### Description Registers a scrolling event listener. ### Method `scrollbar.addListener(listener: ScrollListener): void` ### Parameters #### Path Parameters - **listener** (`ScrollListener`) - Required - Scrolling event listener. ### Request Example ```js scrollbar.addListener((status) => { console.log(status.offset.y); }); ``` ### Response #### Success Response (void) This method does not return a value. ``` -------------------------------- ### Overscroll Plugin for Native-Feel Effects Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Adds iOS bounce and Android glow effects at scroll boundaries. Import and register separately. Options like effect type, damping, and max distance can be configured. ```javascript import Scrollbar from 'smooth-scrollbar'; import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; Scrollbar.use(OverscrollPlugin); const scrollbar = Scrollbar.init(document.querySelector('#panel'), { plugins: { overscroll: { effect: 'bounce', // 'bounce' (iOS) | 'glow' (Android) damping: 0.15, // overscroll momentum damping (default: 0.2) maxOverscroll: 150, // max overscroll distance in px (default: 150) glowColor: '#87ceeb', // canvas glow color for 'glow' effect onScroll(position) { // position: { x: number, y: number } // x/y ranges: [-maxOverscroll, maxOverscroll] console.log('Overscroll position:', position); // => { x: 0, y: 45 } (overscrolled 45px past bottom) }, }, }, }); // Switch effects at runtime scrollbar.updatePluginOptions('overscroll', { effect: 'glow', glowColor: '#ff6b6b' }); // Disable overscroll on a specific element Scrollbar.init(document.querySelector('#no-bounce'), { plugins: { overscroll: false }, }); ``` -------------------------------- ### Scrollbar.use() Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Attaches plugins to scrollbars. This method allows you to extend the functionality of scrollbars by providing custom plugin classes. ```APIDOC ## Scrollbar.use() ### Description Attaches plugins to scrollbars. See [Plugin System](plugin.md). ### Method ```js Scrollbar.use(...Plugins: ScrollbarPluginClass[]): void ``` ### Parameters #### Path Parameters - **Plugins** (ScrollbarPluginClass[]) - Required - Scrollbar plugin class. ### Request Example ```js import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar'; import OverscrollPlugin from 'smooth-scrollbar/plugin/overscroll'; class MyPlugin extends ScrollbarPlugin { // ... } Scrollbar.use(MyPlugin, OverscrollPlugin); ``` ``` -------------------------------- ### Initialize Scrollbars on All `data-scrollbar` Elements Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Automatically initialize scrollbars on all elements in the document that have the `data-scrollbar` attribute. This is useful for quickly applying custom scrollbars to multiple elements. ```html
``` ```javascript import Scrollbar from 'smooth-scrollbar'; const scrollbars = Scrollbar.initAll({ damping: 0.08 }); // => [ SmoothScrollbar, SmoothScrollbar ] console.log(scrollbars.length); // 2 ``` -------------------------------- ### Initialize Scrollbar on an Element Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Use this method to initialize a scrollbar on a specific DOM element. You can provide custom options to configure its behavior. ```javascript Scrollbar.init(document.body, { damping: 0.2, }); ``` -------------------------------- ### Update Plugin Options at Runtime with updatePluginOptions() Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Updates options for a registered plugin after initialization without recreating the scrollbar instance. Available since 8.1.0. ```javascript import Scrollbar from 'smooth-scrollbar'; import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; Scrollbar.use(OverscrollPlugin); const scrollbar = Scrollbar.init(document.querySelector('#panel'), { plugins: { overscroll: { effect: 'bounce', glowColor: '#87ceeb' }, }, }); // Switch to glow effect at runtime scrollbar.updatePluginOptions('overscroll', { effect: 'glow', glowColor: '#ff6b6b', maxOverscroll: 100, }); ``` -------------------------------- ### Define a Custom Plugin with a Name Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md Define a custom plugin by extending `ScrollbarPlugin` and setting the static `pluginName` property. This name is used to reference the plugin when initializing or configuring the scrollbar. ```typescript class MeowPlugin extends ScrollbarPlugin { static pluginName = 'meow'; onInit() { console.log('meow', this.options); } } ``` -------------------------------- ### Configure Overscroll Effect Options Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/overscroll.md Set custom options for the overscroll plugin, including effect type, damping, maximum overscroll distance, and glow color. ```javascript { plugins: { overscroll: { effect: 'glow', damping: 0.1, maxOverscroll: 200, glowColor: '#ff0000' } } } ``` -------------------------------- ### Disabling Overscroll Plugin Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/overscroll.md Illustrates how to completely disable the Overscroll plugin during scrollbar initialization by setting the plugin option to false. ```APIDOC ## Disable Overscroll Plugin ### Description To disable the Overscroll plugin and prevent it from being initialized, set the `overscroll` option to `false` within the `plugins` configuration. ### Usage ```javascript Scrollbar.init(elem, { plugins: { // Overscroll plugin will NEVER be constructed on this scrollbar! overscroll: false, }, }); ``` ``` -------------------------------- ### Programmatic Momentum Control with addMomentum() and setMomentum() Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Use these methods to programmatically inject or override the scrollbar's momentum vector. Neither method fires plugin.transformDelta() hooks. ```javascript const scrollbar = Scrollbar.init(document.querySelector('#panel')); // Add to existing momentum (cumulative) scrollbar.addMomentum(0, 200); // adds 200px downward momentum // Override momentum entirely (e.g., stop all scrolling) scrollbar.setMomentum(0, 0); // equivalent to the deprecated scrollbar.stop() // Programmatically "fling" the scrollbar horizontally scrollbar.setMomentum(500, 0); ``` -------------------------------- ### Create a new git branch Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/CONTRIBUTING.md When submitting a pull request, create a new git branch off the 'develop' branch for your changes. ```bash $ git checkout -b my-fix-branch develop ``` -------------------------------- ### Scrollbar.getAll() Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Returns an array containing all currently active scrollbar instances in the document. ```APIDOC ## Scrollbar.getAll() ### Description Returns an array that contains all scrollbar instances. ### Method `Scrollbar.getAll(): SmoothScrollbar[]` ### Request Example ```js Scrollbar.getAll(); // [ SmoothScrollbar, SmoothScrollbar, ... ] ``` ``` -------------------------------- ### Scrollbar Plugin Lifecycle Hooks Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/plugin.md This section outlines the core hooks available in the ScrollbarPlugin class that allow developers to interact with the scrollbar's lifecycle. ```APIDOC ## Plugin System Hooks `ScrollbarPlugin` is an abstract class that serves as the base for creating custom plugins. It provides several hooks that are invoked at different stages of the scrollbar's lifecycle. ### `onInit()` - **Description**: Invoked right after a scrollbar instance is constructed. Ideal for plugin initialization. - **Example Usage**: ```js import { ScrollbarPlugin } from 'smooth-scrollbar'; class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; onInit() { console.log('Plugin initialized!'); this._mount(); // Example internal method call } } ``` ### `onUpdate()` - **Description**: Invoked after the `scrollbar.update()` method is called. Useful for updating plugin state based on scrollbar changes. - **Example Usage**: ```js class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; onUpdate() { console.log('Scrollbar updated, updating plugin.'); this._update(); // Example internal method call } } ``` ### `transformDelta(delta: Data2d, fromEvent: any): Data2d` - **Description**: Called immediately after a DOM event occurs and before the final delta is applied to the scrollbar. Allows modification of scroll delta for custom effects like scaling or overscroll. - **Parameters**: - `delta` (Data2d) - The calculated scroll delta (x, y). - `fromEvent` (any) - The original event that triggered the scroll. - **Returns**: `Data2d` - The potentially modified scroll delta. - **Example Usage**: ```js type Delta = { x: number, y: number, }; class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; transformDelta(delta: Delta, fromEvent: Event): Delta { // Double the scroll delta return { x: delta.x * 2, y: delta.y * 2, }; } } ``` ### `onRender(remainMomentum: Data2d): void` - **Description**: Invoked every time the render loop runs. Provides the remaining momentum of the scrollbar. This is the last chance to modify momentum using `scrollbar.addMomentum()` or `scrollbar.setMomentum()`. - **Parameters**: - `remainMomentum` (Data2d) - The remaining momentum (x, y) of the scrollbar. - **Caution**: Avoid heavy operations here as it runs within `requestAnimationFrame`. - **Example Usage**: ```js type Momentum = { x: number, y: number, }; class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; private _remain: Momentum; onRender(remainMomentum: Momentum) { this._remain = { ...remainMomentum, }; this.scrollbar.setMomentum(0, 0); // Example: reset momentum this._render(); // Example internal method call } } ``` ### `onDestroy(): void` - **Description**: Called after a scrollbar instance is destroyed. Use this hook for cleanup tasks. - **Example Usage**: ```js class MyPlugin extends ScrollbarPlugin { static pluginName = 'myPlugin'; onDestroy() { console.log('Plugin destroyed.'); this._unmount(); // Example internal method call } } ``` ``` -------------------------------- ### Scrollbar Options Type Definition Source: https://github.com/dolphin-wood/smooth-scrollbar/blob/develop/docs/api.md Defines the structure for scrollbar initialization options, including damping, thumb size, and rendering behavior. ```typescript type ScrollbarOptions = { damping: number, thumbMinSize: number, renderByPixels: boolean, alwaysShowTracks: boolean, continuousScrolling: boolean, wheelEventTarget: EventTarget | null, plugins: any, }; ``` -------------------------------- ### scrollbar.setPosition() Source: https://context7.com/dolphin-wood/smooth-scrollbar/llms.txt Instantly sets the scroll position to specified coordinates. Values outside the bounds are automatically clamped. ```APIDOC ## `scrollbar.setPosition()` ### Description Sets scroll offsets immediately without animation. Out-of-range values are clamped to `[0, limit]`. Use `withoutCallbacks: true` to suppress registered scroll listeners for this operation. ### Method `scrollbar.setPosition(x: number, y: number, options?: { withoutCallbacks?: boolean })` ### Parameters - **`x`** (number) - The target horizontal scroll offset. - **`y`** (number) - The target vertical scroll offset. - **`options`** (object, optional) - Configuration options. - **`withoutCallbacks`** (boolean) - If `true`, suppresses scroll event listeners. Defaults to `false`. ### Usage ```js const scrollbar = Scrollbar.init(document.querySelector('#panel')); // Instant jump — no easing scrollbar.setPosition(0, 500); console.log(scrollbar.offset); // { x: 0, y: 500 } // Clamped to limit automatically // limit = { x: 0, y: 1000 } scrollbar.setPosition(9999, 9999); console.log(scrollbar.offset); // { x: 0, y: 1000 } // Jump silently (listeners not fired) scrollbar.setPosition(0, 0, { withoutCallbacks: true }); ``` ```