### 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