Use-Scrollbar enables a component relies on a native scrollbar to replace its native scrollbar with a virtual scrollbar instead, NOT virtual scroll.
Installations
npm install use-scrollbars
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Min. Node Version
>=16
Node Version
18.17.0
NPM Version
9.6.7
Score
57.2
Supply Chain
96.8
Quality
75.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
Vue (67.38%)
TypeScript (25.53%)
Less (2.24%)
JavaScript (2.23%)
CSS (1.6%)
HTML (0.83%)
Shell (0.19%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Lionad-Morotar
Download Statistics
Total Downloads
2,167
Last Day
1
Last Week
2
Last Month
47
Last Year
453
GitHub Statistics
MIT License
12 Stars
111 Commits
1 Forks
1 Watchers
3 Branches
1 Contributors
Updated on Feb 19, 2025
Package Meta Information
Latest Version
0.4.11
Package Id
use-scrollbars@0.4.11
Unpacked Size
94.79 kB
Size
24.52 kB
File Count
22
NPM Version
9.6.7
Node Version
18.17.0
Published on
Dec 19, 2023
Total Downloads
Cumulative downloads
Total Downloads
2,167
Last Day
0%
1
Compared to previous day
Last Week
-33.3%
2
Compared to previous week
Last Month
104.3%
47
Compared to previous month
Last Year
-63.3%
453
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Peer Dependencies
1
Dev Dependencies
37
use-scrollbar
Use-Scrollbar
enables a component relies on a native scrollbar to replace its native scrollbar with a virtual scrollbar instead, NOT virtual scroll.
🎇 Brief
Assuming a 400px height div, you can easily get a div with beautiful virtual scrollbars by simply wrap the div with ElementPlus Scrollbar scrollbars. But none of the popular scrollbar components provide an interface for handling complex elements, that is to say, you cant wrap an complex component with ElementPlus Scrollbar to have its internal native scrollbar replaced with beautiful virtual scrollbars. So you need use-scrollbar
.
⚒️ Feature
- Powerful API, have ability to deal with complex components1, such as vxe-table、ant-vue-table
- Customisable, so that you can create your own scrollbar style, animation and user interaction
- Theme, integrated with these style configurations: ElementPlus, Steam, CSS-Tricks ...
- Full Typed, with the power of typescript
- Support Vue3 &
Vue2 - WIP
Vue Directives - WIP
Headless Component - WIP
Github pages for document and preview
and PRs are welcom
📸 Preview
1. Native Scrollbar VS Customized Scrollbar (theme: default)
2. Native Scrollbar VS Customized Scrollbar (theme: css-tricks)
🤹♀️ Usage
Simple example
1import { onMounted, ref } from 'vue' 2import { useSrollbar } from 'use-scrollbars' 3 4const componentOrElementRef = ref(null); 5const barStates = useScrollbar(componentOrElementRef, { 6 // override default options 7}); 8 9// dont forget to import style.css 10// in your main.ts (or entry.ts anyway) 11import "use-scrollbars/dist/style.css"
Another example
1import { watchEffect, onMounted, ref } from 'vue' 2import { useSrollbar } from 'use-scrollbars' 3 4const componentOrElementRef = ref(null); 5const barStates = useScrollbar(componentOrElementRef, { 6 // override default options 7}); 8 9const componentStates = ref('your-states'); 10watchEffect(() => { 11 if (componentStates.valu3 === 'your-states') { 12 barStates.theme = 'steam' 13 } else { 14 barStates.destroy() 15 } 16})
More example on pnpm dev
📦 Install
1pnpm install use-scrollbars
🗂️ Document
1. States
1.1. barStates.theme
改变滚动条样式。
1const theme = 'normal' // 'normal' | 'steam' | 'css-tricks' 2barStates.theme = theme
1.2. barStates.offset
改变滚动条相对挂载元素的偏移量。
1barStates.offset.y.top = 10 // px 2barStates.offset.y.right = 10 // px 3barStates.offset.x.left = 5 // px 4barStates.offset.x.bottom = 5 // px
1.3. barStates.scrollTop
如果传入多个 wrapper,那么 scrollTop 属性等同于这几个 wrapper 对应 DOM 元素的最大的哪个 scrollTop 属性。如果需要滚动 wrapper 中的内容,可以给 scrollTop 设置值,也可以使用 barStates.scrollTo 方法。
1.4. barStates.scrollLeft
1.5. barStates.isDragging
判断当前滚动条是否出于拖动状态。
1console.log(barStates.isDragging.y)
1.6. barStates.isScrolling
获取当前滚动区域的滚动状态。
1console.log(barStates.isScrolling.x)
2. Actions
2.1. barStates.init
如果不是通过显式初始化(即 useScrollbar(elem)
)的方式自动初始化滚动条,那么需要使用 init 方法手动初始化。init 方法提供了对控制滚动区(甚至多个滚动区)所需要的更细致的参数。
1const $parent = cmptRef.value.$el.parentElement; 2const $wrapper = $parent.querySelector(".content-wrapper"); 3const $content = $parent.querySelector(".content"); 4 5// 详细 API 见类型文档 6barStates.init({ 7 mount: cmptRef.value, 8 content: [$content], 9 // 可以不传,默认为 content 的 上一级父元素 10 wrapper: [$wrapper], 11 // 可以不传,默认为 wrapper 或 wrapper 的第一个元素 12 viewport: $wrapper, 13})
2.2. barStates.visibleOnHover
监听传入元素的鼠标事件,mouseenter 时显示滚动条,mouseleave 时隐藏滚动条。
2.3. barStates.setOffset
根据传入元素的尺寸自动设置滚动条的偏移量。在某些场景非常有用,比如你想改变一个弹窗,其滚动区域为整个弹窗内容区域,但是内容区填充了一个 position:sticky
头部,此时,如果将滚动条直接挂载到弹窗的内容区域,那么 y 轴滚动条的上方偏移量应为头部的高度。你可以在 barStates.setOffset 中传入此头部元素或组件,动态跟踪其高度并自动设置偏移量。
1const cmptOrElemRef = ref(null); 2 3barStates.setOffset({ 4 y: { 5 top: cmptOrElemRef, 6 } 7})
3. Hooks
3.1 useScrollbar
等同于 useScrollbars,用于将一个已有的滚动系统的原生滚动条替换为虚拟滚动条,也是这个库最主要的功能。
3.2 useNativeScrollbar
获取原生滚动条相关的一些信息,如宽度。
1const nativeBar = useNativeScrollbar() 2 3console.log(nativeBar.thick) // usually 17px in Windows
🚩 Dev
1pnpm install 2pnpm serve
近期开发路线:接下来会看一下性能方面的优化,以及如何在 Vue2/Vue3 中通用。
📄 License
MIT License
Footnotes
-
which is not possible with other libraries ↩

No vulnerabilities found.

No security vulnerabilities found.
Gathering detailed insights and metrics for use-scrollbars