A javascript scrollbar plugin that hides the native scrollbars, provides custom styleable overlay scrollbars, and preserves the native functionality and feel.
A javascript scrollbar plugin that hides the native scrollbars, provides custom styleable overlay scrollbars, and preserves the native functionality and feel.
A javascript scrollbar plugin that hides the native scrollbars, provides custom styleable overlay scrollbars, and preserves the native functionality and feel.
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.
Node, Deno and Bun) - SSR, SSG and ISR support.direction, flex-direction and writing-mode.body element.react, vue, angular, svelte and solid.In addition to the vanilla JavaScript version, you can use the official framework components & utilities:
OverlayScrollbars can be downloaded from npm or the package manager of your choice:
npm install overlayscrollbarsOnce 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.
You can use OverlayScrollbars without any bundler or package manager.
Simply download one of the Releases or use a CDN.
.browser extension..es5 extension if you need to support older browsers, otherwise use the .es6 files..min extension.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.
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'), {});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).
<html data-overlayscrollbars-initialize>
<head></head>
<body data-overlayscrollbars-initialize></body>
</html>
Note: When the browser is not executing JavaScript, bridging initialization flickering will cause the native scrollbars to no be visible at all. If you want to support such a case, you can use the
overlayscrollbars.scriptingenabled.cssfile instead. Please be aware of the browser support of thescripting CSS media feature.
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 element would interfere with native functionality such as window.scrollTo.
OverlayScrollbars provides a set of CSS Custom Properties which makes styling easy and fast.
Out of the box you can choose between two scrollbar themes called os-theme-dark and os-theme-light. You can use the scrollbars.theme option to change the theme.
Custom scrollbar themes can be done in several ways. The easiest and fastest way is to use the predefined set of CSS Custom Properties aka CSS variables. If that's not enough, you can add custom class names or add custom styling to the existing class names.
OverlayScrollbars provides the following CSS Custom Properties:
Viewport styling:
[data-overlayscrollbars-viewport] {
overflow-x: var(--os-viewport-overflow-x);
overflow-y: var(--os-viewport-overflow-y);
} You can use the --os-viewport-overflow-x and --os-viewport-overflow-y properties to re-apply the correct overflow styles in case other styling rules (e.g. from 3rd party libraries) are overwriting them.
Scrollbar styling:
… You can change the properties for both scrollbars at once, or for each scrollbar axis. In the example below, I've chosen os-theme-custom as the theme name:
// Horizontal and vertical scrollbar are 10px
.os-theme-custom {
--os-size: 10px;
}
// Horizontal scrollbar is 10px
.os-theme-custom.os-scrollbar-horizontal {
--os-size: 10px;
}
// Vertical scrollbar is 20px
.os-theme-custom.os-scrollbar-vertical {
--os-size: 20px;
} You can then use your theme by assigning it via the scrollbars.theme option:
OverlayScrollbars(document.body, {
scrollbars: {
theme: 'os-theme-custom'
}
});Since scrollbar styles are usually simple, this set of options should be enough to get the styling you want. If you need more freedom, you can create your own styles by adding styling to the base class names described in the next section.
The scrollbars HTML markup looks like:
</div>
</div>
</div>
</div> The class names are simplified, in a real application the .os-scrollbar element can have additional class names which modify the appearance (mostly visibility and alignment).
Below is a list of the most important class names you will encounter:
| CSS class name | description |
|---|---|
.os-scrollbar |
The root element of a scrollbar. |
.os-scrollbar-rtl |
Indicates a RTL direction of the host element the scrollbar belongs to. |
.os-scrollbar-horizontal |
The root element of a horizontal scrollbar. |
.os-scrollbar-vertical |
The root element of a vertical scrollbar. |
.os-scrollbar-handle-interactive |
Indicates that the handle inside the scrollbar is interactive (scrollbars.dragScroll is not false). |
.os-scrollbar-track-interactive |
Indicates that the track inside the scrollbar is interactive (scrollbars.clickScroll is not false). |
.os-scrollbar-track |
The track element. This is the track of the nested handle element. If scrollbars.clickScroll is not false this is the element users can click to change the scroll offset. |
.os-scrollbar-handle |
The handle element. If scrollbars.dragScroll is not false this is the handle users can |
No open issues yet, or sync has not completed.