jquery.slimscroll.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*! Copyright (c) 2011 Piotr Rochala (http://rocha.la)
  2. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  3. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  4. *
  5. * Version: 1.3.0
  6. *
  7. */
  8. (function ($) {
  9. jQuery.fn.extend({
  10. slimScroll: function (options) {
  11. var defaults = {
  12. // width in pixels of the visible scroll area
  13. width: 'auto',
  14. // height in pixels of the visible scroll area
  15. height: '250px',
  16. // width in pixels of the scrollbar and rail
  17. size: '7px',
  18. // scrollbar color, accepts any hex/color value
  19. color: '#000',
  20. // scrollbar position - left/right
  21. position: 'right',
  22. // distance in pixels between the side edge and the scrollbar
  23. distance: '1px',
  24. // default scroll position on load - top / bottom / $('selector')
  25. start: 'top',
  26. // sets scrollbar opacity
  27. opacity: .4,
  28. // enables always-on mode for the scrollbar
  29. alwaysVisible: false,
  30. // check if we should hide the scrollbar when user is hovering over
  31. disableFadeOut: false,
  32. // sets visibility of the rail
  33. railVisible: false,
  34. // sets rail color
  35. railColor: '#333',
  36. // sets rail opacity
  37. railOpacity: .2,
  38. // whether we should use jQuery UI Draggable to enable bar dragging
  39. railDraggable: true,
  40. // defautlt CSS class of the slimscroll rail
  41. railClass: 'slimScrollRail',
  42. // defautlt CSS class of the slimscroll bar
  43. barClass: 'slimScrollBar',
  44. // defautlt CSS class of the slimscroll wrapper
  45. wrapperClass: 'slimScrollDiv',
  46. // check if mousewheel should scroll the window if we reach top/bottom
  47. allowPageScroll: false,
  48. // scroll amount applied to each mouse wheel step
  49. wheelStep: 20,
  50. // scroll amount applied when user is using gestures
  51. touchScrollStep: 200,
  52. // sets border radius
  53. borderRadius: '7px',
  54. // sets border radius of the rail
  55. railBorderRadius: '7px'
  56. };
  57. var o = $.extend(defaults, options);
  58. // do it for every element that matches selector
  59. this.each(function () {
  60. var isOverPanel, isOverBar, isDragg, queueHide, touchDif,
  61. barHeight, percentScroll, lastScroll,
  62. divS = '<div></div>',
  63. minBarHeight = 30,
  64. releaseScroll = false;
  65. // used in event handlers and for better minification
  66. var me = $(this);
  67. // ensure we are not binding it again
  68. if (me.parent().hasClass(o.wrapperClass)) {
  69. // start from last bar position
  70. var offset = me.scrollTop();
  71. // find bar and rail
  72. bar = me.parent().find('.' + o.barClass);
  73. rail = me.parent().find('.' + o.railClass);
  74. getBarHeight();
  75. // check if we should scroll existing instance
  76. if ($.isPlainObject(options)) {
  77. // Pass height: auto to an existing slimscroll object to force a resize after contents have changed
  78. if ('height' in options && options.height == 'auto') {
  79. me.parent().css('height', 'auto');
  80. me.css('height', 'auto');
  81. var height = me.parent().parent().height();
  82. me.parent().css('height', height);
  83. me.css('height', height);
  84. }
  85. if ('scrollTo' in options) {
  86. // jump to a static point
  87. offset = parseInt(o.scrollTo);
  88. }
  89. else if ('scrollBy' in options) {
  90. // jump by value pixels
  91. offset += parseInt(o.scrollBy);
  92. }
  93. else if ('destroy' in options) {
  94. // remove slimscroll elements
  95. bar.remove();
  96. rail.remove();
  97. me.unwrap();
  98. return;
  99. }
  100. // scroll content by the given offset
  101. scrollContent(offset, false, true);
  102. }
  103. return;
  104. }
  105. // optionally set height to the parent's height
  106. o.height = (o.height == 'auto') ? me.parent().height() : o.height;
  107. // wrap content
  108. var wrapper = $(divS)
  109. .addClass(o.wrapperClass)
  110. .css({
  111. position: 'relative',
  112. overflow: 'hidden',
  113. width: o.width,
  114. height: o.height
  115. });
  116. // update style for the div
  117. me.css({
  118. overflow: 'hidden',
  119. width: o.width,
  120. height: o.height
  121. });
  122. // create scrollbar rail
  123. var rail = $(divS)
  124. .addClass(o.railClass)
  125. .css({
  126. width: o.size,
  127. height: '100%',
  128. position: 'absolute',
  129. top: 0,
  130. display: (o.alwaysVisible && o.railVisible) ? 'block' : 'none',
  131. 'border-radius': o.railBorderRadius,
  132. background: o.railColor,
  133. opacity: o.railOpacity,
  134. zIndex: 90
  135. });
  136. // create scrollbar
  137. var bar = $(divS)
  138. .addClass(o.barClass)
  139. .css({
  140. background: o.color,
  141. width: o.size,
  142. position: 'absolute',
  143. top: 0,
  144. opacity: o.opacity,
  145. display: o.alwaysVisible ? 'block' : 'none',
  146. 'border-radius': o.borderRadius,
  147. BorderRadius: o.borderRadius,
  148. MozBorderRadius: o.borderRadius,
  149. WebkitBorderRadius: o.borderRadius,
  150. zIndex: 99
  151. });
  152. // set position
  153. var posCss = (o.position == 'right') ? { right: o.distance } : { left: o.distance };
  154. rail.css(posCss);
  155. bar.css(posCss);
  156. // wrap it
  157. me.wrap(wrapper);
  158. // append to parent div
  159. me.parent().append(bar);
  160. me.parent().append(rail);
  161. // make it draggable and no longer dependent on the jqueryUI
  162. if (o.railDraggable) {
  163. bar.bind("mousedown", function (e) {
  164. var $doc = $(document);
  165. isDragg = true;
  166. t = parseFloat(bar.css('top'));
  167. pageY = e.pageY;
  168. $doc.bind("mousemove.slimscroll", function (e) {
  169. currTop = t + e.pageY - pageY;
  170. bar.css('top', currTop);
  171. scrollContent(0, bar.position().top, false);// scroll content
  172. });
  173. $doc.bind("mouseup.slimscroll", function (e) {
  174. isDragg = false; hideBar();
  175. $doc.unbind('.slimscroll');
  176. });
  177. return false;
  178. }).bind("selectstart.slimscroll", function (e) {
  179. e.stopPropagation();
  180. e.preventDefault();
  181. return false;
  182. });
  183. }
  184. // on rail over
  185. rail.hover(function () {
  186. showBar();
  187. }, function () {
  188. hideBar();
  189. });
  190. // on bar over
  191. bar.hover(function () {
  192. isOverBar = true;
  193. }, function () {
  194. isOverBar = false;
  195. });
  196. // show on parent mouseover
  197. me.hover(function () {
  198. isOverPanel = true;
  199. showBar();
  200. hideBar();
  201. }, function () {
  202. isOverPanel = false;
  203. hideBar();
  204. });
  205. // support for mobile
  206. me.bind('touchstart', function (e, b) {
  207. if (e.originalEvent.touches.length) {
  208. // record where touch started
  209. touchDif = e.originalEvent.touches[0].pageY;
  210. }
  211. });
  212. me.bind('touchmove', function (e) {
  213. // prevent scrolling the page if necessary
  214. if (!releaseScroll) {
  215. e.originalEvent.preventDefault();
  216. }
  217. if (e.originalEvent.touches.length) {
  218. // see how far user swiped
  219. var diff = (touchDif - e.originalEvent.touches[0].pageY) / o.touchScrollStep;
  220. // scroll content
  221. scrollContent(diff, true);
  222. touchDif = e.originalEvent.touches[0].pageY;
  223. }
  224. });
  225. // set up initial height
  226. getBarHeight();
  227. // check start position
  228. if (o.start === 'bottom') {
  229. // scroll content to bottom
  230. bar.css({ top: me.outerHeight() - bar.outerHeight() });
  231. scrollContent(0, true);
  232. }
  233. else if (o.start !== 'top') {
  234. // assume jQuery selector
  235. scrollContent($(o.start).position().top, null, true);
  236. // make sure bar stays hidden
  237. if (!o.alwaysVisible) { bar.hide(); }
  238. }
  239. // attach scroll events
  240. attachWheel();
  241. function _onWheel(e) {
  242. // use mouse wheel only when mouse is over
  243. if (!isOverPanel) { return; }
  244. var e = e || window.event;
  245. var delta = 0;
  246. if (e.wheelDelta) { delta = -e.wheelDelta / 120; }
  247. if (e.detail) { delta = e.detail / 3; }
  248. var target = e.target || e.srcTarget || e.srcElement;
  249. /* console.log($(target).closest('.' + o.wrapperClass).attr("class"));
  250. console.log(me.parent().attr("class"));*/
  251. if ($(target).closest('.' + o.wrapperClass).is(me.parent())) {
  252. // scroll content
  253. scrollContent(delta, true);
  254. }
  255. // stop window scroll
  256. if (e.preventDefault && !releaseScroll) { e.preventDefault(); }
  257. if (!releaseScroll) { e.returnValue = false; }
  258. }
  259. function scrollContent(y, isWheel, isJump) {
  260. releaseScroll = false;
  261. var delta = y;
  262. var maxTop = me.outerHeight() - bar.outerHeight();
  263. if (isWheel) {
  264. // move bar with mouse wheel
  265. delta = parseInt(bar.css('top')) + y * parseInt(o.wheelStep) / 100 * bar.outerHeight();
  266. // move bar, make sure it doesn't go out
  267. delta = Math.min(Math.max(delta, 0), maxTop);
  268. // if scrolling down, make sure a fractional change to the
  269. // scroll position isn't rounded away when the scrollbar's CSS is set
  270. // this flooring of delta would happened automatically when
  271. // bar.css is set below, but we floor here for clarity
  272. delta = (y > 0) ? Math.ceil(delta) : Math.floor(delta);
  273. // scroll the scrollbar
  274. bar.css({ top: delta + 'px' });
  275. }
  276. // calculate actual scroll amount
  277. percentScroll = parseInt(bar.css('top')) / (me.outerHeight() - bar.outerHeight());
  278. delta = percentScroll * (me[0].scrollHeight - me.outerHeight());
  279. if (isJump) {
  280. delta = y;
  281. var offsetTop = delta / me[0].scrollHeight * me.outerHeight();
  282. offsetTop = Math.min(Math.max(offsetTop, 0), maxTop);
  283. bar.css({ top: offsetTop + 'px' });
  284. }
  285. // scroll content
  286. me.scrollTop(delta);
  287. // fire scrolling event
  288. me.trigger('slimscrolling', ~~delta);
  289. // ensure bar is visible
  290. showBar();
  291. // trigger hide when scroll is stopped
  292. hideBar();
  293. }
  294. function attachWheel() {
  295. if (window.addEventListener) {
  296. this.addEventListener('DOMMouseScroll', _onWheel, false);
  297. this.addEventListener('mousewheel', _onWheel, false);
  298. //this.addEventListener('MozMousePixelScroll', _onWheel, false );
  299. }
  300. else {
  301. document.attachEvent("onmousewheel", _onWheel)
  302. }
  303. }
  304. function getBarHeight() {
  305. // calculate scrollbar height and make sure it is not too small
  306. barHeight = Math.max((me.outerHeight() / me[0].scrollHeight) * me.outerHeight(), minBarHeight);
  307. bar.css({ height: barHeight + 'px' });
  308. // hide scrollbar if content is not long enough
  309. var display = barHeight == me.outerHeight() ? 'none' : 'block';
  310. bar.css({ display: display });
  311. }
  312. function showBar() {
  313. // recalculate bar height
  314. getBarHeight();
  315. clearTimeout(queueHide);
  316. // when bar reached top or bottom
  317. if (percentScroll == ~~percentScroll) {
  318. //release wheel
  319. releaseScroll = o.allowPageScroll;
  320. // publish approporiate event
  321. if (lastScroll != percentScroll) {
  322. var msg = (~~percentScroll == 0) ? 'top' : 'bottom';
  323. me.trigger('slimscroll', msg);
  324. }
  325. }
  326. else {
  327. releaseScroll = false;
  328. }
  329. lastScroll = percentScroll;
  330. // show only when required
  331. if (barHeight >= me.outerHeight()) {
  332. //allow window scroll
  333. releaseScroll = true;
  334. return;
  335. }
  336. bar.stop(true, true).fadeIn('fast');
  337. if (o.railVisible) { rail.stop(true, true).fadeIn('fast'); }
  338. }
  339. function hideBar() {
  340. // only hide when options allow it
  341. if (!o.alwaysVisible) {
  342. queueHide = setTimeout(function () {
  343. if (!(o.disableFadeOut && isOverPanel) && !isOverBar && !isDragg) {
  344. bar.fadeOut('slow');
  345. rail.fadeOut('slow');
  346. }
  347. }, 1000);
  348. }
  349. }
  350. });
  351. // maintain chainability
  352. return this;
  353. }
  354. });
  355. jQuery.fn.extend({
  356. slimscroll: jQuery.fn.slimScroll
  357. });
  358. })(jQuery);