Perfect Scrollbar

How to use

Before using

The following requirements should be met:

  • the container must have a position style.
  • the container must be a normal container element.

Those requirements are met in the CSS in the package, but please keep in mind 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 or top, and the scrollbar-y must have right or left.

Finally, scroll hooking is generally considered a bad practice, and perfect-scrollbar should be used carefully. Unless custom scroll is really needed, using browser-native scroll is always recommended. If you want to diversify your perfect scrollbar, you can combine it with information about the scroll status.

Additionally, with Perfect Scrollbar you can use a component with Scroll back to the top option

Caveats

Perfect-scrollbar emulates some scrolls, but not all of the kinds. It also does not work in some situations. You can find these cases in Caveats. Basically, items listed in the caveats are hacky to implement and may not be implemented in the future. If the features are really needed, please consider using browser-native scroll.

How to use

First of all, please check if the container element meets the requirements and the main CSS is imported.

                            
                                <style>
                                    #container {
                                      position: relative;
                                      width: 600px;
                                      height: 400px;
                                    }
                                </style>
                                <!-- <link rel="stylesheet" href="css/perfect-scrollbar.css"> -->
                            
                        

Import via ES modules:

                            
                                import PerfectScrollbar from 'perfect-scrollbar';
                            
                        

Or in browser:

                            
                                <!-- <script src="dist/perfect-scrollbar.js"></script> -->
                            
                        

To initialise:

                            
                                const container =
                                document.querySelector('#container');
                                const ps = new PerfectScrollbar(container);
    
                                // or just with selector string
                                const ps = new PerfectScrollbar('#container');
                            
                        

It can be initialized with options.

                            
                                const ps = new
                                PerfectScrollbar('#container', {
                                wheelSpeed: 2,
                                wheelPropagation: true,
                                minScrollbarLength: 20
                                });
                            
                        

If the size of your container or content changes, call update.

                            
                                ps.update();
                            
                        

If you want to destroy the scrollbar, use destroy.

                            
                                ps.destroy();
                                ps = null; // to make sure garbages are collected
                            
                        

If you want to scroll to somewhere, just update scrollTop.

                            
                                const container = document.querySelector('#container');
                                container.scrollTop = 0;
                            
                        

You can also get information about how to use the plugin from code in examples.

You can also add a simple customized Scrollbar to your project