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:
<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>
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.
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).
<!-- 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>
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.
You can initialize OverlayScrollbars with an initial set of options, which can be changed at any time with the options method:
OverlayScrollbars(document.querySelector('#myElement'), { overflow: { x: 'hidden', }, });
The default options are:
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, autoHideSuspend: false, dragScroll: true, clickScroll: false, pointers: ['mouse', 'touch', 'pen'], }, };
paddingAbsolute| type | default |
|---|---|
boolean | false |
Indicates whether the padding for the content should be absolute.
showNativeOverlaidScrollbars| type | default |
|---|---|
boolean | false |
Indicates whether the native overlaid scrollbars should 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 emits any of the specified events. The default value can be interpreted as "The plugin will update itself if any img element emits a load event."
update.debounce| type | default |
|---|---|
[number, number] | number | null | [0, 33] |
Note: If 0 is used for the timeout,
requestAnimationFramewill be used instead ofsetTimeoutfor 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 it is treated as the timeout and there is no max wait. With null there is no debounce. Useful to fine-tune performance.
update.attributes| type | default |
|---|---|
string[] | null | null |
Note: There is a base array of attributes that the
MutationObserveralways observes, even if this option isnull.
An array of additional attributes that the MutationObserver should observe the content for.
update.ignoreMutation| type | default |
|---|---|
((mutation) => any) | null | null |
A function which receives a MutationRecord as an argument. If the function returns a truthy value the mutation will be ignored and the plugin won't update. Useful to fine-tune performance.
overflow.x| type | default |
|---|---|
string | 'scroll' |
Note: Valid values are:
'hidden','scroll','visible','visible-hidden'and'visible-scroll'.
The overflow behavior for the horizontal (x) axis.
overflow.y| type | default |
|---|---|
string | 'scroll' |
Note: Valid values are:
'hidden','scroll','visible','visible-hidden'and'visible-scroll'.
The overflow behavior for the vertical (y) axis.
scrollbars.theme| type | default |
|---|---|
string | null | 'os-theme-dark' |
Applies the specified theme (classname) to the scrollbars.
scrollbars.visibility| type | default |
|---|---|
string | 'auto' |
Note: Valid values are:
'visible','hidden', and'auto'.
The visibility of a scrollbar if its scroll axis is able to have a scrollable overflow. (Scrollable overflow for an axis is only possible with the overflow behavior set to 'scroll' or 'visible-scroll').
scrollbars.autoHide| type | default |
|---|---|
string | 'never' |
Note: Valid values are:
'never','scroll','leave'and'move'.
Dictates whether to hide visible scrollbars automatically after a certain user action.
scrollbars.autoHideDelay| type | default |
|---|---|
number | 1300 |
The delay in milliseconds before the scrollbars are automatically hidden.
scrollbars.autoHideSuspend| type | default |
|---|---|
boolean | false |
Suspend the autoHide functionality until the first scroll interaction is performed.
The default value for this option is false for backwards compatibility reasons but is recommended to be true for better accessibility.
scrollbars.dragScroll| type | default |
|---|---|
boolean | true |
Indicates whether you can drag the scrollbar handles for scrolling.
scrollbars.clickScroll| type | default |
|---|---|
boolean | 'instant' | false |
Note: If set to
truethe ClickScrollPlugin is required.
Indicates whether you can click on the scrollbar track for scrolling.
scrollbars.pointers| type | default |
|---|---|
string[] | null | ['mouse', 'touch', 'pen'] |
The PointerTypes the plugin should react to.
// The options of a OverlayScrollbars instance. type Options = { // Whether the padding should be absolute. paddingAbsolute: boolean; // Whether to show the native scrollbars. Has effect only if the native scrollbars are overlaid. showNativeOverlaidScrollbars: boolean; // Customizes the automatic update behavior. update: { /** * The given Event(s) from the elements with the given selector(s) will trigger an update. * Useful for everything the MutationObserver and ResizeObserver can't detect * e.g.: An image's `load` event or the `transitionend` / `animationend` events. */ elementEvents: Array<[elementSelector: string, eventNames: string]> | null; /** * The debounce which is used to detect content changes. * If a tuple is provided you can customize the `timeout` and the `maxWait` in milliseconds. * If a single number customizes only the `timeout`. *


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对 每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号