Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

405 lines
12 KiB

  1. # `perfect-scrollbar`
  2. Minimalistic but perfect custom scrollbar plugin
  3. [![Travis CI](https://travis-ci.org/noraesae/perfect-scrollbar.svg?branch=master)](https://travis-ci.org/noraesae/perfect-scrollbar)
  4. If you want information of old versions<0.6.0, please refer to
  5. [an old documentation](https://github.com/noraesae/perfect-scrollbar/tree/0.5.9).
  6. ## Why perfect-scrollbar?
  7. I was once working on a personal project, and trying to find the jQuery
  8. scrollbar plugin that's *perfect*. But there was no *perfect* one.
  9. That's why I decided to make one.
  10. perfect-scrollbar is minimalistic but *perfect* (for me, and maybe for most developers)
  11. scrollbar plugin working with jQuery or vanilla JavaScript as well.
  12. I hope you love it!
  13. ## [Demo](http://noraesae.github.com/perfect-scrollbar/)
  14. [It's on the GitHub Pages](http://noraesae.github.com/perfect-scrollbar/).
  15. ## What does *perfect* mean?
  16. *perfect* means...
  17. * There should be no css change on any original element.
  18. * The scrollbar should not affect the original design layout.
  19. * The design of the scrollbar should be (nearly) fully customizable.
  20. * If the size of the container or the content changes, the scrollbar
  21. size and position should be able to change.
  22. * *New!* It should work with vanilla JavaScript and major tools like
  23. NPM or Browserify.
  24. ## Then perfect-scrollbar is really *perfect*?
  25. * perfect-scrollbar has some requirements, but doesn't change or add
  26. any style on original elements.
  27. * perfect-scrollbar is designed not to have width or height. It's fixed
  28. on the right and bottom side of the container.
  29. * You can change nearly all css styles of the scrollbar. The scrollbar
  30. design has no dependency on scripts.
  31. * perfect-scrollbar supports an 'update' function. Whenever you need
  32. to update the size or position of the scrollbar, just update.
  33. * Additionally, perfect-scrollbar uses 'scrollTop' and 'scrollLeft',
  34. not absolute positioning or something messy.
  35. * perfect-scrollbar supports RTL perfectly on both WebKit and Gecko based browsers.
  36. It's cool, isn't it?
  37. ## Install
  38. The best way to install and use perfect-scrollbar is with NPM.
  39. It's registered on [npm](https://www.npmjs.org/package/perfect-scrollbar) as `perfect-scrollbar`.
  40. ```
  41. $ npm install perfect-scrollbar
  42. ```
  43. You can download the latest stable version with download links [here](http://noraesae.github.io/perfect-scrollbar/).
  44. You also can find all releases on [Releases](https://github.com/noraesae/perfect-scrollbar/releases).
  45. If you want to use the development version of the plugin, use the
  46. source files which are not minified. They're in the `src` directory.
  47. The development version may be unstable, but some known bugs may
  48. have been fixed.
  49. ```
  50. $ git clone https://github.com/noraesae/perfect-scrollbar.git
  51. $ cd perfect-scrollbar/src
  52. $ npm install
  53. $ gulp # will lint and build the source code.
  54. ```
  55. There is a Bower package for perfect-scrollbar as well. It is managed
  56. under the [perfect-scrollbar-bower](https://github.com/noraesae/perfect-scrollbar-bower)
  57. repository. The plugin is registered as `perfect-scrollbar`.
  58. ```
  59. $ bower install perfect-scrollbar
  60. ```
  61. You can also load it from [cdnjs](http://cdnjs.com/).
  62. It is registered as [`jquery.perfect-scrollbar`](http://www.cdnjs.com/libraries/jquery.perfect-scrollbar).
  63. ## Requirements
  64. To make this plugin *perfect*, some requirements were unavoidable.
  65. But, they're all very trivial and there is nothing to worry about.
  66. The following requirements should meet.
  67. * the container must have a 'position' css style.
  68. The following requirements are included in the basic CSS, but please
  69. keep in mind when you'd like to change the CSS files.
  70. * the container must have an 'overflow:hidden' css style.
  71. * the scrollbar's position must be 'absolute'.
  72. * the scrollbar-x must have a 'bottom' css style, and the scrollbar-y
  73. must have a 'right' css style.
  74. ## How to use
  75. First of all, please check if the container element meets the
  76. requirements.
  77. ```html
  78. <link rel='stylesheet' href='dist/css/perfect-scrollbar.css' />
  79. <style>
  80. #container {
  81. position: relative;
  82. height: 100%; /* Or whatever you want (eg. 400px) */
  83. }
  84. </style>
  85. ```
  86. I would recommend using the plugin with NPM and CommonJS module system
  87. like Browserify.
  88. ```javascript
  89. var Ps = require('perfect-scrollbar');
  90. ```
  91. Or you can just load the script file as usual.
  92. ```html
  93. <script src='dist/js/perfect-scrollbar.js'></script>
  94. ```
  95. To initialise the plugin, `Ps.initialize` is used.
  96. ```javascript
  97. var container = document.getElementById('container');
  98. Ps.initialize(container);
  99. ```
  100. It can be initialised with optional parameters.
  101. ```javascript
  102. Ps.initialize(container, {
  103. wheelSpeed: 2,
  104. wheelPropagation: true,
  105. minScrollbarLength: 20
  106. });
  107. ```
  108. If the size of your container or content changes, call `update`.
  109. ```javascript
  110. Ps.update(container);
  111. ```
  112. If you want to destory the scrollbar, use `destroy`.
  113. ```javascript
  114. Ps.destroy(container);
  115. ```
  116. If you want to scroll to somewhere, just use a `scrollTop`
  117. property and update.
  118. ```javascript
  119. container.scrollTop = 0;
  120. Ps.update(container);
  121. ```
  122. You can also get information about how to use the plugin
  123. from code in the `examples` directory of the source tree.
  124. ## jQuery
  125. As you may already know, perfect-scrollbar was a jQuery plugin.
  126. And it *is* as well. There's a jQuery adaptor and the plugin can
  127. be used in the same way it used to be used before.
  128. I also recommend using NPM and CommonJS here, but it's not mandatory.
  129. ```javascript
  130. var $ = require('jquery');
  131. require('perfect-scrollbar/jquery')($);
  132. ```
  133. For sure, you can just import a built script.
  134. ```html
  135. <script src='dist/js/perfect-scrollbar.jquery.js'></script>
  136. ```
  137. After importing it, you can use the plugin in the usual way.
  138. ```javascript
  139. $('#container').perfectScrollbar(); // Initialize
  140. $('#container').perfectScrollbar({ ... }); // with options
  141. $('#container').perfectScrollbar('update'); // Update
  142. $('#container').perfectScrollbar('destroy'); // Destroy
  143. ```
  144. ## RequireJS usage
  145. For RequireJS loader, no need to write shim, simply import two libs:
  146. ```javascript
  147. require.config({
  148. paths: {
  149. perfectScrollbarJQuery: '.../perfect-scrollbar.jquery',
  150. perfectScrollbar: '.../perfect-scrollbar',
  151. }
  152. ...
  153. })
  154. ```
  155. and load `perfectScrollbar` in the initialiser of your app:
  156. ```javascript
  157. // for vanilla JS:
  158. window.Ps = require('perfectScrollbar');
  159. // for jQuery:
  160. require('perfectScrollbarJQuery');
  161. ```
  162. ## AngularJS + RequireJS usage
  163. With the require.config settings above, at the beginning of your app module
  164. definition, you can have following code:
  165. ```javascript
  166. define([
  167. 'angular',
  168. 'perfectScrollbar',
  169. 'perfectScrollbarJquery'
  170. ],
  171. function (angular) {
  172. var myApp = angular.module('myApp', [])
  173. .run(function() {
  174. window.Ps = require('perfectScrollbar');
  175. require('perfectScrollbarJQuery');
  176. })
  177. return myApp;
  178. });
  179. ```
  180. And initialise perfectScrollbar in a controller:
  181. ```javascript
  182. // by vanilla JS:
  183. var container = document.getElementById('imgLoader');
  184. Ps.initialize(container);
  185. Ps.update(container);
  186. // or by jQuery:
  187. var imgLoader = $("#imgLoader")
  188. imgLoader.perfectScrollbar();
  189. ```
  190. ## Optional parameters
  191. perfect-scrollbar supports optional parameters.
  192. ### wheelSpeed
  193. The scroll speed applied to mousewheel event.
  194. **Default: 1**
  195. ### wheelPropagation
  196. If this option is true, when the scroll reaches the end of the side, mousewheel event will be propagated to parent element.
  197. **Default: false**
  198. ### swipePropagation
  199. If this option is true, when the scroll reaches the end of the side, touch scrolling will be propagated to parent element.
  200. **Default: true**
  201. ### minScrollbarLength
  202. When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels.
  203. **Default: null**
  204. ### maxScrollbarLength
  205. When set to an integer value, the thumb part of the scrollbar will not expand over that number of pixels.
  206. **Default: null**
  207. ### useBothWheelAxes
  208. When set to true, and only one (vertical or horizontal) scrollbar is visible then both vertical and horizontal scrolling will affect the scrollbar.
  209. **Default: false**
  210. ### useKeyboard
  211. When set to true, the scroll works with arrow keys on the keyboard. The element is scrolled only when the mouse cursor hovers the element.
  212. **Default: true**
  213. ### suppressScrollX
  214. When set to true, the scroll bar in X axis will not be available, regardless of the content width.
  215. **Default: false**
  216. ### suppressScrollY
  217. When set to true, the scroll bar in Y axis will not be available, regardless of the content height.
  218. **Default: false**
  219. ### scrollXMarginOffset
  220. The number of pixels the content width can surpass the container width without enabling the X axis scroll bar. Allows some "wiggle room" or "offset break", so that X axis scroll bar is not enabled just because of a few pixels.
  221. **Default: 0**
  222. ### scrollYMarginOffset
  223. The number of pixels the content height can surpass the container height without enabling the Y axis scroll bar. Allows some "wiggle room" or "offset break", so that Y axis scroll bar is not enabled just because of a few pixels.
  224. **Default: 0**
  225. ### stopPropagationOnClick
  226. When set to false, when clicking on a rail, the click event will be allowed to propagate.
  227. **Default: true**
  228. ### useSelectionScroll
  229. When set to true, you can scroll the container by selecting text and move the cursor.
  230. **Default: false**
  231. ## Events
  232. perfect-scrollbar dispatches custom events.
  233. ### ps-scroll-y
  234. This event fires when the y-axis is scrolled in either direction.
  235. ### ps-scroll-x
  236. This event fires when the x-axis is scrolled in either direction.
  237. ### ps-scroll-up
  238. This event fires when scrolling upwards.
  239. ### ps-scroll-down
  240. This event fires when scrolling downwards.
  241. ### ps-scroll-left
  242. This event fires when scrolling to the left.
  243. ### ps-scroll-right
  244. This event fires when scrolling to the right.
  245. ### ps-y-reach-start
  246. This event fires when scrolling reaches the start of the y-axis.
  247. ### ps-y-reach-end
  248. This event fires when scrolling reaches the end of the y-axis (useful for infinite scroll).
  249. ### ps-x-reach-start
  250. This event fires when scrolling reaches the start of the x-axis.
  251. ### ps-x-reach-end
  252. This event fires when scrolling reaches the end of the x-axis.
  253. You can listen to these events either with vanilla JavaScript
  254. ```javascript
  255. document.addEventListener('ps-scroll-x', function () {
  256. // ...
  257. })
  258. ```
  259. or with jQuery
  260. ```javascript
  261. $(document).on('ps-scroll-x', function () {
  262. // ...
  263. })
  264. ```
  265. ## Contribution
  266. #### Please read [Contributing](https://github.com/noraesae/perfect-scrollbar/wiki/Contributing) in the wiki before making any contribution.
  267. I *really* welcome contributions! Please feel free to fork and issue pull requests when...
  268. * You have a very nice idea to improve this plugin!
  269. * You found a bug!
  270. * You're good at English and can help my bad English!
  271. For IE problems, please refer to [IE Support](https://github.com/noraesae/perfect-scrollbar#ie-support).
  272. ## IE Support
  273. The plugin would work in IEs >= IE9 (not well, though).
  274. **The patches to fix problems in IE<=8 won't be accepted.**
  275. When old IEs should be supported, please fork the project and
  276. make patches personally.
  277. ## Helpdesk
  278. If you have any idea to improve this project or any problem
  279. using this, please feel free to upload an
  280. [issue](https://github.com/noraesae/perfect-scrollbar/issues).
  281. For common problems there is a
  282. [FAQ](https://github.com/noraesae/perfect-scrollbar/wiki/FAQ) wiki page. Please check the page before uploading an issue.
  283. ## License
  284. The MIT License (MIT) Copyright (c) 2015 Hyunje Alex Jun and other contributors.
  285. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  286. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  287. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.