perfect-scrollbar.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*! Copyright (c) 2013 Brandon Aaron (http://brandon.aaron.sh)
  2. * Licensed under the MIT License (LICENSE.txt).
  3. *
  4. * Version: 3.1.9
  5. *
  6. * Requires: jQuery 1.2.2+
  7. */
  8. (function (factory) {
  9. if ( typeof define === 'function' && define.amd ) {
  10. // AMD. Register as an anonymous module.
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. // Node/CommonJS style for Browserify
  14. module.exports = factory;
  15. } else {
  16. // Browser globals
  17. factory(jQuery);
  18. }
  19. }(function ($) {
  20. var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'],
  21. toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ?
  22. ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'],
  23. slice = Array.prototype.slice,
  24. nullLowestDeltaTimeout, lowestDelta;
  25. if ( $.event.fixHooks ) {
  26. for ( var i = toFix.length; i; ) {
  27. $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
  28. }
  29. }
  30. var special = $.event.special.mousewheel = {
  31. version: '3.1.9',
  32. setup: function() {
  33. if ( this.addEventListener ) {
  34. for ( var i = toBind.length; i; ) {
  35. this.addEventListener( toBind[--i], handler, false );
  36. }
  37. } else {
  38. this.onmousewheel = handler;
  39. }
  40. // Store the line height and page height for this particular element
  41. $.data(this, 'mousewheel-line-height', special.getLineHeight(this));
  42. $.data(this, 'mousewheel-page-height', special.getPageHeight(this));
  43. },
  44. teardown: function() {
  45. if ( this.removeEventListener ) {
  46. for ( var i = toBind.length; i; ) {
  47. this.removeEventListener( toBind[--i], handler, false );
  48. }
  49. } else {
  50. this.onmousewheel = null;
  51. }
  52. },
  53. getLineHeight: function(elem) {
  54. return parseInt($(elem)['offsetParent' in $.fn ? 'offsetParent' : 'parent']().css('fontSize'), 10);
  55. },
  56. getPageHeight: function(elem) {
  57. return $(elem).height();
  58. },
  59. settings: {
  60. adjustOldDeltas: true
  61. }
  62. };
  63. $.fn.extend({
  64. mousewheel: function(fn) {
  65. return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel');
  66. },
  67. unmousewheel: function(fn) {
  68. return this.unbind('mousewheel', fn);
  69. }
  70. });
  71. function handler(event) {
  72. var orgEvent = event || window.event,
  73. args = slice.call(arguments, 1),
  74. delta = 0,
  75. deltaX = 0,
  76. deltaY = 0,
  77. absDelta = 0;
  78. event = $.event.fix(orgEvent);
  79. event.type = 'mousewheel';
  80. // Old school scrollwheel delta
  81. if ( 'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; }
  82. if ( 'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; }
  83. if ( 'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; }
  84. if ( 'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; }
  85. // Firefox < 17 horizontal scrolling related to DOMMouseScroll event
  86. if ( 'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
  87. deltaX = deltaY * -1;
  88. deltaY = 0;
  89. }
  90. // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy
  91. delta = deltaY === 0 ? deltaX : deltaY;
  92. // New school wheel delta (wheel event)
  93. if ( 'deltaY' in orgEvent ) {
  94. deltaY = orgEvent.deltaY * -1;
  95. delta = deltaY;
  96. }
  97. if ( 'deltaX' in orgEvent ) {
  98. deltaX = orgEvent.deltaX;
  99. if ( deltaY === 0 ) { delta = deltaX * -1; }
  100. }
  101. // No change actually happened, no reason to go any further
  102. if ( deltaY === 0 && deltaX === 0 ) { return; }
  103. // Need to convert lines and pages to pixels if we aren't already in pixels
  104. // There are three delta modes:
  105. // * deltaMode 0 is by pixels, nothing to do
  106. // * deltaMode 1 is by lines
  107. // * deltaMode 2 is by pages
  108. if ( orgEvent.deltaMode === 1 ) {
  109. var lineHeight = $.data(this, 'mousewheel-line-height');
  110. delta *= lineHeight;
  111. deltaY *= lineHeight;
  112. deltaX *= lineHeight;
  113. } else if ( orgEvent.deltaMode === 2 ) {
  114. var pageHeight = $.data(this, 'mousewheel-page-height');
  115. delta *= pageHeight;
  116. deltaY *= pageHeight;
  117. deltaX *= pageHeight;
  118. }
  119. // Store lowest absolute delta to normalize the delta values
  120. absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) );
  121. if ( !lowestDelta || absDelta < lowestDelta ) {
  122. lowestDelta = absDelta;
  123. // Adjust older deltas if necessary
  124. if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
  125. lowestDelta /= 40;
  126. }
  127. }
  128. // Adjust older deltas if necessary
  129. if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
  130. // Divide all the things by 40!
  131. delta /= 40;
  132. deltaX /= 40;
  133. deltaY /= 40;
  134. }
  135. // Get a whole, normalized value for the deltas
  136. delta = Math[ delta >= 1 ? 'floor' : 'ceil' ](delta / lowestDelta);
  137. deltaX = Math[ deltaX >= 1 ? 'floor' : 'ceil' ](deltaX / lowestDelta);
  138. deltaY = Math[ deltaY >= 1 ? 'floor' : 'ceil' ](deltaY / lowestDelta);
  139. // Add information to the event object
  140. event.deltaX = deltaX;
  141. event.deltaY = deltaY;
  142. event.deltaFactor = lowestDelta;
  143. // Go ahead and set deltaMode to 0 since we converted to pixels
  144. // Although this is a little odd since we overwrite the deltaX/Y
  145. // properties with normalized deltas.
  146. event.deltaMode = 0;
  147. // Add event and delta to the front of the arguments
  148. args.unshift(event, delta, deltaX, deltaY);
  149. // Clearout lowestDelta after sometime to better
  150. // handle multiple device types that give different
  151. // a different lowestDelta
  152. // Ex: trackpad = 3 and mouse wheel = 120
  153. if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); }
  154. nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200);
  155. return ($.event.dispatch || $.event.handle).apply(this, args);
  156. }
  157. function nullLowestDelta() {
  158. lowestDelta = null;
  159. }
  160. function shouldAdjustOldDeltas(orgEvent, absDelta) {
  161. // If this is an older event and the delta is divisable by 120,
  162. // then we are assuming that the browser is treating this as an
  163. // older mouse wheel event and that we should divide the deltas
  164. // by 40 to try and get a more usable deltaFactor.
  165. // Side note, this actually impacts the reported scroll distance
  166. // in older browsers and can cause scrolling to be slower than native.
  167. // Turn this off by setting $.event.special.mousewheel.settings.adjustOldDeltas to false.
  168. return special.settings.adjustOldDeltas && orgEvent.type === 'mousewheel' && absDelta % 120 === 0;
  169. }
  170. }));
  171. /* Copyright (c) 2012, 2014 Hyeonje Alex Jun and other contributors
  172. * Licensed under the MIT License
  173. */
  174. (function (factory) {
  175. 'use strict';
  176. if (typeof define === 'function' && define.amd) {
  177. // AMD. Register as an anonymous module.
  178. define(['jquery'], factory);
  179. } else if (typeof exports === 'object') {
  180. // Node/CommonJS
  181. factory(require('jquery'));
  182. } else {
  183. // Browser globals
  184. factory(jQuery);
  185. }
  186. }(function ($) {
  187. 'use strict';
  188. // The default settings for the plugin
  189. var defaultSettings = {
  190. wheelSpeed: 10,
  191. wheelPropagation: false,
  192. minScrollbarLength: null,
  193. useBothWheelAxes: false,
  194. useKeyboard: true,
  195. suppressScrollX: false,
  196. suppressScrollY: false,
  197. scrollXMarginOffset: 0,
  198. scrollYMarginOffset: 0,
  199. includePadding: false
  200. };
  201. var getEventClassName = (function () {
  202. var incrementingId = 0;
  203. return function () {
  204. var id = incrementingId;
  205. incrementingId += 1;
  206. return '.perfect-scrollbar-' + id;
  207. };
  208. }());
  209. $.fn.perfectScrollbar = function (suppliedSettings, option) {
  210. return this.each(function () {
  211. // Use the default settings
  212. var settings = $.extend(true, {}, defaultSettings),
  213. $this = $(this);
  214. if (typeof suppliedSettings === "object") {
  215. // But over-ride any supplied
  216. $.extend(true, settings, suppliedSettings);
  217. } else {
  218. // If no settings were supplied, then the first param must be the option
  219. option = suppliedSettings;
  220. }
  221. // Catch options
  222. if (option === 'update') {
  223. if ($this.data('perfect-scrollbar-update')) {
  224. $this.data('perfect-scrollbar-update')();
  225. }
  226. return $this;
  227. }
  228. else if (option === 'destroy') {
  229. if ($this.data('perfect-scrollbar-destroy')) {
  230. $this.data('perfect-scrollbar-destroy')();
  231. }
  232. return $this;
  233. }
  234. if ($this.data('perfect-scrollbar')) {
  235. // if there's already perfect-scrollbar
  236. return $this.data('perfect-scrollbar');
  237. }
  238. // Or generate new perfectScrollbar
  239. // Set class to the container
  240. $this.addClass('ps-container');
  241. var $scrollbarXRail = $("<div class='ps-scrollbar-x-rail'></div>").appendTo($this),
  242. $scrollbarYRail = $("<div class='ps-scrollbar-y-rail'></div>").appendTo($this),
  243. $scrollbarX = $("<div class='ps-scrollbar-x'></div>").appendTo($scrollbarXRail),
  244. $scrollbarY = $("<div class='ps-scrollbar-y'></div>").appendTo($scrollbarYRail),
  245. scrollbarXActive,
  246. scrollbarYActive,
  247. containerWidth,
  248. containerHeight,
  249. contentWidth,
  250. contentHeight,
  251. scrollbarXWidth,
  252. scrollbarXLeft,
  253. scrollbarXBottom = parseInt($scrollbarXRail.css('bottom'), 10),
  254. isScrollbarXUsingBottom = scrollbarXBottom === scrollbarXBottom, // !isNaN
  255. scrollbarXTop = isScrollbarXUsingBottom ? null : parseInt($scrollbarXRail.css('top'), 10),
  256. scrollbarYHeight,
  257. scrollbarYTop,
  258. scrollbarYRight = parseInt($scrollbarYRail.css('right'), 10),
  259. isScrollbarYUsingRight = scrollbarYRight === scrollbarYRight, // !isNaN
  260. scrollbarYLeft = isScrollbarYUsingRight ? null: parseInt($scrollbarYRail.css('left'), 10),
  261. isRtl = $this.css('direction') === "rtl",
  262. eventClassName = getEventClassName();
  263. var updateContentScrollTop = function (currentTop, deltaY) {
  264. var newTop = currentTop + deltaY,
  265. maxTop = containerHeight - scrollbarYHeight;
  266. if (newTop < 0) {
  267. scrollbarYTop = 0;
  268. }
  269. else if (newTop > maxTop) {
  270. scrollbarYTop = maxTop;
  271. }
  272. else {
  273. scrollbarYTop = newTop;
  274. }
  275. var scrollTop = parseInt(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight), 10);
  276. $this.scrollTop(scrollTop);
  277. if (isScrollbarXUsingBottom) {
  278. $scrollbarXRail.css({bottom: scrollbarXBottom - scrollTop});
  279. } else {
  280. $scrollbarXRail.css({top: scrollbarXTop + scrollTop});
  281. }
  282. };
  283. var updateContentScrollLeft = function (currentLeft, deltaX) {
  284. var newLeft = currentLeft + deltaX,
  285. maxLeft = containerWidth - scrollbarXWidth;
  286. if (newLeft < 0) {
  287. scrollbarXLeft = 0;
  288. }
  289. else if (newLeft > maxLeft) {
  290. scrollbarXLeft = maxLeft;
  291. }
  292. else {
  293. scrollbarXLeft = newLeft;
  294. }
  295. var scrollLeft = parseInt(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth), 10);
  296. $this.scrollLeft(scrollLeft);
  297. if (isScrollbarYUsingRight) {
  298. $scrollbarYRail.css({right: scrollbarYRight - scrollLeft});
  299. } else {
  300. $scrollbarYRail.css({left: scrollbarYLeft + scrollLeft});
  301. }
  302. };
  303. var getSettingsAdjustedThumbSize = function (thumbSize) {
  304. if (settings.minScrollbarLength) {
  305. thumbSize = Math.max(thumbSize, settings.minScrollbarLength);
  306. }
  307. return thumbSize;
  308. };
  309. var updateScrollbarCss = function () {
  310. var scrollbarXStyles = {width: containerWidth, display: scrollbarXActive ? "inherit": "none"};
  311. if (isRtl) {
  312. scrollbarXStyles.left = $this.scrollLeft() + containerWidth - contentWidth;
  313. } else {
  314. scrollbarXStyles.left = $this.scrollLeft();
  315. }
  316. if (isScrollbarXUsingBottom) {
  317. scrollbarXStyles.bottom = scrollbarXBottom - $this.scrollTop();
  318. } else {
  319. scrollbarXStyles.top = scrollbarXTop + $this.scrollTop();
  320. }
  321. $scrollbarXRail.css(scrollbarXStyles);
  322. var scrollbarYStyles = {top: $this.scrollTop(), height: containerHeight, display: scrollbarYActive ? "inherit": "none"};
  323. if (isScrollbarYUsingRight) {
  324. if (isRtl) {
  325. scrollbarYStyles.right = contentWidth - $this.scrollLeft() - scrollbarYRight - $scrollbarY.outerWidth();
  326. } else {
  327. scrollbarYStyles.right = scrollbarYRight - $this.scrollLeft();
  328. }
  329. } else {
  330. if (isRtl) {
  331. scrollbarYStyles.left = $this.scrollLeft() + containerWidth * 2 - contentWidth - scrollbarYLeft - $scrollbarY.outerWidth();
  332. } else {
  333. scrollbarYStyles.left = scrollbarYLeft + $this.scrollLeft();
  334. }
  335. }
  336. $scrollbarYRail.css(scrollbarYStyles);
  337. $scrollbarX.css({left: scrollbarXLeft, width: scrollbarXWidth});
  338. $scrollbarY.css({top: scrollbarYTop, height: scrollbarYHeight});
  339. };
  340. var updateBarSizeAndPosition = function () {
  341. containerWidth = settings.includePadding ? $this.innerWidth() : $this.width();
  342. containerHeight = settings.includePadding ? $this.innerHeight() : $this.height();
  343. contentWidth = $this.prop('scrollWidth');
  344. contentHeight = $this.prop('scrollHeight');
  345. if (!settings.suppressScrollX && containerWidth + settings.scrollXMarginOffset < contentWidth) {
  346. scrollbarXActive = true;
  347. scrollbarXWidth = getSettingsAdjustedThumbSize(parseInt(containerWidth * containerWidth / contentWidth, 10));
  348. scrollbarXLeft = parseInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth), 10);
  349. }
  350. else {
  351. scrollbarXActive = false;
  352. scrollbarXWidth = 0;
  353. scrollbarXLeft = 0;
  354. $this.scrollLeft(0);
  355. }
  356. if (!settings.suppressScrollY && containerHeight + settings.scrollYMarginOffset < contentHeight) {
  357. scrollbarYActive = true;
  358. scrollbarYHeight = getSettingsAdjustedThumbSize(parseInt(containerHeight * containerHeight / contentHeight, 10));
  359. scrollbarYTop = parseInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight), 10);
  360. }
  361. else {
  362. scrollbarYActive = false;
  363. scrollbarYHeight = 0;
  364. scrollbarYTop = 0;
  365. $this.scrollTop(0);
  366. }
  367. if (scrollbarYTop >= containerHeight - scrollbarYHeight) {
  368. scrollbarYTop = containerHeight - scrollbarYHeight;
  369. }
  370. if (scrollbarXLeft >= containerWidth - scrollbarXWidth) {
  371. scrollbarXLeft = containerWidth - scrollbarXWidth;
  372. }
  373. updateScrollbarCss();
  374. };
  375. var bindMouseScrollXHandler = function () {
  376. var currentLeft,
  377. currentPageX;
  378. $scrollbarX.bind('mousedown' + eventClassName, function (e) {
  379. currentPageX = e.pageX;
  380. currentLeft = $scrollbarX.position().left;
  381. $scrollbarXRail.addClass('in-scrolling');
  382. e.stopPropagation();
  383. e.preventDefault();
  384. });
  385. $(document).bind('mousemove' + eventClassName, function (e) {
  386. if ($scrollbarXRail.hasClass('in-scrolling')) {
  387. updateContentScrollLeft(currentLeft, e.pageX - currentPageX);
  388. e.stopPropagation();
  389. e.preventDefault();
  390. }
  391. });
  392. $(document).bind('mouseup' + eventClassName, function (e) {
  393. if ($scrollbarXRail.hasClass('in-scrolling')) {
  394. $scrollbarXRail.removeClass('in-scrolling');
  395. }
  396. });
  397. currentLeft =
  398. currentPageX = null;
  399. };
  400. var bindMouseScrollYHandler = function () {
  401. var currentTop,
  402. currentPageY;
  403. $scrollbarY.bind('mousedown' + eventClassName, function (e) {
  404. currentPageY = e.pageY;
  405. currentTop = $scrollbarY.position().top;
  406. $scrollbarYRail.addClass('in-scrolling');
  407. e.stopPropagation();
  408. e.preventDefault();
  409. });
  410. $(document).bind('mousemove' + eventClassName, function (e) {
  411. if ($scrollbarYRail.hasClass('in-scrolling')) {
  412. updateContentScrollTop(currentTop, e.pageY - currentPageY);
  413. e.stopPropagation();
  414. e.preventDefault();
  415. }
  416. });
  417. $(document).bind('mouseup' + eventClassName, function (e) {
  418. if ($scrollbarYRail.hasClass('in-scrolling')) {
  419. $scrollbarYRail.removeClass('in-scrolling');
  420. }
  421. });
  422. currentTop =
  423. currentPageY = null;
  424. };
  425. // check if the default scrolling should be prevented.
  426. var shouldPreventDefault = function (deltaX, deltaY) {
  427. var scrollTop = $this.scrollTop();
  428. if (deltaX === 0) {
  429. if (!scrollbarYActive) {
  430. return false;
  431. }
  432. if ((scrollTop === 0 && deltaY > 0) || (scrollTop >= contentHeight - containerHeight && deltaY < 0)) {
  433. return !settings.wheelPropagation;
  434. }
  435. }
  436. var scrollLeft = $this.scrollLeft();
  437. if (deltaY === 0) {
  438. if (!scrollbarXActive) {
  439. return false;
  440. }
  441. if ((scrollLeft === 0 && deltaX < 0) || (scrollLeft >= contentWidth - containerWidth && deltaX > 0)) {
  442. return !settings.wheelPropagation;
  443. }
  444. }
  445. return true;
  446. };
  447. // bind handlers
  448. var bindMouseWheelHandler = function () {
  449. // FIXME: Backward compatibility.
  450. // After e.deltaFactor applied, wheelSpeed should have smaller value.
  451. // Currently, there's no way to change the settings after the scrollbar initialized.
  452. // But if the way is implemented in the future, wheelSpeed should be reset.
  453. settings.wheelSpeed /= 10;
  454. var shouldPrevent = false;
  455. $this.bind('mousewheel' + eventClassName, function (e, deprecatedDelta, deprecatedDeltaX, deprecatedDeltaY) {
  456. var deltaX = e.deltaX * e.deltaFactor || deprecatedDeltaX,
  457. deltaY = e.deltaY * e.deltaFactor || deprecatedDeltaY;
  458. shouldPrevent = false;
  459. if (!settings.useBothWheelAxes) {
  460. // deltaX will only be used for horizontal scrolling and deltaY will
  461. // only be used for vertical scrolling - this is the default
  462. $this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
  463. $this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
  464. } else if (scrollbarYActive && !scrollbarXActive) {
  465. // only vertical scrollbar is active and useBothWheelAxes option is
  466. // active, so let's scroll vertical bar using both mouse wheel axes
  467. if (deltaY) {
  468. $this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
  469. } else {
  470. $this.scrollTop($this.scrollTop() + (deltaX * settings.wheelSpeed));
  471. }
  472. shouldPrevent = true;
  473. } else if (scrollbarXActive && !scrollbarYActive) {
  474. // useBothWheelAxes and only horizontal bar is active, so use both
  475. // wheel axes for horizontal bar
  476. if (deltaX) {
  477. $this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
  478. } else {
  479. $this.scrollLeft($this.scrollLeft() - (deltaY * settings.wheelSpeed));
  480. }
  481. shouldPrevent = true;
  482. }
  483. // update bar position
  484. updateBarSizeAndPosition();
  485. shouldPrevent = (shouldPrevent || shouldPreventDefault(deltaX, deltaY));
  486. if (shouldPrevent) {
  487. e.stopPropagation();
  488. e.preventDefault();
  489. }
  490. });
  491. // fix Firefox scroll problem
  492. $this.bind('MozMousePixelScroll' + eventClassName, function (e) {
  493. if (shouldPrevent) {
  494. e.preventDefault();
  495. }
  496. });
  497. };
  498. var bindKeyboardHandler = function () {
  499. var hovered = false;
  500. $this.bind('mouseenter' + eventClassName, function (e) {
  501. hovered = true;
  502. });
  503. $this.bind('mouseleave' + eventClassName, function (e) {
  504. hovered = false;
  505. });
  506. var shouldPrevent = false;
  507. $(document).bind('keydown' + eventClassName, function (e) {
  508. if (!hovered || $(document.activeElement).is(":input,[contenteditable]")) {
  509. return;
  510. }
  511. var deltaX = 0,
  512. deltaY = 0;
  513. switch (e.which) {
  514. case 37: // left
  515. deltaX = -30;
  516. break;
  517. case 38: // up
  518. deltaY = 30;
  519. break;
  520. case 39: // right
  521. deltaX = 30;
  522. break;
  523. case 40: // down
  524. deltaY = -30;
  525. break;
  526. case 33: // page up
  527. deltaY = 90;
  528. break;
  529. case 32: // space bar
  530. case 34: // page down
  531. deltaY = -90;
  532. break;
  533. case 35: // end
  534. deltaY = -containerHeight;
  535. break;
  536. case 36: // home
  537. deltaY = containerHeight;
  538. break;
  539. default:
  540. return;
  541. }
  542. $this.scrollTop($this.scrollTop() - deltaY);
  543. $this.scrollLeft($this.scrollLeft() + deltaX);
  544. shouldPrevent = shouldPreventDefault(deltaX, deltaY);
  545. if (shouldPrevent) {
  546. e.preventDefault();
  547. }
  548. });
  549. };
  550. var bindRailClickHandler = function () {
  551. var stopPropagation = function (e) { e.stopPropagation(); };
  552. $scrollbarY.bind('click' + eventClassName, stopPropagation);
  553. $scrollbarYRail.bind('click' + eventClassName, function (e) {
  554. var halfOfScrollbarLength = parseInt(scrollbarYHeight / 2, 10),
  555. positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength,
  556. maxPositionTop = containerHeight - scrollbarYHeight,
  557. positionRatio = positionTop / maxPositionTop;
  558. if (positionRatio < 0) {
  559. positionRatio = 0;
  560. } else if (positionRatio > 1) {
  561. positionRatio = 1;
  562. }
  563. $this.scrollTop((contentHeight - containerHeight) * positionRatio);
  564. });
  565. $scrollbarX.bind('click' + eventClassName, stopPropagation);
  566. $scrollbarXRail.bind('click' + eventClassName, function (e) {
  567. var halfOfScrollbarLength = parseInt(scrollbarXWidth / 2, 10),
  568. positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength,
  569. maxPositionLeft = containerWidth - scrollbarXWidth,
  570. positionRatio = positionLeft / maxPositionLeft;
  571. if (positionRatio < 0) {
  572. positionRatio = 0;
  573. } else if (positionRatio > 1) {
  574. positionRatio = 1;
  575. }
  576. $this.scrollLeft((contentWidth - containerWidth) * positionRatio);
  577. });
  578. };
  579. // bind mobile touch handler
  580. var bindMobileTouchHandler = function () {
  581. var applyTouchMove = function (differenceX, differenceY) {
  582. $this.scrollTop($this.scrollTop() - differenceY);
  583. $this.scrollLeft($this.scrollLeft() - differenceX);
  584. // update bar position
  585. updateBarSizeAndPosition();
  586. };
  587. var startCoords = {},
  588. startTime = 0,
  589. speed = {},
  590. breakingProcess = null,
  591. inGlobalTouch = false;
  592. $(window).bind("touchstart" + eventClassName, function (e) {
  593. inGlobalTouch = true;
  594. });
  595. $(window).bind("touchend" + eventClassName, function (e) {
  596. inGlobalTouch = false;
  597. });
  598. $this.bind("touchstart" + eventClassName, function (e) {
  599. var touch = e.originalEvent.targetTouches[0];
  600. startCoords.pageX = touch.pageX;
  601. startCoords.pageY = touch.pageY;
  602. startTime = (new Date()).getTime();
  603. if (breakingProcess !== null) {
  604. clearInterval(breakingProcess);
  605. }
  606. e.stopPropagation();
  607. });
  608. $this.bind("touchmove" + eventClassName, function (e) {
  609. if (!inGlobalTouch && e.originalEvent.targetTouches.length === 1) {
  610. var touch = e.originalEvent.targetTouches[0];
  611. var currentCoords = {};
  612. currentCoords.pageX = touch.pageX;
  613. currentCoords.pageY = touch.pageY;
  614. var differenceX = currentCoords.pageX - startCoords.pageX,
  615. differenceY = currentCoords.pageY - startCoords.pageY;
  616. applyTouchMove(differenceX, differenceY);
  617. startCoords = currentCoords;
  618. var currentTime = (new Date()).getTime();
  619. var timeGap = currentTime - startTime;
  620. if (timeGap > 0) {
  621. speed.x = differenceX / timeGap;
  622. speed.y = differenceY / timeGap;
  623. startTime = currentTime;
  624. }
  625. e.preventDefault();
  626. }
  627. });
  628. $this.bind("touchend" + eventClassName, function (e) {
  629. clearInterval(breakingProcess);
  630. breakingProcess = setInterval(function () {
  631. if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) {
  632. clearInterval(breakingProcess);
  633. return;
  634. }
  635. applyTouchMove(speed.x * 30, speed.y * 30);
  636. speed.x *= 0.8;
  637. speed.y *= 0.8;
  638. }, 10);
  639. });
  640. };
  641. var bindScrollHandler = function () {
  642. $this.bind('scroll' + eventClassName, function (e) {
  643. updateBarSizeAndPosition();
  644. });
  645. };
  646. var destroy = function () {
  647. $this.unbind(eventClassName);
  648. $(window).unbind(eventClassName);
  649. $(document).unbind(eventClassName);
  650. $this.data('perfect-scrollbar', null);
  651. $this.data('perfect-scrollbar-update', null);
  652. $this.data('perfect-scrollbar-destroy', null);
  653. $scrollbarX.remove();
  654. $scrollbarY.remove();
  655. $scrollbarXRail.remove();
  656. $scrollbarYRail.remove();
  657. // clean all variables
  658. $scrollbarXRail =
  659. $scrollbarYRail =
  660. $scrollbarX =
  661. $scrollbarY =
  662. scrollbarXActive =
  663. scrollbarYActive =
  664. containerWidth =
  665. containerHeight =
  666. contentWidth =
  667. contentHeight =
  668. scrollbarXWidth =
  669. scrollbarXLeft =
  670. scrollbarXBottom =
  671. isScrollbarXUsingBottom =
  672. scrollbarXTop =
  673. scrollbarYHeight =
  674. scrollbarYTop =
  675. scrollbarYRight =
  676. isScrollbarYUsingRight =
  677. scrollbarYLeft =
  678. isRtl =
  679. eventClassName = null;
  680. };
  681. var ieSupport = function (version) {
  682. $this.addClass('ie').addClass('ie' + version);
  683. var bindHoverHandlers = function () {
  684. var mouseenter = function () {
  685. $(this).addClass('hover');
  686. };
  687. var mouseleave = function () {
  688. $(this).removeClass('hover');
  689. };
  690. $this.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
  691. $scrollbarXRail.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
  692. $scrollbarYRail.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
  693. $scrollbarX.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
  694. $scrollbarY.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
  695. };
  696. var fixIe6ScrollbarPosition = function () {
  697. updateScrollbarCss = function () {
  698. var scrollbarXStyles = {left: scrollbarXLeft + $this.scrollLeft(), width: scrollbarXWidth};
  699. if (isScrollbarXUsingBottom) {
  700. scrollbarXStyles.bottom = scrollbarXBottom;
  701. } else {
  702. scrollbarXStyles.top = scrollbarXTop;
  703. }
  704. $scrollbarX.css(scrollbarXStyles);
  705. var scrollbarYStyles = {top: scrollbarYTop + $this.scrollTop(), height: scrollbarYHeight};
  706. if (isScrollbarYUsingRight) {
  707. scrollbarYStyles.right = scrollbarYRight;
  708. } else {
  709. scrollbarYStyles.left = scrollbarYLeft;
  710. }
  711. $scrollbarY.css(scrollbarYStyles);
  712. $scrollbarX.hide().show();
  713. $scrollbarY.hide().show();
  714. };
  715. };
  716. if (version === 6) {
  717. bindHoverHandlers();
  718. fixIe6ScrollbarPosition();
  719. }
  720. };
  721. var supportsTouch = (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch);
  722. var initialize = function () {
  723. var ieMatch = navigator.userAgent.toLowerCase().match(/(msie) ([\w.]+)/);
  724. if (ieMatch && ieMatch[1] === 'msie') {
  725. // must be executed at first, because 'ieSupport' may addClass to the container
  726. ieSupport(parseInt(ieMatch[2], 10));
  727. }
  728. updateBarSizeAndPosition();
  729. bindScrollHandler();
  730. bindMouseScrollXHandler();
  731. bindMouseScrollYHandler();
  732. bindRailClickHandler();
  733. if (supportsTouch) {
  734. bindMobileTouchHandler();
  735. }
  736. if ($this.mousewheel) {
  737. bindMouseWheelHandler();
  738. }
  739. if (settings.useKeyboard) {
  740. bindKeyboardHandler();
  741. }
  742. $this.data('perfect-scrollbar', $this);
  743. $this.data('perfect-scrollbar-update', updateBarSizeAndPosition);
  744. $this.data('perfect-scrollbar-destroy', destroy);
  745. };
  746. // initialize
  747. initialize();
  748. return $this;
  749. });
  750. };
  751. }));