OverlayScrollbars
A javascript scrollbar plugin that hides the native scrollbars, provides custom styleable overlay scrollbars, and preserves the native functionality and feel.
Install / Use
/learn @KingSora/OverlayScrollbarsREADME
OverlayScrollbars
A javascript scrollbar plugin that hides the native scrollbars, provides custom styleable overlay scrollbars, and preserves the native functionality and feel.
Why?
I created this plugin because I hate ugly and space-consuming scrollbars. Similar plugins didn't meet my requirements in terms of features, quality, simplicity, license or browser support.
Goals & Features
- Simple, powerful and well-documented API.
- High browser compatibility - Firefox 59+, Chrome 55+, Opera 42+, Edge 15+ and Safari 10+.
- Fully Accessible - Native scrolling behavior is fully preserved.
- Can run on the server (
Node,DenoandBun) - SSR, SSG and ISR support. - Tested on various devices - Mobile, Desktop and Tablet.
- Tested with various (and mixed) inputs - Mouse, Touch and Pen.
- Treeshaking - bundle only what you really need.
- Automatic update detection - no polling required.
- Leverage latest browser features - best performance in new browsers.
- Flow independent - supports all values for
direction,flex-directionandwriting-mode. - Supports scroll snapping.
- Supports all virtual scrolling libraries.
- Supports the
bodyelement. - Easy and effective scrollbar styling.
- Highly customizable.
- TypeScript support - completely written in TypeScript.
- Dependency-free - 100% self-written to ensure small size and best functionality.
- High quality and fully typed framework versions for
react,vue,angular,svelteandsolid.
Choose your framework
In addition to the vanilla JavaScript version, you can use the official framework components & utilities:
<a href="https://github.com/KingSora/OverlayScrollbars/tree/master/packages/overlayscrollbars-react"><img src="https://raw.githubusercontent.com/KingSora/OverlayScrollbars/master/packages/overlayscrollbars-react/logo.svg" width="80" height="80" alt="React"></a> <a href="https://github.com/KingSora/OverlayScrollbars/tree/master/packages/overlayscrollbars-vue"><img src="https://raw.githubusercontent.com/KingSora/OverlayScrollbars/master/packages/overlayscrollbars-vue/logo.svg" width="80" height="80" alt="Vue"></a> <a href="https://github.com/KingSora/OverlayScrollbars/tree/master/packages/overlayscrollbars-ngx"><img src="https://raw.githubusercontent.com/KingSora/OverlayScrollbars/master/packages/overlayscrollbars-ngx/logo.svg" width="80" height="80" alt="Angular"></a> <a href="https://github.com/KingSora/OverlayScrollbars/tree/master/packages/overlayscrollbars-svelte"><img src="https://raw.githubusercontent.com/KingSora/OverlayScrollbars/master/packages/overlayscrollbars-svelte/logo.svg" width="80" height="80" alt="Svelte"></a> <a href="https://github.com/KingSora/OverlayScrollbars/tree/master/packages/overlayscrollbars-solid"><img src="https://raw.githubusercontent.com/KingSora/OverlayScrollbars/master/packages/overlayscrollbars-solid/logo.svg" width="80" height="80" alt="Solid"></a>
Getting started
npm & nodejs
OverlayScrollbars can be downloaded from npm or the package manager of your choice:
npm install overlayscrollbars
Once installed, it can be imported:
import 'overlayscrollbars/overlayscrollbars.css';
import {
OverlayScrollbars,
ScrollbarsHidingPlugin,
SizeObserverPlugin,
ClickScrollPlugin
} from 'overlayscrollbars';
Note: If the path
'overlayscrollbars/overlayscrollbars.css'is not working use'overlayscrollbars/styles/overlayscrollbars.css'as the import path for the CSS file.
You can use this Node Example as an reference / starting point.
Manual Download & Embedding
You can use OverlayScrollbars without any bundler or package manager.
Simply download one of the Releases or use a CDN.
- Use the javascript files with the
.browserextension. - Use the javascript files with the
.es5extension if you need to support older browsers, otherwise use the.es6files. - For production use the javascript / stylesheet files with the
.minextension.
Embed OverlayScrollbars manually in your HTML:
<link type="text/css" href="path/to/overlayscrollbars.css" rel="stylesheet" />
<script type="text/javascript" src="path/to/overlayscrollbars.browser.es.js" defer></script>
Use the global variable OverlayScrollbarsGlobal to access the api similar to how you can do it in nodejs / modules:
var {
OverlayScrollbars,
ScrollbarsHidingPlugin,
SizeObserverPlugin,
ClickScrollPlugin
} = OverlayScrollbarsGlobal;
You can use this Browser Example as an reference or a starting point.
The examples in this documentation use the import syntax instead of the OverlayScrollbarsGlobal object. However, both versions are equivalent.
Initialization
The initialization of OverlayScrollbars is explicit per element. Only the scrollbars of the element on which the plugin is initialized will be changed. Scrollbars of child elements will remain unchanged unless the plugin is initialized on them as well.
You can either initialize a new instance directly with an Element or with an Object where you have more control over the initialization process.
// Simple initialization with an element
const osInstance = OverlayScrollbars(document.querySelector('#myElement'), {});
Bridging initialization flickering
When you initialize OverlayScrollbars, it takes a few milliseconds to create and append all the elements to the DOM. During this time, the native scrollbars are still visible and will be switched out after the initialization is finished. This is seen as flickering.
To fix this behavior apply the data-overlayscrollbars-initialize attribute to the target element (and the html element as well when initializing a scrollbar for the body element).
<!-- for the body element -->
<html data-overlayscrollbars-initialize>
<head></head>
<body data-overlayscrollbars-initialize></body>
</html>
<!-- for all other elements -->
<div data-overlayscrollbars-initialize>
OverlayScrollbars is applied to this div
</div>
Initialization with an Object
<details> <summary> This is an in depth topic. Click here to read it. </summary> <br />The only required field is the target field. This is the field to which the plugin will be applied.
If you use the object initialization with only the target field, the result is equivalent to the element initialization:
// Both initializations have the same outcome
OverlayScrollbars(document.querySelector('#myElement'), {});
OverlayScrollbars({ target: document.querySelector('#myElement') }, {});
When initializing with an object you can specify how the library handles generated elements. For example, you can specify an existing element as the `viewport' element. Then the library won't generate it, but use the specified element instead:
OverlayScrollbars({
target: document.querySelector('#target'),
elements: {
viewport: document.querySelector('#viewport'),
},
}, {});
This is very useful if you have a fixed DOM structure and don't want OverlayScrollbars to create its own elements. These cases are very common when you want another library to work with OverlayScrollbars.
You can also decide to which element the scrollbars should be applied to:
OverlayScrollbars({
target: document.querySelector('#target'),
scrollbars: {
slot: document.querySelector('#target').parentElement,
},
}, {});
Last but not least, you can decide when to cancel the initialization:
OverlayScrollbars({
target: document.querySelector('#target'),
cancel: {
nativeScrollbarsOverlaid: true,
body: null,
}
}, {});
In the above example, the initialization will be aborted if the native scrollbars are overlaid, or if your target is a body element and the plugin has determined that initializing to the body ele
