Easy to use, super lightweight (only 6KB~ for UMD Module) and fully stylable, customisable JS scrollbar.
Once you define your scrollbar and scrollbar scrollable containers with the same ID, as can be seen in the examples, you can easily place them anywhere on the page and style them to your liking.
You can even add more HTML inside the defined scrollbars and style that too. e.g. icons, backgrounds, extra buttons etc.
npm i stylable-scrollbar
ESM
import initStylableScrollbars from 'stylable-scrollbar'
initStylableScrollbars()
CJS
const initStylableScrollbars = require('stylable-scrollbar')
initStylableScrollbars()
Vanilla JS
<script src="https://unpkg.com/[email protected]/dist/umd/index.umd.js"></script>
<script>
window.initStylableScrollbars()
</script>
Check out a minimal example at https://jasondonnellya.github.io/stylable-scrollbar/ and below is a basic usage example containing two separate scrollbars.
<!-- The stylable-scrollbar-scrollable attribute must have a value which acts as an ID. -->
<div stylable-scrollbar-scrollable="example"></div>
<!-- The stylable-scrollbar attribute must have a matching ID to it's scrollable container as seen above. -->
<div stylable-scrollbar="example" direction="horizontal">
<div stylable-scrollbar-handle></div>
</div>
<div stylable-scrollbar-scrollable="2"></div>
<!-- If a direction is not applied to the scrollbar, vertical is the default. -->
<div stylable-scrollbar="2" direction="vertical">
<div stylable-scrollbar-handle></div>
</div>
(*) Requires a value that acts as an 'id' matching the scrollable container.
(*) Requires a value that acts as an 'id' matching the scrollable container.
The handle of the scrollbar.
The direction which the scollbar scrolls. ("horizontal", "vertical") defaults to "vertical".
Scrollspeed defines what speed the container scrolls at when using the mousewheel. (number) defaults to 1.
The responsive attribute dictates whether a scrollbar should hide when it is not possible to scroll the container. (true, false) defaults to true
It is possible to pass an object that contains settings for stylable-scrollbar as shown.
initStylableScrollbars({ keepContainerScrollbars: true })
This will ignore the CSS that is generated to remove scrollbars from any element with selector [stylable-scrollbar-scrollable] so you can keep the default scrollbars within the containers.
It is easy to target all the built in HTML of stylable-scrollbar using the attribute selector, see below example.
:root {
--width: 300px;
--height: 20px;
}
[stylable-scrollbar-scrollable] {
width: var(--width);
height: var(--width);
padding: 10px;
background: rgb(240, 240, 240);
}
[stylable-scrollbar] {
width: var(--width);
height: var(--height);
background: lightgray;
border: solid transparent 0px;
border-radius: 20px;
/* Add display none here to hide scrollbars before JS has loaded. */
display: none;
}
[stylable-scrollbar-handle] {
width: 40px;
height: 20px;
background: darkgrey;
border: solid transparent 0px;
border-radius: 20px;
box-shadow: 0px 0px 5px 1px black;
}
[stylable-scrollbar] [direction="vertical"] {
width: var(--height);
height: var(--width);
[stylable-scrollbar-handle] {
width: 20px;
height: 40px;
}
}
If you notice any constraints or bugs with this scrollbar, or you would just like to suggest features, don't hesitate to open an issue on the GitHub repository. Thanks!