- Notifications
You must be signed in to change notification settings - Fork0
Minimalistic but perfect custom scrollbar plugin
License
xidedix/perfect-scrollbar
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Minimalistic but perfect custom scrollbar plugin
To read documentation for versions < 1.0, please visitv0.8.1
.
perfect-scrollbar is minimalistic butperfect (for me, and maybe for mostdevelopers) scrollbar plugin.
- No change on design layout
- No manipulation on DOM tree
- Use plain
scrollTop
andscrollLeft
- Scrollbar style is fully customizable
- Efficient update on layout change
I hope you love it!
It's on theGitHub Pages.
The best way to install and use perfect-scrollbar is with npm. It's registeredasperfect-scrollbar.
$ npm install perfect-scrollbar
You can manually download perfect-scrollbarfromReleases.
If you want to use the development version of the plugin, build from sourcemanually. The development version may be unstable.
$ git clone https://github.com/utatti/perfect-scrollbar.git$ cd perfect-scrollbar$ npm install$ npm run build
You can fork the following JSFiddles for testing and experimenting purposes:
Sources not mentioned above are not maintained officially. If there are issuesof the following sources, please ask and resolve in each repository.
The following requirements should meet.
- the container must have a
position
style. - the container must be a normal container element.
The following requirements are included in the basic CSS, but please keep inmind when you'd like to change the CSS files.
- the container must have an
overflow: hidden
css style. - the scrollbar's position must be
absolute
. - the scrollbar-x must have
bottom
ortop
, and the scrollbar-y must haveright
orleft
.
Finally, scroll hooking is generally considered as a bad practice, andperfect-scrollbar should be used carefully. Unless custom scroll is reallyneeded, using browser-native scroll is always recommended.
perfect-scrollbar emulates some scrolls, but not all of the kinds. It alsodoes not workin some situations. You can find these cases inCaveats.Basically, items listed in the caveats are hacky to implement and may not beimplemented in the future. If the features are really needed, please considerusing browser-native scroll.
First of all, please check if the container element meets the requirements andthe main CSS is imported.
<style>#container {position: relative;width:600px;height:400px; }</style><linkrel="stylesheet"href="css/perfect-scrollbar.css">
Import via ES modules:
importPerfectScrollbarfrom'perfect-scrollbar';
Or in browser:
<scriptsrc="dist/perfect-scrollbar.js"></script>
To initialise:
constcontainer=document.querySelector('#container');constps=newPerfectScrollbar(container);// or just with selector stringconstps=newPerfectScrollbar('#container');
It can be initialised withoptions.
constps=newPerfectScrollbar('#container',{wheelSpeed:2,wheelPropagation:true,minScrollbarLength:20});
If the size of your container or content changes, callupdate
.
ps.update();
If you want to destroy the scrollbar, usedestroy
.
ps.destroy();ps=null;// to make sure garbages are collected
If you want to scroll to somewhere, just updatescrollTop
.
constcontainer=document.querySelector('#container');container.scrollTop=0;
You can also get information about how to use the plugin from code inexamples/
.
It is a list of handlers to scroll the element.
Default:['click-rail', 'drag-thumb', 'keyboard', 'wheel', 'touch']
The scroll speed applied to mousewheel event.
Default:1
If this option is true, when the scroll reaches the end of the side, mousewheelevent will be propagated to parent element.
Default:false
If this option is true, swipe scrolling will be eased.
Default:true
When set to an integer value, the thumb part of the scrollbar will not shrinkbelow that number of pixels.
Default:null
When set to an integer value, the thumb part of the scrollbar will not expandover that number of pixels.
Default:null
This sets threashold forps--scrolling-x
andps--scrolling-y
classes toremain. In the default CSS, they make scrollbars shown regardless of hoverstate. The unit is millisecond.
Default:1000
When set to true, and only one (vertical or horizontal) scrollbar is visiblethen both vertical and horizontal scrolling will affect the scrollbar.
Default:false
When set to true, the scroll bar in X axis will not be available, regardless ofthe content width.
Default:false
When set to true, the scroll bar in Y axis will not be available, regardless ofthe content height.
Default:false
The number of pixels the content width can surpass the container width withoutenabling the X axis scroll bar. Allows some "wiggle room" or "offset break", sothat X axis scroll bar is not enabled just because of a few pixels.
Default:0
The number of pixels the content height can surpass the container height withoutenabling the Y axis scroll bar. Allows some "wiggle room" or "offset break", sothat Y axis scroll bar is not enabled just because of a few pixels.
Default:0
perfect-scrollbar dispatches custom events.
container.addEventListener('ps-scroll-x',()=> ...);
This event fires when the y-axis is scrolled in either direction.
This event fires when the x-axis is scrolled in either direction.
This event fires when scrolling upwards.
This event fires when scrolling downwards.
This event fires when scrolling to the left.
This event fires when scrolling to the right.
This event fires when scrolling reaches the start of the y-axis.
This event fires when scrolling reaches the end of the y-axis (useful forinfinite scroll).
This event fires when scrolling reaches the start of the x-axis.
This event fires when scrolling reaches the end of the x-axis.
You can also watch the reach state via thereach
property.
constps=newPerfectScrollbar(...);console.log(ps.reach.x);// => 'start' or 'end' or nullconsole.log(ps.reach.y);// => 'start' or 'end' or null
If you have any idea to improve this project or any problem using this, pleasefeel free to upload anissue.
For common problems, there is aFAQ wikipage. Please check the page before uploading an issue.
Also, the project is not actively maintained. No maintainer is paid, and most ofus are busy on our professional or personal works. Please understand that it maytake a while for an issue to be resolved. Uploading a PR would be the fastestway to fix an issue.
The plugin is developed to work in modern MS browsers including Edge and IE11,but may have some issues in IE11 mainly because of IE rendering bug concerningsync update on scroll properties. The problem is mentioned inCaveats too.
IE<11 is not supported, and patches to fix problems in IE<=10 will not beaccepted. When old IEs should be supported, please fork the project and makemodification locally.
About
Minimalistic but perfect custom scrollbar plugin
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- JavaScript92.0%
- CSS6.4%
- Shell1.6%