2022-08-08 10:23:51 +02:00
2022-08-07 20:26:04 +02:00
2022-08-02 16:15:39 +02:00
2021-01-25 00:47:15 +01:00
2020-09-10 12:16:21 +02:00
2017-09-11 13:58:44 +02:00
2022-08-07 20:26:04 +02:00
2022-08-07 20:26:04 +02:00
2022-08-08 10:23:51 +02:00

OverlayScrollbars

Downloads Version License

OverlayScrollbars is a javascript scrollbar plugin that hides native scrollbars, provides custom styleable overlay scrollbars and keeps the native functionality and feeling.

Why

I've created this plugin because I hate ugly and space consuming scrollbars. Similar plugins haven't met my requirements in terms of features, quality, simplicity, license or browser support.

Goals & Features

  • Simple, powerful and good documented API
  • High browser compatibility - Firefox, Chrome, Opera, Edge, Safari 10+ and IE 11
  • Tested on various devices - Mobile, Desktop and Tablet
  • Tested with various (and mixed) inputs - Mouse, touch and pen
  • Plugin based features with Treeshaking - bundle only what you really need
  • Automatic update detection - no polling
  • Usage of latest browser features - best performance in new browsers
  • Bidirectional - LTR or RTL direction support
  • Simple and effective scrollbar styling
  • TypeScript support - fully written in TypeScript

Getting started

npm & Node

OverlayScrollbars can be downloaded from npm or the package manager of your choice:

npm install overlayscrollbars

After installation it can be imported:

import 'overlayscrollbars/overlayscrollbars.css';
import { OverlayScrollbars } from 'overlayscrollbars';
Manual download & embedding

You can use OverlayScrollbars without any bundler or package manager.
Simply download it from the Releases or use a CDN.

  • Use the javascript files with the .browser extension.
  • If you target old browsers use the .es5 javascript file, for new browsers .es6.
  • For production use the javascript / stylesheet files with the .min extension.

Embedd OverlayScrollbars manually in your HTML:

<link type="text/css" href="path/to/overlayscrollbars.css" rel="stylesheet" />
<script type="text/javascript" src="path/to/overlayscrollbars.js" defer></script>

Initialization

Note

During initialization its expected that the CSS file is loaded and parsed by the browser.

You can initialize either 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'), {});
Initialization with an Object

Note

For now please refer to the TypeScript definitions for a more detailed description of all possibilities.

The only required field is the target field. This is the field to which the plugin is applied to.
If you use the object initialization only with the target field, the outcome is equivalent to the element initialization:

// Both initializations have the same outcome

OverlayScrollbars(document.querySelector('#myElement'), {});
OverlayScrollbars({ target: document.querySelector('#myElement') }, {});

In the initialization object you can specify how the library is handling generated elements. For example you can appoint an existing element as the viewport element. Like this the library won't generate it but take 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 generate its own elements. Those cases arise very often when you want an other library to work together 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,
 },
}, {});

And last but not least you can decide when the initialization should be canceled:

OverlayScrollbars({ 
 target: document.querySelector('#target'),
 cancel: {
   nativeScrollbarsOverlaid: true,
   body: null,
 }
}, {});

In the above example the initialization is canceled when the native scrollbars are overlaid or when your target is a body element and the plugin determined that a initialization to the body element would affect native functionality like window.scrollTo.

Options

OverlayScrollbars provides a lot of options which can be changed at any time:

const defaultOptions = {
  paddingAbsolute: false,
  showNativeOverlaidScrollbars: false,
  update: {
    elementEvents: [['img', 'load']],
    debounce: [0, 33],
    attributes: null,
    ignoreMutation: null,
  },
  overflow: {
    x: 'scroll',
    y: 'scroll',
  },
  scrollbars: {
    theme: 'os-theme-dark',
    visibility: 'auto',
    autoHide: 'never',
    autoHideDelay: 1300,
    dragScroll: true,
    clickScroll: false,
    pointers: ['mouse', 'touch', 'pen'],
  },
};
Options in depth

paddingAbsolute

type default
boolean false

Indicates whether the padding for the content shall be absolute.

showNativeOverlaidScrollbars

type default
boolean false

Indicates whether the native overlaid scrollbars shall be visible.

update.elementEvents

type default
Array<[string, string]> | null [['img', 'load']]

An array of tuples. The first value in the tuple is an selector and the second value are event names. The plugin will update itself if any of the elements with the specified selector will emit any specified event. The default value can be interpreted as "The plugin will update itself if any img element emits an load event."

update.debounce

type default
[number, number] | number | null [0, 33]

Note

If 0 is used for the timeout, requestAnimationFrame instead of setTimeout is used for the debounce.

Debounces the MutationObserver which tracks changes to the content. If a tuple is passed, the first value is the timeout and second is the max wait. If only a number is passed you specify only the timeout and there is no max wait. With null there is no debounce.

Sponsors

Thanks to BrowserStack for sponsoring open source projects and letting me test OverlayScrollbars for free.
S
Description
A javascript scrollbar plugin that hides native scrollbars, provides custom styleable overlay scrollbars and keeps the native functionality and feeling.
Readme MIT 115 MiB
Languages
TypeScript 85.3%
JavaScript 8.4%
SCSS 3.7%
HTML 1.6%
Svelte 0.6%
Other 0.3%