Reference
API
One class, one options object, five methods. Everything below is the real behaviour of the library.
Options
Pass them to the constructor, or change them later with update(newOptions).
| Option | Default | Description |
|---|---|---|
| element | required | Selector string or HTMLElement. 'body' or document.body enables page mode. |
| mode | 'native' | 'native', 'css' or 'simulated'. |
| axis | 'y' | 'y', 'x' or 'both'. |
| style | 'pill' | 'pill', 'thin' or 'rail' (visible track). |
| autoHide | false | Show the track only while scrolling, hovering or dragging. |
| autoHideDelay | 800 | Milliseconds before the track fades out. |
| width | 8 | Track thickness in px. |
| minThumbSize | 32 | Smallest thumb length in px. |
| thumbColor | null | Thumb color. null uses the CSS variable --scrollbar-thumb. |
| trackColor | null | Track color. null uses --scrollbar-track. |
| lerp | 0.1 | Easing factor from 0 to 1 for simulated wheel scrolling and for animated scrollTo(..., true) in every mode. |
| disableTouch | true | Do nothing on touch devices, which already have overlay bars. |
| baseClass | 'c--scrollbar-a' | BEM block for the container, content, tracks and thumbs. |
| activeClass | 'c--scrollbar-a--is-active' | Added while scrolling. |
| draggingClass | 'c--scrollbar-a--second' | Added while a thumb is dragged. |
| onInit | null | (el) => void, called once after setup. |
| onScroll | null | ({ x, y, progressX, progressY }, el) => void. Progress goes from 0 to 1. |
| onDestroy | null | (el) => void, called by destroy(). |
Methods
| Method | Description |
|---|---|
| update(newOptions) | Merge new options and re-measure. Call update({}) after the content size changes. element and baseClass are fixed at construction. |
| scrollTo({ x, y }, smooth) | Scroll to a position. With smooth the movement is eased. |
| disable() | Pause the custom behaviour and restore the native bar. |
| enable() | Resume after disable(). |
| destroy() | Remove tracks and listeners, restore the native bar, then call onDestroy. The layout style stays, so __content keeps overflow: auto. |
Markup
A container with the c--scrollbar-a class and one c--scrollbar-a__content child. The container needs a height or max-height (or a width, for axis: 'x'). The injected layout rules for the container and __content use :where(), so your own CSS on those classes wins. The library creates one __overlay per axis with data-scrollbar-axis="y" or "x" (and data-scrollbar-page in page mode), and inside it an __overlay__item that is the thumb; the thumb is aria-hidden. The bar rules key on the attribute, so to move or restyle a bar write .my-list > .c--scrollbar-a__overlay[data-scrollbar-axis='y']; the plain __overlay class is too weak to override width, height, position or background. The attribute form beats the base bar rules but only ties with the thin, rail, theme and corner-inset rules: load your rule after the library styles, or add the host attribute (.my-list[data-scrollbar-style] > ...). The default stylesheet is optional.
Page mode
With element: 'body' there is no markup to write. The library listens to window, measures document.scrollingElement, hides the system bar on html and shows a fixed overlay track that does not reserve space. destroy() restores the system bar. Safari on iOS and other touch devices keep their overlay bars, so page mode does nothing there.
Install
Three ways to get the look: the compiled CSS with variables, the SCSS partial, or the minimum rules pasted into your own file. Each tab has the steps, an example you can paste, and how to check that it works. This is the same guide as the Styling setup section of the readme.
Styling
Pick the SCSS blocks and a theme you want, and copy the result as the partial, as standalone nested SCSS, or as compiled CSS variables.
Compatibility
Each missing feature degrades as listed below. Touch devices are the case where the native bar is simply left alone.
| Feature | When it is missing |
|---|---|
| ResizeObserver | Sizes are recalculated on resize and on update({}). |
| scrollbar-width / scrollbar-color | css mode keeps the native bar and styles it with ::-webkit-scrollbar rules, which live in style.css. |
| Touch devices | Inert by default (disableTouch: true). |
| prefers-reduced-motion | simulated turns off wheel easing and scrolls natively. |
| Server-side rendering | Importing is safe. window and document are only used in the constructor. |
Playground
Change any option and watch one live instance pick it up through update(), with the matching constructor call written out below.