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.

2148 lines
53 KiB

  1. /*! iScroll v5.2.0 ~ (c) 2008-2016 Matteo Spinelli ~ http://cubiq.org/license */
  2. (function (window, document, Math) {
  3. var rAF = window.requestAnimationFrame ||
  4. window.webkitRequestAnimationFrame ||
  5. window.mozRequestAnimationFrame ||
  6. window.oRequestAnimationFrame ||
  7. window.msRequestAnimationFrame ||
  8. function (callback) { window.setTimeout(callback, 1000 / 60); };
  9. var utils = (function () {
  10. var me = {};
  11. var _elementStyle = document.createElement('div').style;
  12. var _vendor = (function () {
  13. var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'],
  14. transform,
  15. i = 0,
  16. l = vendors.length;
  17. for ( ; i < l; i++ ) {
  18. transform = vendors[i] + 'ransform';
  19. if ( transform in _elementStyle ) return vendors[i].substr(0, vendors[i].length-1);
  20. }
  21. return false;
  22. })();
  23. function _prefixStyle (style) {
  24. if ( _vendor === false ) return false;
  25. if ( _vendor === '' ) return style;
  26. return _vendor + style.charAt(0).toUpperCase() + style.substr(1);
  27. }
  28. me.getTime = Date.now || function getTime () { return new Date().getTime(); };
  29. me.extend = function (target, obj) {
  30. for ( var i in obj ) {
  31. target[i] = obj[i];
  32. }
  33. };
  34. me.addEvent = function (el, type, fn, capture) {
  35. el.addEventListener(type, fn, !!capture);
  36. };
  37. me.removeEvent = function (el, type, fn, capture) {
  38. el.removeEventListener(type, fn, !!capture);
  39. };
  40. me.prefixPointerEvent = function (pointerEvent) {
  41. return window.MSPointerEvent ?
  42. 'MSPointer' + pointerEvent.charAt(7).toUpperCase() + pointerEvent.substr(8):
  43. pointerEvent;
  44. };
  45. me.momentum = function (current, start, time, lowerMargin, wrapperSize, deceleration) {
  46. var distance = current - start,
  47. speed = Math.abs(distance) / time,
  48. destination,
  49. duration;
  50. deceleration = deceleration === undefined ? 0.0006 : deceleration;
  51. destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
  52. duration = speed / deceleration;
  53. if ( destination < lowerMargin ) {
  54. destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
  55. distance = Math.abs(destination - current);
  56. duration = distance / speed;
  57. } else if ( destination > 0 ) {
  58. destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
  59. distance = Math.abs(current) + destination;
  60. duration = distance / speed;
  61. }
  62. return {
  63. destination: Math.round(destination),
  64. duration: duration
  65. };
  66. };
  67. var _transform = _prefixStyle('transform');
  68. me.extend(me, {
  69. hasTransform: _transform !== false,
  70. hasPerspective: _prefixStyle('perspective') in _elementStyle,
  71. hasTouch: 'ontouchstart' in window,
  72. hasPointer: !!(window.PointerEvent || window.MSPointerEvent), // IE10 is prefixed
  73. hasTransition: _prefixStyle('transition') in _elementStyle
  74. });
  75. /*
  76. This should find all Android browsers lower than build 535.19 (both stock browser and webview)
  77. - galaxy S2 is ok
  78. - 2.3.6 : `AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1`
  79. - 4.0.4 : `AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
  80. - galaxy S3 is badAndroid (stock brower, webview)
  81. `AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
  82. - galaxy S4 is badAndroid (stock brower, webview)
  83. `AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
  84. - galaxy S5 is OK
  85. `AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36 (Chrome/)`
  86. - galaxy S6 is OK
  87. `AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36 (Chrome/)`
  88. */
  89. me.isBadAndroid = (function() {
  90. var appVersion = window.navigator.appVersion;
  91. // Android browser is not a chrome browser.
  92. if (/Android/.test(appVersion) && !(/Chrome\/\d/.test(appVersion))) {
  93. var safariVersion = appVersion.match(/Safari\/(\d+.\d)/);
  94. if(safariVersion && typeof safariVersion === "object" && safariVersion.length >= 2) {
  95. return parseFloat(safariVersion[1]) < 535.19;
  96. } else {
  97. return true;
  98. }
  99. } else {
  100. return false;
  101. }
  102. })();
  103. me.extend(me.style = {}, {
  104. transform: _transform,
  105. transitionTimingFunction: _prefixStyle('transitionTimingFunction'),
  106. transitionDuration: _prefixStyle('transitionDuration'),
  107. transitionDelay: _prefixStyle('transitionDelay'),
  108. transformOrigin: _prefixStyle('transformOrigin')
  109. });
  110. me.hasClass = function (e, c) {
  111. var re = new RegExp("(^|\\s)" + c + "(\\s|$)");
  112. return re.test(e.className);
  113. };
  114. me.addClass = function (e, c) {
  115. if ( me.hasClass(e, c) ) {
  116. return;
  117. }
  118. var newclass = e.className.split(' ');
  119. newclass.push(c);
  120. e.className = newclass.join(' ');
  121. };
  122. me.removeClass = function (e, c) {
  123. if ( !me.hasClass(e, c) ) {
  124. return;
  125. }
  126. var re = new RegExp("(^|\\s)" + c + "(\\s|$)", 'g');
  127. e.className = e.className.replace(re, ' ');
  128. };
  129. me.offset = function (el) {
  130. var left = -el.offsetLeft,
  131. top = -el.offsetTop;
  132. // jshint -W084
  133. while (el = el.offsetParent) {
  134. left -= el.offsetLeft;
  135. top -= el.offsetTop;
  136. }
  137. // jshint +W084
  138. return {
  139. left: left,
  140. top: top
  141. };
  142. };
  143. me.preventDefaultException = function (el, exceptions) {
  144. for ( var i in exceptions ) {
  145. if ( exceptions[i].test(el[i]) ) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. };
  151. me.extend(me.eventType = {}, {
  152. touchstart: 1,
  153. touchmove: 1,
  154. touchend: 1,
  155. mousedown: 2,
  156. mousemove: 2,
  157. mouseup: 2,
  158. pointerdown: 3,
  159. pointermove: 3,
  160. pointerup: 3,
  161. MSPointerDown: 3,
  162. MSPointerMove: 3,
  163. MSPointerUp: 3
  164. });
  165. me.extend(me.ease = {}, {
  166. quadratic: {
  167. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  168. fn: function (k) {
  169. return k * ( 2 - k );
  170. }
  171. },
  172. circular: {
  173. style: 'cubic-bezier(0.1, 0.57, 0.1, 1)', // Not properly "circular" but this looks better, it should be (0.075, 0.82, 0.165, 1)
  174. fn: function (k) {
  175. return Math.sqrt( 1 - ( --k * k ) );
  176. }
  177. },
  178. back: {
  179. style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
  180. fn: function (k) {
  181. var b = 4;
  182. return ( k = k - 1 ) * k * ( ( b + 1 ) * k + b ) + 1;
  183. }
  184. },
  185. bounce: {
  186. style: '',
  187. fn: function (k) {
  188. if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
  189. return 7.5625 * k * k;
  190. } else if ( k < ( 2 / 2.75 ) ) {
  191. return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
  192. } else if ( k < ( 2.5 / 2.75 ) ) {
  193. return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
  194. } else {
  195. return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
  196. }
  197. }
  198. },
  199. elastic: {
  200. style: '',
  201. fn: function (k) {
  202. var f = 0.22,
  203. e = 0.4;
  204. if ( k === 0 ) { return 0; }
  205. if ( k == 1 ) { return 1; }
  206. return ( e * Math.pow( 2, - 10 * k ) * Math.sin( ( k - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 );
  207. }
  208. }
  209. });
  210. me.tap = function (e, eventName) {
  211. var ev = document.createEvent('Event');
  212. ev.initEvent(eventName, true, true);
  213. ev.pageX = e.pageX;
  214. ev.pageY = e.pageY;
  215. e.target.dispatchEvent(ev);
  216. };
  217. me.click = function (e) {
  218. var target = e.target,
  219. ev;
  220. if ( !(/(SELECT|INPUT|TEXTAREA)/i).test(target.tagName) ) {
  221. // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/initMouseEvent
  222. // initMouseEvent is deprecated.
  223. ev = document.createEvent(window.MouseEvent ? 'MouseEvents' : 'Event');
  224. ev.initEvent('click', true, true);
  225. ev.view = e.view || window;
  226. ev.detail = 1;
  227. ev.screenX = target.screenX || 0;
  228. ev.screenY = target.screenY || 0;
  229. ev.clientX = target.clientX || 0;
  230. ev.clientY = target.clientY || 0;
  231. ev.ctrlKey = !!e.ctrlKey;
  232. ev.altKey = !!e.altKey;
  233. ev.shiftKey = !!e.shiftKey;
  234. ev.metaKey = !!e.metaKey;
  235. ev.button = 0;
  236. ev.relatedTarget = null;
  237. ev._constructed = true;
  238. target.dispatchEvent(ev);
  239. }
  240. };
  241. return me;
  242. })();
  243. function IScroll (el, options) {
  244. this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
  245. this.scroller = this.wrapper.children[0];
  246. this.scrollerStyle = this.scroller.style; // cache style for better performance
  247. this.options = {
  248. resizeScrollbars: true,
  249. mouseWheelSpeed: 20,
  250. snapThreshold: 0.334,
  251. // INSERT POINT: OPTIONS
  252. disablePointer : !utils.hasPointer,
  253. disableTouch : utils.hasPointer || !utils.hasTouch,
  254. disableMouse : utils.hasPointer || utils.hasTouch,
  255. startX: 0,
  256. startY: 0,
  257. scrollY: true,
  258. directionLockThreshold: 5,
  259. momentum: true,
  260. bounce: true,
  261. bounceTime: 600,
  262. bounceEasing: '',
  263. preventDefault: true,
  264. preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ },
  265. HWCompositing: true,
  266. useTransition: true,
  267. useTransform: true,
  268. bindToWrapper: typeof window.onmousedown === "undefined"
  269. };
  270. for ( var i in options ) {
  271. this.options[i] = options[i];
  272. }
  273. // Normalize options
  274. this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
  275. this.options.useTransition = utils.hasTransition && this.options.useTransition;
  276. this.options.useTransform = utils.hasTransform && this.options.useTransform;
  277. this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
  278. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  279. // If you want eventPassthrough I have to lock one of the axes
  280. this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
  281. this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
  282. // With eventPassthrough we also need lockDirection mechanism
  283. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  284. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  285. this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
  286. this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
  287. if ( this.options.tap === true ) {
  288. this.options.tap = 'tap';
  289. }
  290. // https://github.com/cubiq/iscroll/issues/1029
  291. if (!this.options.useTransition && !this.options.useTransform) {
  292. if(!(/relative|absolute/i).test(this.scrollerStyle.position)) {
  293. this.scrollerStyle.position = "relative";
  294. }
  295. }
  296. if ( this.options.shrinkScrollbars == 'scale' ) {
  297. this.options.useTransition = false;
  298. }
  299. this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
  300. if ( this.options.probeType == 3 ) {
  301. this.options.useTransition = false; }
  302. // INSERT POINT: NORMALIZATION
  303. // Some defaults
  304. this.x = 0;
  305. this.y = 0;
  306. this.directionX = 0;
  307. this.directionY = 0;
  308. this._events = {};
  309. // INSERT POINT: DEFAULTS
  310. this._init();
  311. this.refresh();
  312. this.scrollTo(this.options.startX, this.options.startY);
  313. this.enable();
  314. }
  315. IScroll.prototype = {
  316. version: '5.2.0',
  317. _init: function () {
  318. this._initEvents();
  319. if ( this.options.scrollbars || this.options.indicators ) {
  320. this._initIndicators();
  321. }
  322. if ( this.options.mouseWheel ) {
  323. this._initWheel();
  324. }
  325. if ( this.options.snap ) {
  326. this._initSnap();
  327. }
  328. if ( this.options.keyBindings ) {
  329. this._initKeys();
  330. }
  331. // INSERT POINT: _init
  332. },
  333. destroy: function () {
  334. this._initEvents(true);
  335. clearTimeout(this.resizeTimeout);
  336. this.resizeTimeout = null;
  337. this._execEvent('destroy');
  338. },
  339. _transitionEnd: function (e) {
  340. if ( e.target != this.scroller || !this.isInTransition ) {
  341. return;
  342. }
  343. this._transitionTime();
  344. if ( !this.resetPosition(this.options.bounceTime) ) {
  345. this.isInTransition = false;
  346. this._execEvent('scrollEnd');
  347. }
  348. },
  349. _start: function (e) {
  350. // React to left mouse button only
  351. if ( utils.eventType[e.type] != 1 ) {
  352. // for button property
  353. // http://unixpapa.com/js/mouse.html
  354. var button;
  355. if (!e.which) {
  356. /* IE case */
  357. button = (e.button < 2) ? 0 :
  358. ((e.button == 4) ? 1 : 2);
  359. } else {
  360. /* All others */
  361. button = e.button;
  362. }
  363. if ( button !== 0 ) {
  364. return;
  365. }
  366. }
  367. if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
  368. return;
  369. }
  370. if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  371. e.preventDefault();
  372. }
  373. var point = e.touches ? e.touches[0] : e,
  374. pos;
  375. this.initiated = utils.eventType[e.type];
  376. this.moved = false;
  377. this.distX = 0;
  378. this.distY = 0;
  379. this.directionX = 0;
  380. this.directionY = 0;
  381. this.directionLocked = 0;
  382. this.startTime = utils.getTime();
  383. if ( this.options.useTransition && this.isInTransition ) {
  384. this._transitionTime();
  385. this.isInTransition = false;
  386. pos = this.getComputedPosition();
  387. this._translate(Math.round(pos.x), Math.round(pos.y));
  388. this._execEvent('scrollEnd');
  389. } else if ( !this.options.useTransition && this.isAnimating ) {
  390. this.isAnimating = false;
  391. this._execEvent('scrollEnd');
  392. }
  393. this.startX = this.x;
  394. this.startY = this.y;
  395. this.absStartX = this.x;
  396. this.absStartY = this.y;
  397. this.pointX = point.pageX;
  398. this.pointY = point.pageY;
  399. this._execEvent('beforeScrollStart');
  400. },
  401. _move: function (e) {
  402. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  403. return;
  404. }
  405. if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
  406. e.preventDefault();
  407. }
  408. var point = e.touches ? e.touches[0] : e,
  409. deltaX = point.pageX - this.pointX,
  410. deltaY = point.pageY - this.pointY,
  411. timestamp = utils.getTime(),
  412. newX, newY,
  413. absDistX, absDistY;
  414. this.pointX = point.pageX;
  415. this.pointY = point.pageY;
  416. this.distX += deltaX;
  417. this.distY += deltaY;
  418. absDistX = Math.abs(this.distX);
  419. absDistY = Math.abs(this.distY);
  420. // We need to move at least 10 pixels for the scrolling to initiate
  421. if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
  422. return;
  423. }
  424. // If you are scrolling in one direction lock the other
  425. if ( !this.directionLocked && !this.options.freeScroll ) {
  426. if ( absDistX > absDistY + this.options.directionLockThreshold ) {
  427. this.directionLocked = 'h'; // lock horizontally
  428. } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
  429. this.directionLocked = 'v'; // lock vertically
  430. } else {
  431. this.directionLocked = 'n'; // no lock
  432. }
  433. }
  434. if ( this.directionLocked == 'h' ) {
  435. if ( this.options.eventPassthrough == 'vertical' ) {
  436. e.preventDefault();
  437. } else if ( this.options.eventPassthrough == 'horizontal' ) {
  438. this.initiated = false;
  439. return;
  440. }
  441. deltaY = 0;
  442. } else if ( this.directionLocked == 'v' ) {
  443. if ( this.options.eventPassthrough == 'horizontal' ) {
  444. e.preventDefault();
  445. } else if ( this.options.eventPassthrough == 'vertical' ) {
  446. this.initiated = false;
  447. return;
  448. }
  449. deltaX = 0;
  450. }
  451. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  452. deltaY = this.hasVerticalScroll ? deltaY : 0;
  453. newX = this.x + deltaX;
  454. newY = this.y + deltaY;
  455. // Slow down if outside of the boundaries
  456. if ( newX > 0 || newX < this.maxScrollX ) {
  457. newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
  458. }
  459. if ( newY > 0 || newY < this.maxScrollY ) {
  460. newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
  461. }
  462. this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
  463. this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
  464. if ( !this.moved ) {
  465. this._execEvent('scrollStart');
  466. }
  467. this.moved = true;
  468. this._translate(newX, newY);
  469. /* REPLACE START: _move */
  470. if ( timestamp - this.startTime > 300 ) {
  471. this.startTime = timestamp;
  472. this.startX = this.x;
  473. this.startY = this.y;
  474. if ( this.options.probeType == 1 ) {
  475. this._execEvent('scroll');
  476. }
  477. }
  478. if ( this.options.probeType > 1 ) {
  479. this._execEvent('scroll');
  480. }
  481. /* REPLACE END: _move */
  482. },
  483. _end: function (e) {
  484. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  485. return;
  486. }
  487. if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  488. e.preventDefault();
  489. }
  490. var point = e.changedTouches ? e.changedTouches[0] : e,
  491. momentumX,
  492. momentumY,
  493. duration = utils.getTime() - this.startTime,
  494. newX = Math.round(this.x),
  495. newY = Math.round(this.y),
  496. distanceX = Math.abs(newX - this.startX),
  497. distanceY = Math.abs(newY - this.startY),
  498. time = 0,
  499. easing = '';
  500. this.isInTransition = 0;
  501. this.initiated = 0;
  502. this.endTime = utils.getTime();
  503. // reset if we are outside of the boundaries
  504. if ( this.resetPosition(this.options.bounceTime) ) {
  505. return;
  506. }
  507. this.scrollTo(newX, newY); // ensures that the last position is rounded
  508. // we scrolled less than 10 pixels
  509. if ( !this.moved ) {
  510. if ( this.options.tap ) {
  511. utils.tap(e, this.options.tap);
  512. }
  513. if ( this.options.click ) {
  514. utils.click(e);
  515. }
  516. this._execEvent('scrollCancel');
  517. return;
  518. }
  519. if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
  520. this._execEvent('flick');
  521. return;
  522. }
  523. // start momentum animation if needed
  524. if ( this.options.momentum && duration < 300 ) {
  525. momentumX = this.hasHorizontalScroll ? utils.momentum(this.x, this.startX, duration, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0, this.options.deceleration) : { destination: newX, duration: 0 };
  526. momentumY = this.hasVerticalScroll ? utils.momentum(this.y, this.startY, duration, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0, this.options.deceleration) : { destination: newY, duration: 0 };
  527. newX = momentumX.destination;
  528. newY = momentumY.destination;
  529. time = Math.max(momentumX.duration, momentumY.duration);
  530. this.isInTransition = 1;
  531. }
  532. if ( this.options.snap ) {
  533. var snap = this._nearestSnap(newX, newY);
  534. this.currentPage = snap;
  535. time = this.options.snapSpeed || Math.max(
  536. Math.max(
  537. Math.min(Math.abs(newX - snap.x), 1000),
  538. Math.min(Math.abs(newY - snap.y), 1000)
  539. ), 300);
  540. newX = snap.x;
  541. newY = snap.y;
  542. this.directionX = 0;
  543. this.directionY = 0;
  544. easing = this.options.bounceEasing;
  545. }
  546. // INSERT POINT: _end
  547. if ( newX != this.x || newY != this.y ) {
  548. // change easing function when scroller goes out of the boundaries
  549. if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
  550. easing = utils.ease.quadratic;
  551. }
  552. this.scrollTo(newX, newY, time, easing);
  553. return;
  554. }
  555. this._execEvent('scrollEnd');
  556. },
  557. _resize: function () {
  558. var that = this;
  559. clearTimeout(this.resizeTimeout);
  560. this.resizeTimeout = setTimeout(function () {
  561. that.refresh();
  562. }, this.options.resizePolling);
  563. },
  564. resetPosition: function (time) {
  565. var x = this.x,
  566. y = this.y;
  567. time = time || 0;
  568. if ( !this.hasHorizontalScroll || this.x > 0 ) {
  569. x = 0;
  570. } else if ( this.x < this.maxScrollX ) {
  571. x = this.maxScrollX;
  572. }
  573. if ( !this.hasVerticalScroll || this.y > 0 ) {
  574. y = 0;
  575. } else if ( this.y < this.maxScrollY ) {
  576. y = this.maxScrollY;
  577. }
  578. if ( x == this.x && y == this.y ) {
  579. return false;
  580. }
  581. this.scrollTo(x, y, time, this.options.bounceEasing);
  582. return true;
  583. },
  584. disable: function () {
  585. this.enabled = false;
  586. },
  587. enable: function () {
  588. this.enabled = true;
  589. },
  590. refresh: function () {
  591. var rf = this.wrapper.offsetHeight; // Force reflow
  592. this.wrapperWidth = this.wrapper.clientWidth;
  593. this.wrapperHeight = this.wrapper.clientHeight;
  594. /* REPLACE START: refresh */
  595. this.scrollerWidth = this.scroller.offsetWidth;
  596. this.scrollerHeight = this.scroller.offsetHeight;
  597. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  598. this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  599. /* REPLACE END: refresh */
  600. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  601. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  602. if ( !this.hasHorizontalScroll ) {
  603. this.maxScrollX = 0;
  604. this.scrollerWidth = this.wrapperWidth;
  605. }
  606. if ( !this.hasVerticalScroll ) {
  607. this.maxScrollY = 0;
  608. this.scrollerHeight = this.wrapperHeight;
  609. }
  610. this.endTime = 0;
  611. this.directionX = 0;
  612. this.directionY = 0;
  613. this.wrapperOffset = utils.offset(this.wrapper);
  614. this._execEvent('refresh');
  615. this.resetPosition();
  616. // INSERT POINT: _refresh
  617. },
  618. on: function (type, fn) {
  619. if ( !this._events[type] ) {
  620. this._events[type] = [];
  621. }
  622. this._events[type].push(fn);
  623. },
  624. off: function (type, fn) {
  625. if ( !this._events[type] ) {
  626. return;
  627. }
  628. var index = this._events[type].indexOf(fn);
  629. if ( index > -1 ) {
  630. this._events[type].splice(index, 1);
  631. }
  632. },
  633. _execEvent: function (type) {
  634. if ( !this._events[type] ) {
  635. return;
  636. }
  637. var i = 0,
  638. l = this._events[type].length;
  639. if ( !l ) {
  640. return;
  641. }
  642. for ( ; i < l; i++ ) {
  643. this._events[type][i].apply(this, [].slice.call(arguments, 1));
  644. }
  645. },
  646. scrollBy: function (x, y, time, easing) {
  647. x = this.x + x;
  648. y = this.y + y;
  649. time = time || 0;
  650. this.scrollTo(x, y, time, easing);
  651. },
  652. scrollTo: function (x, y, time, easing) {
  653. easing = easing || utils.ease.circular;
  654. this.isInTransition = this.options.useTransition && time > 0;
  655. var transitionType = this.options.useTransition && easing.style;
  656. if ( !time || transitionType ) {
  657. if(transitionType) {
  658. this._transitionTimingFunction(easing.style);
  659. this._transitionTime(time);
  660. }
  661. this._translate(x, y);
  662. } else {
  663. this._animate(x, y, time, easing.fn);
  664. }
  665. },
  666. scrollToElement: function (el, time, offsetX, offsetY, easing) {
  667. el = el.nodeType ? el : this.scroller.querySelector(el);
  668. if ( !el ) {
  669. return;
  670. }
  671. var pos = utils.offset(el);
  672. pos.left -= this.wrapperOffset.left;
  673. pos.top -= this.wrapperOffset.top;
  674. // if offsetX/Y are true we center the element to the screen
  675. if ( offsetX === true ) {
  676. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  677. }
  678. if ( offsetY === true ) {
  679. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  680. }
  681. pos.left -= offsetX || 0;
  682. pos.top -= offsetY || 0;
  683. pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  684. pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  685. time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
  686. this.scrollTo(pos.left, pos.top, time, easing);
  687. },
  688. _transitionTime: function (time) {
  689. if (!this.options.useTransition) {
  690. return;
  691. }
  692. time = time || 0;
  693. var durationProp = utils.style.transitionDuration;
  694. if(!durationProp) {
  695. return;
  696. }
  697. this.scrollerStyle[durationProp] = time + 'ms';
  698. if ( !time && utils.isBadAndroid ) {
  699. this.scrollerStyle[durationProp] = '0.0001ms';
  700. // remove 0.0001ms
  701. var self = this;
  702. rAF(function() {
  703. if(self.scrollerStyle[durationProp] === '0.0001ms') {
  704. self.scrollerStyle[durationProp] = '0s';
  705. }
  706. });
  707. }
  708. if ( this.indicators ) {
  709. for ( var i = this.indicators.length; i--; ) {
  710. this.indicators[i].transitionTime(time);
  711. }
  712. }
  713. // INSERT POINT: _transitionTime
  714. },
  715. _transitionTimingFunction: function (easing) {
  716. this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
  717. if ( this.indicators ) {
  718. for ( var i = this.indicators.length; i--; ) {
  719. this.indicators[i].transitionTimingFunction(easing);
  720. }
  721. }
  722. // INSERT POINT: _transitionTimingFunction
  723. },
  724. _translate: function (x, y) {
  725. if ( this.options.useTransform ) {
  726. /* REPLACE START: _translate */
  727. this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
  728. /* REPLACE END: _translate */
  729. } else {
  730. x = Math.round(x);
  731. y = Math.round(y);
  732. this.scrollerStyle.left = x + 'px';
  733. this.scrollerStyle.top = y + 'px';
  734. }
  735. this.x = x;
  736. this.y = y;
  737. if ( this.indicators ) {
  738. for ( var i = this.indicators.length; i--; ) {
  739. this.indicators[i].updatePosition();
  740. }
  741. }
  742. // INSERT POINT: _translate
  743. },
  744. _initEvents: function (remove) {
  745. var eventType = remove ? utils.removeEvent : utils.addEvent,
  746. target = this.options.bindToWrapper ? this.wrapper : window;
  747. eventType(window, 'orientationchange', this);
  748. eventType(window, 'resize', this);
  749. if ( this.options.click ) {
  750. eventType(this.wrapper, 'click', this, true);
  751. }
  752. if ( !this.options.disableMouse ) {
  753. eventType(this.wrapper, 'mousedown', this);
  754. eventType(target, 'mousemove', this);
  755. eventType(target, 'mousecancel', this);
  756. eventType(target, 'mouseup', this);
  757. }
  758. if ( utils.hasPointer && !this.options.disablePointer ) {
  759. eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
  760. eventType(target, utils.prefixPointerEvent('pointermove'), this);
  761. eventType(target, utils.prefixPointerEvent('pointercancel'), this);
  762. eventType(target, utils.prefixPointerEvent('pointerup'), this);
  763. }
  764. if ( utils.hasTouch && !this.options.disableTouch ) {
  765. eventType(this.wrapper, 'touchstart', this);
  766. eventType(target, 'touchmove', this);
  767. eventType(target, 'touchcancel', this);
  768. eventType(target, 'touchend', this);
  769. }
  770. eventType(this.scroller, 'transitionend', this);
  771. eventType(this.scroller, 'webkitTransitionEnd', this);
  772. eventType(this.scroller, 'oTransitionEnd', this);
  773. eventType(this.scroller, 'MSTransitionEnd', this);
  774. },
  775. getComputedPosition: function () {
  776. var matrix = window.getComputedStyle(this.scroller, null),
  777. x, y;
  778. if ( this.options.useTransform ) {
  779. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  780. x = +(matrix[12] || matrix[4]);
  781. y = +(matrix[13] || matrix[5]);
  782. } else {
  783. x = +matrix.left.replace(/[^-\d.]/g, '');
  784. y = +matrix.top.replace(/[^-\d.]/g, '');
  785. }
  786. return { x: x, y: y };
  787. },
  788. _initIndicators: function () {
  789. var interactive = this.options.interactiveScrollbars,
  790. customStyle = typeof this.options.scrollbars != 'string',
  791. indicators = [],
  792. indicator;
  793. var that = this;
  794. this.indicators = [];
  795. if ( this.options.scrollbars ) {
  796. // Vertical scrollbar
  797. if ( this.options.scrollY ) {
  798. indicator = {
  799. el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
  800. interactive: interactive,
  801. defaultScrollbars: true,
  802. customStyle: customStyle,
  803. resize: this.options.resizeScrollbars,
  804. shrink: this.options.shrinkScrollbars,
  805. fade: this.options.fadeScrollbars,
  806. listenX: false
  807. };
  808. this.wrapper.appendChild(indicator.el);
  809. indicators.push(indicator);
  810. }
  811. // Horizontal scrollbar
  812. if ( this.options.scrollX ) {
  813. indicator = {
  814. el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
  815. interactive: interactive,
  816. defaultScrollbars: true,
  817. customStyle: customStyle,
  818. resize: this.options.resizeScrollbars,
  819. shrink: this.options.shrinkScrollbars,
  820. fade: this.options.fadeScrollbars,
  821. listenY: false
  822. };
  823. this.wrapper.appendChild(indicator.el);
  824. indicators.push(indicator);
  825. }
  826. }
  827. if ( this.options.indicators ) {
  828. // TODO: check concat compatibility
  829. indicators = indicators.concat(this.options.indicators);
  830. }
  831. for ( var i = indicators.length; i--; ) {
  832. this.indicators.push( new Indicator(this, indicators[i]) );
  833. }
  834. // TODO: check if we can use array.map (wide compatibility and performance issues)
  835. function _indicatorsMap (fn) {
  836. if (that.indicators) {
  837. for ( var i = that.indicators.length; i--; ) {
  838. fn.call(that.indicators[i]);
  839. }
  840. }
  841. }
  842. if ( this.options.fadeScrollbars ) {
  843. this.on('scrollEnd', function () {
  844. _indicatorsMap(function () {
  845. this.fade();
  846. });
  847. });
  848. this.on('scrollCancel', function () {
  849. _indicatorsMap(function () {
  850. this.fade();
  851. });
  852. });
  853. this.on('scrollStart', function () {
  854. _indicatorsMap(function () {
  855. this.fade(1);
  856. });
  857. });
  858. this.on('beforeScrollStart', function () {
  859. _indicatorsMap(function () {
  860. this.fade(1, true);
  861. });
  862. });
  863. }
  864. this.on('refresh', function () {
  865. _indicatorsMap(function () {
  866. this.refresh();
  867. });
  868. });
  869. this.on('destroy', function () {
  870. _indicatorsMap(function () {
  871. this.destroy();
  872. });
  873. delete this.indicators;
  874. });
  875. },
  876. _initWheel: function () {
  877. utils.addEvent(this.wrapper, 'wheel', this);
  878. utils.addEvent(this.wrapper, 'mousewheel', this);
  879. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  880. this.on('destroy', function () {
  881. clearTimeout(this.wheelTimeout);
  882. this.wheelTimeout = null;
  883. utils.removeEvent(this.wrapper, 'wheel', this);
  884. utils.removeEvent(this.wrapper, 'mousewheel', this);
  885. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  886. });
  887. },
  888. _wheel: function (e) {
  889. if ( !this.enabled ) {
  890. return;
  891. }
  892. e.preventDefault();
  893. var wheelDeltaX, wheelDeltaY,
  894. newX, newY,
  895. that = this;
  896. if ( this.wheelTimeout === undefined ) {
  897. that._execEvent('scrollStart');
  898. }
  899. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  900. clearTimeout(this.wheelTimeout);
  901. this.wheelTimeout = setTimeout(function () {
  902. if(!that.options.snap) {
  903. that._execEvent('scrollEnd');
  904. }
  905. that.wheelTimeout = undefined;
  906. }, 400);
  907. if ( 'deltaX' in e ) {
  908. if (e.deltaMode === 1) {
  909. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  910. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  911. } else {
  912. wheelDeltaX = -e.deltaX;
  913. wheelDeltaY = -e.deltaY;
  914. }
  915. } else if ( 'wheelDeltaX' in e ) {
  916. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  917. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  918. } else if ( 'wheelDelta' in e ) {
  919. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  920. } else if ( 'detail' in e ) {
  921. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  922. } else {
  923. return;
  924. }
  925. wheelDeltaX *= this.options.invertWheelDirection;
  926. wheelDeltaY *= this.options.invertWheelDirection;
  927. if ( !this.hasVerticalScroll ) {
  928. wheelDeltaX = wheelDeltaY;
  929. wheelDeltaY = 0;
  930. }
  931. if ( this.options.snap ) {
  932. newX = this.currentPage.pageX;
  933. newY = this.currentPage.pageY;
  934. if ( wheelDeltaX > 0 ) {
  935. newX--;
  936. } else if ( wheelDeltaX < 0 ) {
  937. newX++;
  938. }
  939. if ( wheelDeltaY > 0 ) {
  940. newY--;
  941. } else if ( wheelDeltaY < 0 ) {
  942. newY++;
  943. }
  944. this.goToPage(newX, newY);
  945. return;
  946. }
  947. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  948. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  949. this.directionX = wheelDeltaX > 0 ? -1 : wheelDeltaX < 0 ? 1 : 0;
  950. this.directionY = wheelDeltaY > 0 ? -1 : wheelDeltaY < 0 ? 1 : 0;
  951. if ( newX > 0 ) {
  952. newX = 0;
  953. } else if ( newX < this.maxScrollX ) {
  954. newX = this.maxScrollX;
  955. }
  956. if ( newY > 0 ) {
  957. newY = 0;
  958. } else if ( newY < this.maxScrollY ) {
  959. newY = this.maxScrollY;
  960. }
  961. this.scrollTo(newX, newY, 0);
  962. if ( this.options.probeType > 1 ) {
  963. this._execEvent('scroll');
  964. }
  965. // INSERT POINT: _wheel
  966. },
  967. _initSnap: function () {
  968. this.currentPage = {};
  969. if ( typeof this.options.snap == 'string' ) {
  970. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  971. }
  972. this.on('refresh', function () {
  973. var i = 0, l,
  974. m = 0, n,
  975. cx, cy,
  976. x = 0, y,
  977. stepX = this.options.snapStepX || this.wrapperWidth,
  978. stepY = this.options.snapStepY || this.wrapperHeight,
  979. el;
  980. this.pages = [];
  981. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  982. return;
  983. }
  984. if ( this.options.snap === true ) {
  985. cx = Math.round( stepX / 2 );
  986. cy = Math.round( stepY / 2 );
  987. while ( x > -this.scrollerWidth ) {
  988. this.pages[i] = [];
  989. l = 0;
  990. y = 0;
  991. while ( y > -this.scrollerHeight ) {
  992. this.pages[i][l] = {
  993. x: Math.max(x, this.maxScrollX),
  994. y: Math.max(y, this.maxScrollY),
  995. width: stepX,
  996. height: stepY,
  997. cx: x - cx,
  998. cy: y - cy
  999. };
  1000. y -= stepY;
  1001. l++;
  1002. }
  1003. x -= stepX;
  1004. i++;
  1005. }
  1006. } else {
  1007. el = this.options.snap;
  1008. l = el.length;
  1009. n = -1;
  1010. for ( ; i < l; i++ ) {
  1011. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  1012. m = 0;
  1013. n++;
  1014. }
  1015. if ( !this.pages[m] ) {
  1016. this.pages[m] = [];
  1017. }
  1018. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  1019. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  1020. cx = x - Math.round(el[i].offsetWidth / 2);
  1021. cy = y - Math.round(el[i].offsetHeight / 2);
  1022. this.pages[m][n] = {
  1023. x: x,
  1024. y: y,
  1025. width: el[i].offsetWidth,
  1026. height: el[i].offsetHeight,
  1027. cx: cx,
  1028. cy: cy
  1029. };
  1030. if ( x > this.maxScrollX ) {
  1031. m++;
  1032. }
  1033. }
  1034. }
  1035. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  1036. // Update snap threshold if needed
  1037. if ( this.options.snapThreshold % 1 === 0 ) {
  1038. this.snapThresholdX = this.options.snapThreshold;
  1039. this.snapThresholdY = this.options.snapThreshold;
  1040. } else {
  1041. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  1042. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  1043. }
  1044. });
  1045. this.on('flick', function () {
  1046. var time = this.options.snapSpeed || Math.max(
  1047. Math.max(
  1048. Math.min(Math.abs(this.x - this.startX), 1000),
  1049. Math.min(Math.abs(this.y - this.startY), 1000)
  1050. ), 300);
  1051. this.goToPage(
  1052. this.currentPage.pageX + this.directionX,
  1053. this.currentPage.pageY + this.directionY,
  1054. time
  1055. );
  1056. });
  1057. },
  1058. _nearestSnap: function (x, y) {
  1059. if ( !this.pages.length ) {
  1060. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1061. }
  1062. var i = 0,
  1063. l = this.pages.length,
  1064. m = 0;
  1065. // Check if we exceeded the snap threshold
  1066. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  1067. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  1068. return this.currentPage;
  1069. }
  1070. if ( x > 0 ) {
  1071. x = 0;
  1072. } else if ( x < this.maxScrollX ) {
  1073. x = this.maxScrollX;
  1074. }
  1075. if ( y > 0 ) {
  1076. y = 0;
  1077. } else if ( y < this.maxScrollY ) {
  1078. y = this.maxScrollY;
  1079. }
  1080. for ( ; i < l; i++ ) {
  1081. if ( x >= this.pages[i][0].cx ) {
  1082. x = this.pages[i][0].x;
  1083. break;
  1084. }
  1085. }
  1086. l = this.pages[i].length;
  1087. for ( ; m < l; m++ ) {
  1088. if ( y >= this.pages[0][m].cy ) {
  1089. y = this.pages[0][m].y;
  1090. break;
  1091. }
  1092. }
  1093. if ( i == this.currentPage.pageX ) {
  1094. i += this.directionX;
  1095. if ( i < 0 ) {
  1096. i = 0;
  1097. } else if ( i >= this.pages.length ) {
  1098. i = this.pages.length - 1;
  1099. }
  1100. x = this.pages[i][0].x;
  1101. }
  1102. if ( m == this.currentPage.pageY ) {
  1103. m += this.directionY;
  1104. if ( m < 0 ) {
  1105. m = 0;
  1106. } else if ( m >= this.pages[0].length ) {
  1107. m = this.pages[0].length - 1;
  1108. }
  1109. y = this.pages[0][m].y;
  1110. }
  1111. return {
  1112. x: x,
  1113. y: y,
  1114. pageX: i,
  1115. pageY: m
  1116. };
  1117. },
  1118. goToPage: function (x, y, time, easing) {
  1119. easing = easing || this.options.bounceEasing;
  1120. if ( x >= this.pages.length ) {
  1121. x = this.pages.length - 1;
  1122. } else if ( x < 0 ) {
  1123. x = 0;
  1124. }
  1125. if ( y >= this.pages[x].length ) {
  1126. y = this.pages[x].length - 1;
  1127. } else if ( y < 0 ) {
  1128. y = 0;
  1129. }
  1130. var posX = this.pages[x][y].x,
  1131. posY = this.pages[x][y].y;
  1132. time = time === undefined ? this.options.snapSpeed || Math.max(
  1133. Math.max(
  1134. Math.min(Math.abs(posX - this.x), 1000),
  1135. Math.min(Math.abs(posY - this.y), 1000)
  1136. ), 300) : time;
  1137. this.currentPage = {
  1138. x: posX,
  1139. y: posY,
  1140. pageX: x,
  1141. pageY: y
  1142. };
  1143. this.scrollTo(posX, posY, time, easing);
  1144. },
  1145. next: function (time, easing) {
  1146. var x = this.currentPage.pageX,
  1147. y = this.currentPage.pageY;
  1148. x++;
  1149. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  1150. x = 0;
  1151. y++;
  1152. }
  1153. this.goToPage(x, y, time, easing);
  1154. },
  1155. prev: function (time, easing) {
  1156. var x = this.currentPage.pageX,
  1157. y = this.currentPage.pageY;
  1158. x--;
  1159. if ( x < 0 && this.hasVerticalScroll ) {
  1160. x = 0;
  1161. y--;
  1162. }
  1163. this.goToPage(x, y, time, easing);
  1164. },
  1165. _initKeys: function (e) {
  1166. // default key bindings
  1167. var keys = {
  1168. pageUp: 33,
  1169. pageDown: 34,
  1170. end: 35,
  1171. home: 36,
  1172. left: 37,
  1173. up: 38,
  1174. right: 39,
  1175. down: 40
  1176. };
  1177. var i;
  1178. // if you give me characters I give you keycode
  1179. if ( typeof this.options.keyBindings == 'object' ) {
  1180. for ( i in this.options.keyBindings ) {
  1181. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1182. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1183. }
  1184. }
  1185. } else {
  1186. this.options.keyBindings = {};
  1187. }
  1188. for ( i in keys ) {
  1189. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1190. }
  1191. utils.addEvent(window, 'keydown', this);
  1192. this.on('destroy', function () {
  1193. utils.removeEvent(window, 'keydown', this);
  1194. });
  1195. },
  1196. _key: function (e) {
  1197. if ( !this.enabled ) {
  1198. return;
  1199. }
  1200. var snap = this.options.snap, // we are using this alot, better to cache it
  1201. newX = snap ? this.currentPage.pageX : this.x,
  1202. newY = snap ? this.currentPage.pageY : this.y,
  1203. now = utils.getTime(),
  1204. prevTime = this.keyTime || 0,
  1205. acceleration = 0.250,
  1206. pos;
  1207. if ( this.options.useTransition && this.isInTransition ) {
  1208. pos = this.getComputedPosition();
  1209. this._translate(Math.round(pos.x), Math.round(pos.y));
  1210. this.isInTransition = false;
  1211. }
  1212. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1213. switch ( e.keyCode ) {
  1214. case this.options.keyBindings.pageUp:
  1215. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1216. newX += snap ? 1 : this.wrapperWidth;
  1217. } else {
  1218. newY += snap ? 1 : this.wrapperHeight;
  1219. }
  1220. break;
  1221. case this.options.keyBindings.pageDown:
  1222. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1223. newX -= snap ? 1 : this.wrapperWidth;
  1224. } else {
  1225. newY -= snap ? 1 : this.wrapperHeight;
  1226. }
  1227. break;
  1228. case this.options.keyBindings.end:
  1229. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1230. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1231. break;
  1232. case this.options.keyBindings.home:
  1233. newX = 0;
  1234. newY = 0;
  1235. break;
  1236. case this.options.keyBindings.left:
  1237. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1238. break;
  1239. case this.options.keyBindings.up:
  1240. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1241. break;
  1242. case this.options.keyBindings.right:
  1243. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1244. break;
  1245. case this.options.keyBindings.down:
  1246. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1247. break;
  1248. default:
  1249. return;
  1250. }
  1251. if ( snap ) {
  1252. this.goToPage(newX, newY);
  1253. return;
  1254. }
  1255. if ( newX > 0 ) {
  1256. newX = 0;
  1257. this.keyAcceleration = 0;
  1258. } else if ( newX < this.maxScrollX ) {
  1259. newX = this.maxScrollX;
  1260. this.keyAcceleration = 0;
  1261. }
  1262. if ( newY > 0 ) {
  1263. newY = 0;
  1264. this.keyAcceleration = 0;
  1265. } else if ( newY < this.maxScrollY ) {
  1266. newY = this.maxScrollY;
  1267. this.keyAcceleration = 0;
  1268. }
  1269. this.scrollTo(newX, newY, 0);
  1270. this.keyTime = now;
  1271. },
  1272. _animate: function (destX, destY, duration, easingFn) {
  1273. var that = this,
  1274. startX = this.x,
  1275. startY = this.y,
  1276. startTime = utils.getTime(),
  1277. destTime = startTime + duration;
  1278. function step () {
  1279. var now = utils.getTime(),
  1280. newX, newY,
  1281. easing;
  1282. if ( now >= destTime ) {
  1283. that.isAnimating = false;
  1284. that._translate(destX, destY);
  1285. if ( !that.resetPosition(that.options.bounceTime) ) {
  1286. that._execEvent('scrollEnd');
  1287. }
  1288. return;
  1289. }
  1290. now = ( now - startTime ) / duration;
  1291. easing = easingFn(now);
  1292. newX = ( destX - startX ) * easing + startX;
  1293. newY = ( destY - startY ) * easing + startY;
  1294. that._translate(newX, newY);
  1295. if ( that.isAnimating ) {
  1296. rAF(step);
  1297. }
  1298. if ( that.options.probeType == 3 ) {
  1299. that._execEvent('scroll');
  1300. }
  1301. }
  1302. this.isAnimating = true;
  1303. step();
  1304. },
  1305. handleEvent: function (e) {
  1306. switch ( e.type ) {
  1307. case 'touchstart':
  1308. case 'pointerdown':
  1309. case 'MSPointerDown':
  1310. case 'mousedown':
  1311. this._start(e);
  1312. break;
  1313. case 'touchmove':
  1314. case 'pointermove':
  1315. case 'MSPointerMove':
  1316. case 'mousemove':
  1317. this._move(e);
  1318. break;
  1319. case 'touchend':
  1320. case 'pointerup':
  1321. case 'MSPointerUp':
  1322. case 'mouseup':
  1323. case 'touchcancel':
  1324. case 'pointercancel':
  1325. case 'MSPointerCancel':
  1326. case 'mousecancel':
  1327. this._end(e);
  1328. break;
  1329. case 'orientationchange':
  1330. case 'resize':
  1331. this._resize();
  1332. break;
  1333. case 'transitionend':
  1334. case 'webkitTransitionEnd':
  1335. case 'oTransitionEnd':
  1336. case 'MSTransitionEnd':
  1337. this._transitionEnd(e);
  1338. break;
  1339. case 'wheel':
  1340. case 'DOMMouseScroll':
  1341. case 'mousewheel':
  1342. this._wheel(e);
  1343. break;
  1344. case 'keydown':
  1345. this._key(e);
  1346. break;
  1347. case 'click':
  1348. if ( this.enabled && !e._constructed ) {
  1349. e.preventDefault();
  1350. e.stopPropagation();
  1351. }
  1352. break;
  1353. }
  1354. }
  1355. };
  1356. function createDefaultScrollbar (direction, interactive, type) {
  1357. var scrollbar = document.createElement('div'),
  1358. indicator = document.createElement('div');
  1359. if ( type === true ) {
  1360. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1361. indicator.style.cssText = '-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:absolute;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);border-radius:3px';
  1362. }
  1363. indicator.className = 'iScrollIndicator';
  1364. if ( direction == 'h' ) {
  1365. if ( type === true ) {
  1366. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1367. indicator.style.height = '100%';
  1368. }
  1369. scrollbar.className = 'iScrollHorizontalScrollbar';
  1370. } else {
  1371. if ( type === true ) {
  1372. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1373. indicator.style.width = '100%';
  1374. }
  1375. scrollbar.className = 'iScrollVerticalScrollbar';
  1376. }
  1377. scrollbar.style.cssText += ';overflow:hidden';
  1378. if ( !interactive ) {
  1379. scrollbar.style.pointerEvents = 'none';
  1380. }
  1381. scrollbar.appendChild(indicator);
  1382. return scrollbar;
  1383. }
  1384. function Indicator (scroller, options) {
  1385. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1386. this.wrapperStyle = this.wrapper.style;
  1387. this.indicator = this.wrapper.children[0];
  1388. this.indicatorStyle = this.indicator.style;
  1389. this.scroller = scroller;
  1390. this.options = {
  1391. listenX: true,
  1392. listenY: true,
  1393. interactive: false,
  1394. resize: true,
  1395. defaultScrollbars: false,
  1396. shrink: false,
  1397. fade: false,
  1398. speedRatioX: 0,
  1399. speedRatioY: 0
  1400. };
  1401. for ( var i in options ) {
  1402. this.options[i] = options[i];
  1403. }
  1404. this.sizeRatioX = 1;
  1405. this.sizeRatioY = 1;
  1406. this.maxPosX = 0;
  1407. this.maxPosY = 0;
  1408. if ( this.options.interactive ) {
  1409. if ( !this.options.disableTouch ) {
  1410. utils.addEvent(this.indicator, 'touchstart', this);
  1411. utils.addEvent(window, 'touchend', this);
  1412. }
  1413. if ( !this.options.disablePointer ) {
  1414. utils.addEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1415. utils.addEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1416. }
  1417. if ( !this.options.disableMouse ) {
  1418. utils.addEvent(this.indicator, 'mousedown', this);
  1419. utils.addEvent(window, 'mouseup', this);
  1420. }
  1421. }
  1422. if ( this.options.fade ) {
  1423. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1424. var durationProp = utils.style.transitionDuration;
  1425. if(!durationProp) {
  1426. return;
  1427. }
  1428. this.wrapperStyle[durationProp] = utils.isBadAndroid ? '0.0001ms' : '0ms';
  1429. // remove 0.0001ms
  1430. var self = this;
  1431. if(utils.isBadAndroid) {
  1432. rAF(function() {
  1433. if(self.wrapperStyle[durationProp] === '0.0001ms') {
  1434. self.wrapperStyle[durationProp] = '0s';
  1435. }
  1436. });
  1437. }
  1438. this.wrapperStyle.opacity = '0';
  1439. }
  1440. }
  1441. Indicator.prototype = {
  1442. handleEvent: function (e) {
  1443. switch ( e.type ) {
  1444. case 'touchstart':
  1445. case 'pointerdown':
  1446. case 'MSPointerDown':
  1447. case 'mousedown':
  1448. this._start(e);
  1449. break;
  1450. case 'touchmove':
  1451. case 'pointermove':
  1452. case 'MSPointerMove':
  1453. case 'mousemove':
  1454. this._move(e);
  1455. break;
  1456. case 'touchend':
  1457. case 'pointerup':
  1458. case 'MSPointerUp':
  1459. case 'mouseup':
  1460. case 'touchcancel':
  1461. case 'pointercancel':
  1462. case 'MSPointerCancel':
  1463. case 'mousecancel':
  1464. this._end(e);
  1465. break;
  1466. }
  1467. },
  1468. destroy: function () {
  1469. if ( this.options.fadeScrollbars ) {
  1470. clearTimeout(this.fadeTimeout);
  1471. this.fadeTimeout = null;
  1472. }
  1473. if ( this.options.interactive ) {
  1474. utils.removeEvent(this.indicator, 'touchstart', this);
  1475. utils.removeEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1476. utils.removeEvent(this.indicator, 'mousedown', this);
  1477. utils.removeEvent(window, 'touchmove', this);
  1478. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1479. utils.removeEvent(window, 'mousemove', this);
  1480. utils.removeEvent(window, 'touchend', this);
  1481. utils.removeEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1482. utils.removeEvent(window, 'mouseup', this);
  1483. }
  1484. if ( this.options.defaultScrollbars ) {
  1485. this.wrapper.parentNode.removeChild(this.wrapper);
  1486. }
  1487. },
  1488. _start: function (e) {
  1489. var point = e.touches ? e.touches[0] : e;
  1490. e.preventDefault();
  1491. e.stopPropagation();
  1492. this.transitionTime();
  1493. this.initiated = true;
  1494. this.moved = false;
  1495. this.lastPointX = point.pageX;
  1496. this.lastPointY = point.pageY;
  1497. this.startTime = utils.getTime();
  1498. if ( !this.options.disableTouch ) {
  1499. utils.addEvent(window, 'touchmove', this);
  1500. }
  1501. if ( !this.options.disablePointer ) {
  1502. utils.addEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1503. }
  1504. if ( !this.options.disableMouse ) {
  1505. utils.addEvent(window, 'mousemove', this);
  1506. }
  1507. this.scroller._execEvent('beforeScrollStart');
  1508. },
  1509. _move: function (e) {
  1510. var point = e.touches ? e.touches[0] : e,
  1511. deltaX, deltaY,
  1512. newX, newY,
  1513. timestamp = utils.getTime();
  1514. if ( !this.moved ) {
  1515. this.scroller._execEvent('scrollStart');
  1516. }
  1517. this.moved = true;
  1518. deltaX = point.pageX - this.lastPointX;
  1519. this.lastPointX = point.pageX;
  1520. deltaY = point.pageY - this.lastPointY;
  1521. this.lastPointY = point.pageY;
  1522. newX = this.x + deltaX;
  1523. newY = this.y + deltaY;
  1524. this._pos(newX, newY);
  1525. if ( this.scroller.options.probeType == 1 && timestamp - this.startTime > 300 ) {
  1526. this.startTime = timestamp;
  1527. this.scroller._execEvent('scroll');
  1528. } else if ( this.scroller.options.probeType > 1 ) {
  1529. this.scroller._execEvent('scroll');
  1530. }
  1531. // INSERT POINT: indicator._move
  1532. e.preventDefault();
  1533. e.stopPropagation();
  1534. },
  1535. _end: function (e) {
  1536. if ( !this.initiated ) {
  1537. return;
  1538. }
  1539. this.initiated = false;
  1540. e.preventDefault();
  1541. e.stopPropagation();
  1542. utils.removeEvent(window, 'touchmove', this);
  1543. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1544. utils.removeEvent(window, 'mousemove', this);
  1545. if ( this.scroller.options.snap ) {
  1546. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1547. var time = this.options.snapSpeed || Math.max(
  1548. Math.max(
  1549. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1550. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1551. ), 300);
  1552. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1553. this.scroller.directionX = 0;
  1554. this.scroller.directionY = 0;
  1555. this.scroller.currentPage = snap;
  1556. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1557. }
  1558. }
  1559. if ( this.moved ) {
  1560. this.scroller._execEvent('scrollEnd');
  1561. }
  1562. },
  1563. transitionTime: function (time) {
  1564. time = time || 0;
  1565. var durationProp = utils.style.transitionDuration;
  1566. if(!durationProp) {
  1567. return;
  1568. }
  1569. this.indicatorStyle[durationProp] = time + 'ms';
  1570. if ( !time && utils.isBadAndroid ) {
  1571. this.indicatorStyle[durationProp] = '0.0001ms';
  1572. // remove 0.0001ms
  1573. var self = this;
  1574. rAF(function() {
  1575. if(self.indicatorStyle[durationProp] === '0.0001ms') {
  1576. self.indicatorStyle[durationProp] = '0s';
  1577. }
  1578. });
  1579. }
  1580. },
  1581. transitionTimingFunction: function (easing) {
  1582. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  1583. },
  1584. refresh: function () {
  1585. this.transitionTime();
  1586. if ( this.options.listenX && !this.options.listenY ) {
  1587. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  1588. } else if ( this.options.listenY && !this.options.listenX ) {
  1589. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  1590. } else {
  1591. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  1592. }
  1593. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  1594. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  1595. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  1596. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1597. if ( this.options.listenX ) {
  1598. this.wrapper.style.right = '8px';
  1599. } else {
  1600. this.wrapper.style.bottom = '8px';
  1601. }
  1602. }
  1603. } else {
  1604. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  1605. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  1606. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1607. if ( this.options.listenX ) {
  1608. this.wrapper.style.right = '2px';
  1609. } else {
  1610. this.wrapper.style.bottom = '2px';
  1611. }
  1612. }
  1613. }
  1614. var r = this.wrapper.offsetHeight; // force refresh
  1615. if ( this.options.listenX ) {
  1616. this.wrapperWidth = this.wrapper.clientWidth;
  1617. if ( this.options.resize ) {
  1618. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  1619. this.indicatorStyle.width = this.indicatorWidth + 'px';
  1620. } else {
  1621. this.indicatorWidth = this.indicator.clientWidth;
  1622. }
  1623. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  1624. if ( this.options.shrink == 'clip' ) {
  1625. this.minBoundaryX = -this.indicatorWidth + 8;
  1626. this.maxBoundaryX = this.wrapperWidth - 8;
  1627. } else {
  1628. this.minBoundaryX = 0;
  1629. this.maxBoundaryX = this.maxPosX;
  1630. }
  1631. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  1632. }
  1633. if ( this.options.listenY ) {
  1634. this.wrapperHeight = this.wrapper.clientHeight;
  1635. if ( this.options.resize ) {
  1636. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  1637. this.indicatorStyle.height = this.indicatorHeight + 'px';
  1638. } else {
  1639. this.indicatorHeight = this.indicator.clientHeight;
  1640. }
  1641. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1642. if ( this.options.shrink == 'clip' ) {
  1643. this.minBoundaryY = -this.indicatorHeight + 8;
  1644. this.maxBoundaryY = this.wrapperHeight - 8;
  1645. } else {
  1646. this.minBoundaryY = 0;
  1647. this.maxBoundaryY = this.maxPosY;
  1648. }
  1649. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1650. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  1651. }
  1652. this.updatePosition();
  1653. },
  1654. updatePosition: function () {
  1655. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  1656. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  1657. if ( !this.options.ignoreBoundaries ) {
  1658. if ( x < this.minBoundaryX ) {
  1659. if ( this.options.shrink == 'scale' ) {
  1660. this.width = Math.max(this.indicatorWidth + x, 8);
  1661. this.indicatorStyle.width = this.width + 'px';
  1662. }
  1663. x = this.minBoundaryX;
  1664. } else if ( x > this.maxBoundaryX ) {
  1665. if ( this.options.shrink == 'scale' ) {
  1666. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  1667. this.indicatorStyle.width = this.width + 'px';
  1668. x = this.maxPosX + this.indicatorWidth - this.width;
  1669. } else {
  1670. x = this.maxBoundaryX;
  1671. }
  1672. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  1673. this.width = this.indicatorWidth;
  1674. this.indicatorStyle.width = this.width + 'px';
  1675. }
  1676. if ( y < this.minBoundaryY ) {
  1677. if ( this.options.shrink == 'scale' ) {
  1678. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  1679. this.indicatorStyle.height = this.height + 'px';
  1680. }
  1681. y = this.minBoundaryY;
  1682. } else if ( y > this.maxBoundaryY ) {
  1683. if ( this.options.shrink == 'scale' ) {
  1684. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  1685. this.indicatorStyle.height = this.height + 'px';
  1686. y = this.maxPosY + this.indicatorHeight - this.height;
  1687. } else {
  1688. y = this.maxBoundaryY;
  1689. }
  1690. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  1691. this.height = this.indicatorHeight;
  1692. this.indicatorStyle.height = this.height + 'px';
  1693. }
  1694. }
  1695. this.x = x;
  1696. this.y = y;
  1697. if ( this.scroller.options.useTransform ) {
  1698. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  1699. } else {
  1700. this.indicatorStyle.left = x + 'px';
  1701. this.indicatorStyle.top = y + 'px';
  1702. }
  1703. },
  1704. _pos: function (x, y) {
  1705. if ( x < 0 ) {
  1706. x = 0;
  1707. } else if ( x > this.maxPosX ) {
  1708. x = this.maxPosX;
  1709. }
  1710. if ( y < 0 ) {
  1711. y = 0;
  1712. } else if ( y > this.maxPosY ) {
  1713. y = this.maxPosY;
  1714. }
  1715. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  1716. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  1717. this.scroller.scrollTo(x, y);
  1718. },
  1719. fade: function (val, hold) {
  1720. if ( hold && !this.visible ) {
  1721. return;
  1722. }
  1723. clearTimeout(this.fadeTimeout);
  1724. this.fadeTimeout = null;
  1725. var time = val ? 250 : 500,
  1726. delay = val ? 0 : 300;
  1727. val = val ? '1' : '0';
  1728. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  1729. this.fadeTimeout = setTimeout((function (val) {
  1730. this.wrapperStyle.opacity = val;
  1731. this.visible = +val;
  1732. }).bind(this, val), delay);
  1733. }
  1734. };
  1735. IScroll.utils = utils;
  1736. if ( typeof module != 'undefined' && module.exports ) {
  1737. module.exports = IScroll;
  1738. } else if ( typeof define == 'function' && define.amd ) {
  1739. define( function () { return IScroll; } );
  1740. } else {
  1741. window.IScroll = IScroll;
  1742. }
  1743. })(window, document, Math);