Vue2Native Scrollbar
Vue 2 directive for custom scrollbar that uses native scroll behavior.
Lightweight, performant, customizable and without dependencies.
About
The first Vue 2 custom scrollbar library that only enhances scrolling instead of reimplementing it with custom scroll behavior.
It uses native scroll events to detect and synchronize scrollbar position. This makes possible to hack into the native events of scrolling element without hassle.
It was created initially for GGather.com where it plays nicely with a lot of dynamic content changes and most importantly - with infinite scrolling. And now after testing, fixes, improvements and added customization options - it's ready to be shared with the world.
- Directive instead custom component, which saves a lot of headaches.
- Native scroll events, no jankiness, no hijacking.
- Simple to use, lightweight & performant.
- Works in browser and also build (webpack, etc.) environments.
- Compatible with all major browsers including IE9 and above.
- Useful customization options.
- No 3rd party dependencies.
- Tested in production.
Installation
NPM
npm install vue-native-scrollbar --save
CDN
https://unpkg.com/vue-native-scrollbar
Manual
Download the .js file directly from latest commit and include manually in your project
vue-native-scrollbar.jsUsage
Basic Markup
<div v-scrollbar> <!-- el1 -->
<div> <!-- el2 -->
<!-- your scrollable content -->
</div>
<!-- dragger will be automatically added here -->
</div>
Markup with example options
<div v-scrollbar="{
preventParentScroll: true,
scrollThrottle: 30,
}"> <!-- el1 -->
<div> <!-- el2 -->
<!-- your scrollable content -->
</div>
<!-- dragger will be automatically added here -->
</div>
How It Works
Every Vue Native Scrollbar (referenced further as VNS) scrollable content needs to be wrapped in two elements. First outermost parent element el1
hides the native browser scrollbar of the second parent element el2
and it also contains the custom scrollbar element (referenced further as dragger
) which gets appended automatically on the VNS initialization.
The VNS internals listen to el2
scroll event and synchronize dragger
position when you scroll. And vice versa - when you directly use dragger
it listens to mouse events to detect the position of dragger and sets scroll position of el2
.
Other than setting the scroll position when you use dragger
the VNS doesn't interfere at all with native browser scrolling and you can listen to scroll/wheel/etc. events as you would normally do.
This also means the scroll behavior will remain unchanged. You'll still be able to use mouse wheel click scrolling or touch scrolling with momentum. It doesn't try to reimplement the scrolling from scratch like most of similar libraries try (with bad results).
Gotchas
-
Element
el1
has programmatically added stylesposition: relative
andoverflow: hidden
so you can't change them without using!important
. But you shouldn't do it anyway - your scrollbar will break. -
Element
el2
has programmatically added stylesoverflow-x: hidden
,overflow-y: scroll
,height: 100%
,-ms-overflow-style: scrollbar
andwidth
which value depends on browser. -
Similar thing with
dragger
- it has programmatically added stylesposition: absolute
andheight
withtop
that have values in pixels relative to scroll position. -
Sometimes in rare cases the scrollbar won't refresh on child components update. In such cases please manually use
$forceUpdate
(in the scope of component containing VNS) to refresh scrollbar. -
Right now VNS doesn't support loading detection of img elements so either set width and height properties of image from beginning or manually detect load events and
$forceUpdate
component to refresh scrollbar. Pull requests welcome.
Customization options
Name | Value Type |
Value Default |
Description |
---|---|---|---|
preventParentScroll | Boolean | false | Prevent parent taking over scrolling after reaching bottom or top of VNS element. This feature is in beta - right now it supports only detecting mouse wheel event and doesn't support touch scrolling. Pull requests welcome. |
unselectableBody | Boolean | true | Disable user select on body when using dragger. If this value is false then user select will be only disabled on vue native scrollbar element. |
scrollThrottle | Integer | 10 | How frequently in ms should dragger position calculations take place on scroll event. Setting this above 30 will make dragger position refresh choppy, but may help with performance in extreme situations. |
draggerThrottle | Integer | 10 | How frequently in ms should scroll position update when using dragger. Setting this above 30 will make scroll synchronization choppy, but may help with performance in extreme situations. |
resizeRefresh | Boolean | true | Refresh dragger positions on window resize. |
resizeDebounce | Integer | 100 | Debounce value in ms controlling how frequently resize refresh is fired. Relevant only if resizeRefresh is enabled. |
scrollingPhantomDelay | Integer | 1000 | How long in ms should phantom scrolling class stay applied to element. |
draggingPhantomDelay | Integer | 1000 | How long in ms should phantom dragging class stay applied to element. Warning - setting this below value of scrollThrottle may have some unforeseen effects. |
Customization options (custom classes)
Name | Value Type |
Value Default |
Description |
---|---|---|---|
el1Class | String | 'vns' | Element 1 class. |
el1ScrollVisibleClass | String | 'vns-visible' | Added dynamically when the scrollbar is visible. |
el1ScrollInvisibleClass | String | 'vns-invisible' | Added dynamically when the scrollbar is invisible - there's nothing to scroll. |
el1ScrollingClass | String | 'vns-scrolling' | Added dynamically on scrolling. |
el1ScrollingPhantomClass | String | 'vns-scrolling-phantom' | Added dynamically on scrolling and removed after value of scrollingPhantomDelay. |
el1DraggingClass | String | 'vns-dragging' | Added dynamically when the dragger is dragged or pressed. |
el1DraggingPhantomClass | String | 'vns-dragging-phantom' | Added dynamically when the dragger is dragged or pressed and removed after value of draggingPhantomDelay. |
draggerClass | String | 'vns-dragger' | Dragger class. |
draggerStylerClass | String | 'vns-dragger-styler' | Dragger styler class. |
Styling
How to Style Scrollbar
Default Style
If you were looking for this library it means you want to style your scrollbars as you want - so VNS doesn't include default styles internally. But if you e.g. want to quickly try or understand how you should style VNS scrollbars then below are prepared default styles that you can copy-paste into your project.
.vns > .vns-dragger {
z-index: 2;
width: 12px;
right: 0;
}
.vns > .vns-dragger > .vns-dragger-styler {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform: rotate3d(0,0,0,0);
transform: rotate3d(0,0,0,0);
-webkit-transition:
background-color 100ms ease-out,
margin 100ms ease-out,
height 100ms ease-out;
transition:
background-color 100ms ease-out,
margin 100ms ease-out,
height 100ms ease-out;
background-color: rgba(48, 121, 244,.1);
margin: 5px 5px 5px 0;
border-radius: 20px;
height: calc(100% - 10px);
display: block;
}
.vns.vns-scrolling-phantom > .vns-dragger > .vns-dragger-styler {
background-color: rgba(48, 121, 244,.3);
}
.vns > .vns-dragger:hover > .vns-dragger-styler {
background-color: rgba(48, 121, 244,.5);
margin: 0px;
height: 100%;
}
.vns.vns-dragging > .vns-dragger > .vns-dragger-styler {
background-color: rgba(48, 121, 244,.5);
margin: 0px;
height: 100%;
}
.vns.vns-dragging-phantom > .vns-dragger > .vns-dragger-styler {
background-color: rgba(48, 121, 244,.5);
}
Examples
Other than examples below - this site itself is an example of Vue Native Scrollbar usage. If you look to the right it has a custom white scrollbar that showcases itself pretty nicely.
Default without options
But that didn’t explain the separate tracks along which the volcanoes formed.
Enabled preventParentScroll
Choppy - scrollThrottle
and draggerThrottle
set to 100
Scrollbar invisible
Contributing & Credits
Contributing
There's still a lot that can be introduced to Vue Native Scrollbar. If you have willingness you can help out on GitHub with the issues below.
- Option to dynamically enable/disable scrollbar.
- Support for compatibility of Vue Native Scrollbar element classes with component scoped selectors.
- Support for scrollbar refresh on dynamic content (mainly img) load (MutationObserver?).
- Support for scrollbar refresh on orientationchange event.
- Support for dragging dragger using touch events.
-
preventParentScroll
support for touch scrolling. - Support for horizontal scrolling.
- Issue of scrollbar sometimes not refreshing on child components updates.
Credits
Created by Dominik Serafin for GGather.com.
Inspired by nanoScroller.