You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
8.8 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. # perfect-scrollbar
  2. Minimalistic but perfect custom scrollbar plugin
  3. [![npm](https://img.shields.io/npm/v/perfect-scrollbar.svg)](https://www.npmjs.com/package/perfect-scrollbar)
  4. [![Travis CI](https://travis-ci.org/utatti/perfect-scrollbar.svg?branch=master)](https://travis-ci.org/utatti/perfect-scrollbar)
  5. ***To read documentation for versions < 1.0, please visit [`v0.8.1`](https://github.com/utatti/perfect-scrollbar/tree/0.8.1).***
  6. ## Why perfect-scrollbar?
  7. perfect-scrollbar is minimalistic but *perfect* (for me, and maybe for most
  8. developers) scrollbar plugin.
  9. * No change on design layout
  10. * No manipulation on DOM tree
  11. * Use plain `scrollTop` and `scrollLeft`
  12. * Scrollbar style is fully customizable
  13. * Efficient update on layout change
  14. I hope you love it!
  15. ## Demo
  16. It's on the [GitHub Pages](http://utatti.github.io/perfect-scrollbar/).
  17. ## Table of Contents
  18. * [Install](#install)
  19. * [Before using perfect-scrollbar](#before-using-perfect-scrollbar)
  20. * [Caveats](#caveats)
  21. * [How to use](#how-to-use)
  22. * [Options](#options)
  23. * [Events](#events)
  24. * [Helpdesk](#helpdesk)
  25. * [IE Support](#ie-support)
  26. * [License](#license)
  27. ## Install
  28. #### npm
  29. The best way to install and use perfect-scrollbar is with npm. It's registered
  30. as [perfect-scrollbar](https://www.npmjs.com/package/perfect-scrollbar).
  31. ```
  32. $ npm install perfect-scrollbar
  33. ```
  34. #### Manual download
  35. You can manually download perfect-scrollbar
  36. from [Releases](https://github.com/utatti/perfect-scrollbar/releases).
  37. #### From sources
  38. If you want to use the development version of the plugin, build from source
  39. manually. The development version may be unstable.
  40. ```
  41. $ git clone https://github.com/utatti/perfect-scrollbar.git
  42. $ cd perfect-scrollbar
  43. $ npm install
  44. $ npm run build
  45. ```
  46. #### JSFiddle
  47. You can fork the following JSFiddles for testing and experimenting purposes:
  48. * [perfect-scrollbar JSFiddle](https://jsfiddle.net/utatti/dyvL31r6/)
  49. #### Unofficial sources
  50. Sources not mentioned above are not maintained officially. If there are issues
  51. of the following sources, please ask and resolve in each repository.
  52. ## Before using perfect-scrollbar
  53. The following requirements should meet.
  54. * the container must have a `position` style.
  55. * the container must be a normal container element.
  56. The following requirements are included in the basic CSS, but please keep in
  57. mind when you'd like to change the CSS files.
  58. * the container must have an `overflow: hidden` css style.
  59. * the scrollbar's position must be `absolute`.
  60. * the scrollbar-x must have `bottom` or `top`, and the scrollbar-y must have
  61. `right` or `left`.
  62. Finally, scroll hooking is generally considered as a bad practice, and
  63. perfect-scrollbar should be used carefully. Unless custom scroll is really
  64. needed, using browser-native scroll is always recommended.
  65. ## Caveats
  66. perfect-scrollbar emulates some scrolls, but not all of the kinds. It also *does not* work
  67. in some situations. You can find these cases in [Caveats](https://github.com/utatti/perfect-scrollbar/wiki/Caveats).
  68. Basically, items listed in the caveats are hacky to implement and may not be
  69. implemented in the future. If the features are really needed, please consider
  70. using browser-native scroll.
  71. ## How to use
  72. First of all, please check if the container element meets the requirements and
  73. the main CSS is imported.
  74. ```html
  75. <style>
  76. #container {
  77. position: relative;
  78. width: 600px;
  79. height: 400px;
  80. }
  81. </style>
  82. <link rel="stylesheet" href="css/perfect-scrollbar.css">
  83. ```
  84. Import via ES modules:
  85. ```js
  86. import PerfectScrollbar from 'perfect-scrollbar';
  87. ```
  88. Or in browser:
  89. ```html
  90. <script src="dist/perfect-scrollbar.js"></script>
  91. ```
  92. To initialise:
  93. ```js
  94. const container = document.querySelector('#container');
  95. const ps = new PerfectScrollbar(container);
  96. // or just with selector string
  97. const ps = new PerfectScrollbar('#container');
  98. ```
  99. It can be initialised with [options](#options).
  100. ```js
  101. const ps = new PerfectScrollbar('#container', {
  102. wheelSpeed: 2,
  103. wheelPropagation: true,
  104. minScrollbarLength: 20
  105. });
  106. ```
  107. If the size of your container or content changes, call `update`.
  108. ```js
  109. ps.update();
  110. ```
  111. If you want to destroy the scrollbar, use `destroy`.
  112. ```js
  113. ps.destroy();
  114. ps = null; // to make sure garbages are collected
  115. ```
  116. If you want to scroll to somewhere, just update `scrollTop`.
  117. ```js
  118. const container = document.querySelector('#container');
  119. container.scrollTop = 0;
  120. ```
  121. You can also get information about how to use the plugin from code in [`examples/`](examples).
  122. ## Options
  123. ### `handlers {String[]}`
  124. It is a list of handlers to scroll the element.
  125. **Default**: `['click-rail', 'drag-thumb', 'keyboard', 'wheel', 'touch']`
  126. ### `wheelSpeed {Number}`
  127. The scroll speed applied to mousewheel event.
  128. **Default**: `1`
  129. ### `wheelPropagation {Boolean}`
  130. If this option is true, when the scroll reaches the end of the side, mousewheel
  131. event will be propagated to parent element.
  132. **Default**: `false`
  133. ### `swipeEasing {Boolean}`
  134. If this option is true, swipe scrolling will be eased.
  135. **Default**: `true`
  136. ### `minScrollbarLength {Number?}`
  137. When set to an integer value, the thumb part of the scrollbar will not shrink
  138. below that number of pixels.
  139. **Default**: `null`
  140. ### `maxScrollbarLength {Number?}`
  141. When set to an integer value, the thumb part of the scrollbar will not expand
  142. over that number of pixels.
  143. **Default**: `null`
  144. ### `scrollingThreshold {Number}`
  145. This sets threashold for `ps--scrolling-x` and `ps--scrolling-y` classes to
  146. remain. In the default CSS, they make scrollbars shown regardless of hover
  147. state. The unit is millisecond.
  148. **Default**: `1000`
  149. ### `useBothWheelAxes {Boolean}`
  150. When set to true, and only one (vertical or horizontal) scrollbar is visible
  151. then both vertical and horizontal scrolling will affect the scrollbar.
  152. **Default**: `false`
  153. ### `suppressScrollX {Boolean}`
  154. When set to true, the scroll bar in X axis will not be available, regardless of
  155. the content width.
  156. **Default**: `false`
  157. ### `suppressScrollY {Boolean}`
  158. When set to true, the scroll bar in Y axis will not be available, regardless of
  159. the content height.
  160. **Default**: `false`
  161. ### `scrollXMarginOffset {Number}`
  162. The number of pixels the content width can surpass the container width without
  163. enabling the X axis scroll bar. Allows some "wiggle room" or "offset break", so
  164. that X axis scroll bar is not enabled just because of a few pixels.
  165. **Default**: `0`
  166. ### `scrollYMarginOffset {Number}`
  167. The number of pixels the content height can surpass the container height without
  168. enabling the Y axis scroll bar. Allows some "wiggle room" or "offset break", so
  169. that Y axis scroll bar is not enabled just because of a few pixels.
  170. **Default**: `0`
  171. ## Events
  172. perfect-scrollbar dispatches custom events.
  173. ```js
  174. container.addEventListener('ps-scroll-x', () => ...);
  175. ```
  176. ### `ps-scroll-y`
  177. This event fires when the y-axis is scrolled in either direction.
  178. ### `ps-scroll-x`
  179. This event fires when the x-axis is scrolled in either direction.
  180. ### `ps-scroll-up`
  181. This event fires when scrolling upwards.
  182. ### `ps-scroll-down`
  183. This event fires when scrolling downwards.
  184. ### `ps-scroll-left`
  185. This event fires when scrolling to the left.
  186. ### `ps-scroll-right`
  187. This event fires when scrolling to the right.
  188. ### `ps-y-reach-start`
  189. This event fires when scrolling reaches the start of the y-axis.
  190. ### `ps-y-reach-end`
  191. This event fires when scrolling reaches the end of the y-axis (useful for
  192. infinite scroll).
  193. ### `ps-x-reach-start`
  194. This event fires when scrolling reaches the start of the x-axis.
  195. ### `ps-x-reach-end`
  196. This event fires when scrolling reaches the end of the x-axis.
  197. You can also watch the reach state via the `reach` property.
  198. ```js
  199. const ps = new PerfectScrollbar(...);
  200. console.log(ps.reach.x); // => 'start' or 'end' or null
  201. console.log(ps.reach.y); // => 'start' or 'end' or null
  202. ```
  203. ## Helpdesk
  204. If you have any idea to improve this project or any problem using this, please
  205. feel free to upload an [issue](https://github.com/utatti/perfect-scrollbar/issues).
  206. For common problems, there is a [FAQ](https://github.com/utatti/perfect-scrollbar/wiki/FAQ) wiki
  207. page. Please check the page before uploading an issue.
  208. Also, the project is not actively maintained. No maintainer is paid, and most of
  209. us are busy on our professional or personal works. Please understand that it may
  210. take a while for an issue to be resolved. Uploading a PR would be the fastest
  211. way to fix an issue.
  212. ## IE Support
  213. The plugin is developed to work in modern MS browsers including Edge and IE11,
  214. but may have some issues in IE11 mainly because of IE rendering bug concerning
  215. sync update on scroll properties. The problem is mentioned in
  216. [Caveats](https://github.com/utatti/perfect-scrollbar/wiki/Caveats) too.
  217. IE<11 is not supported, and patches to fix problems in IE<=10 will not be
  218. accepted. When old IEs should be supported, please fork the project and make
  219. modification locally.
  220. ## License
  221. [MIT](LICENSE)