Advertisement
Guest User

Untitled

a guest
May 2nd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 55.44 KB | None | 0 0
  1. // ==UserScript==
  2. // @name No zoom
  3. // @match https://example.com/*
  4. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
  5. // @run-at document-start
  6. // @grant none
  7. // @include http://*
  8. // @include https://*
  9.  
  10. // ==/UserScript==
  11. /* globals jQuery, $ */
  12.  
  13.  
  14. /*! iScroll v5.1.1 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */
  15. (function (window, document, Math) {
  16. var rAF = window.requestAnimationFrame ||
  17. window.webkitRequestAnimationFrame ||
  18. window.mozRequestAnimationFrame ||
  19. window.oRequestAnimationFrame ||
  20. window.msRequestAnimationFrame ||
  21. function (callback) { window.setTimeout(callback, 1000 / 60); };
  22.  
  23. var utils = (function () {
  24. var me = {};
  25.  
  26. var _elementStyle = document.createElement('div').style;
  27. var _vendor = (function () {
  28. var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'],
  29. transform,
  30. i = 0,
  31. l = vendors.length;
  32.  
  33. for ( ; i < l; i++ ) {
  34. transform = vendors[i] + 'ransform';
  35. if ( transform in _elementStyle ) return vendors[i].substr(0, vendors[i].length-1);
  36. }
  37.  
  38. return false;
  39. })();
  40.  
  41. function _prefixStyle (style) {
  42. if ( _vendor === false ) return false;
  43. if ( _vendor === '' ) return style;
  44. return _vendor + style.charAt(0).toUpperCase() + style.substr(1);
  45. }
  46.  
  47. me.getTime = Date.now || function getTime () { return new Date().getTime(); };
  48.  
  49. me.extend = function (target, obj) {
  50. for ( var i in obj ) {
  51. target[i] = obj[i];
  52. }
  53. };
  54.  
  55. me.addEvent = function (el, type, fn, capture) {
  56. el.addEventListener(type, fn, !!capture);
  57. };
  58.  
  59. me.removeEvent = function (el, type, fn, capture) {
  60. el.removeEventListener(type, fn, !!capture);
  61. };
  62.  
  63. me.momentum = function (current, start, time, lowerMargin, wrapperSize, deceleration) {
  64. var distance = current - start,
  65. speed = Math.abs(distance) / time,
  66. destination,
  67. duration;
  68.  
  69. if (speed > 1) {
  70. speed = 1;
  71. }
  72.  
  73. deceleration = deceleration === undefined ? 0.0006 : deceleration;
  74.  
  75. destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
  76. duration = speed / deceleration;
  77.  
  78. if ( destination < lowerMargin ) {
  79. destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
  80. distance = Math.abs(destination - current);
  81. duration = distance / speed;
  82. } else if ( destination > 0 ) {
  83. destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
  84. distance = Math.abs(current) + destination;
  85. duration = distance / speed;
  86. }
  87.  
  88. return {
  89. destination: Math.round(destination),
  90. duration: duration
  91. };
  92. };
  93.  
  94. var _transform = _prefixStyle('transform');
  95.  
  96. me.extend(me, {
  97. hasTransform: _transform !== false,
  98. hasPerspective: _prefixStyle('perspective') in _elementStyle,
  99. hasTouch: 'ontouchstart' in window,
  100. hasPointer: navigator.msPointerEnabled,
  101. hasTransition: _prefixStyle('transition') in _elementStyle
  102. });
  103.  
  104. // This should find all Android browsers lower than build 535.19 (both stock browser and webview)
  105. me.isBadAndroid = /Android /.test(window.navigator.appVersion) && !(/Chrome\/\d/.test(window.navigator.appVersion));
  106.  
  107. me.extend(me.style = {}, {
  108. transform: _transform,
  109. transitionTimingFunction: _prefixStyle('transitionTimingFunction'),
  110. transitionDuration: _prefixStyle('transitionDuration'),
  111. transitionDelay: _prefixStyle('transitionDelay'),
  112. transformOrigin: _prefixStyle('transformOrigin')
  113. });
  114.  
  115. me.hasClass = function (e, c) {
  116. var re = new RegExp("(^|\\s)" + c + "(\\s|$)");
  117. return re.test(e.className);
  118. };
  119.  
  120. me.addClass = function (e, c) {
  121. if ( me.hasClass(e, c) ) {
  122. return;
  123. }
  124.  
  125. var newclass = e.className.split(' ');
  126. newclass.push(c);
  127. e.className = newclass.join(' ');
  128. };
  129.  
  130. me.removeClass = function (e, c) {
  131. if ( !me.hasClass(e, c) ) {
  132. return;
  133. }
  134.  
  135. var re = new RegExp("(^|\\s)" + c + "(\\s|$)", 'g');
  136. e.className = e.className.replace(re, ' ');
  137. };
  138.  
  139. me.offset = function (el) {
  140. var left = -el.offsetLeft,
  141. top = -el.offsetTop;
  142.  
  143. // jshint -W084
  144. while (el = el.offsetParent) {
  145. left -= el.offsetLeft;
  146. top -= el.offsetTop;
  147. }
  148. // jshint +W084
  149.  
  150. return {
  151. left: left,
  152. top: top
  153. };
  154. };
  155.  
  156. me.preventDefaultException = function (el, exceptions) {
  157. for ( var i in exceptions ) {
  158. if ( exceptions[i].test(el[i]) ) {
  159. return true;
  160. }
  161. }
  162.  
  163. return false;
  164. };
  165.  
  166. me.extend(me.eventType = {}, {
  167. touchstart: 1,
  168. touchmove: 1,
  169. touchend: 1,
  170.  
  171. mousedown: 2,
  172. mousemove: 2,
  173. mouseup: 2,
  174.  
  175. MSPointerDown: 3,
  176. MSPointerMove: 3,
  177. MSPointerUp: 3
  178. });
  179.  
  180. me.extend(me.ease = {}, {
  181. quadratic: {
  182. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  183. fn: function (k) {
  184. return k * ( 2 - k );
  185. }
  186. },
  187. circular: {
  188. 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)
  189. fn: function (k) {
  190. return Math.sqrt( 1 - ( --k * k ) );
  191. }
  192. },
  193. back: {
  194. style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
  195. fn: function (k) {
  196. var b = 4;
  197. return ( k = k - 1 ) * k * ( ( b + 1 ) * k + b ) + 1;
  198. }
  199. },
  200. bounce: {
  201. style: '',
  202. fn: function (k) {
  203. if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
  204. return 7.5625 * k * k;
  205. } else if ( k < ( 2 / 2.75 ) ) {
  206. return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
  207. } else if ( k < ( 2.5 / 2.75 ) ) {
  208. return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
  209. } else {
  210. return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
  211. }
  212. }
  213. },
  214. elastic: {
  215. style: '',
  216. fn: function (k) {
  217. var f = 0.22,
  218. e = 0.4;
  219.  
  220. if ( k === 0 ) { return 0; }
  221. if ( k == 1 ) { return 1; }
  222.  
  223. return ( e * Math.pow( 2, - 10 * k ) * Math.sin( ( k - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 );
  224. }
  225. }
  226. });
  227.  
  228. me.tap = function (e, eventName) {
  229. var ev = document.createEvent('Event');
  230. ev.initEvent(eventName, true, true);
  231. ev.pageX = e.pageX;
  232. ev.pageY = e.pageY;
  233. e.target.dispatchEvent(ev);
  234. };
  235.  
  236. me.click = function (e) {
  237. var target = e.target,
  238. ev;
  239.  
  240. if ( !(/(SELECT|INPUT|TEXTAREA)/i).test(target.tagName) ) {
  241. ev = document.createEvent('MouseEvents');
  242. ev.initMouseEvent('click', true, true, e.view, 1,
  243. target.screenX, target.screenY, target.clientX, target.clientY,
  244. e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
  245. 0, null);
  246.  
  247. ev._constructed = true;
  248. target.dispatchEvent(ev);
  249. }
  250. };
  251.  
  252. return me;
  253. })();
  254.  
  255. function IScroll (el, options) {
  256. this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
  257. this.scroller = this.wrapper.children[0];
  258. this.scrollerStyle = this.scroller.style; // cache style for better performance
  259.  
  260. this.options = {
  261.  
  262. zoomMin: 1,
  263. zoomMax: 4, startZoom: 1,
  264.  
  265. resizeScrollbars: true,
  266.  
  267. mouseWheelSpeed: 20,
  268.  
  269. snapThreshold: 0.334,
  270.  
  271. // INSERT POINT: OPTIONS
  272.  
  273. startX: 0,
  274. startY: 0,
  275. scrollY: true,
  276. directionLockThreshold: 5,
  277. momentum: true,
  278.  
  279. bounce: true,
  280. bounceTime: 600,
  281. bounceEasing: '',
  282.  
  283. preventDefault: true,
  284. preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ },
  285.  
  286. HWCompositing: true,
  287. useTransition: false,
  288. useTransform: false
  289. };
  290.  
  291. for ( var i in options ) {
  292. this.options[i] = options[i];
  293. }
  294.  
  295. // Normalize options
  296. this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
  297.  
  298. this.options.useTransition = utils.hasTransition && this.options.useTransition;
  299. this.options.useTransform = utils.hasTransform && this.options.useTransform;
  300.  
  301. this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
  302. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  303.  
  304. // If you want eventPassthrough I have to lock one of the axes
  305. this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
  306. this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
  307.  
  308. // With eventPassthrough we also need lockDirection mechanism
  309. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  310. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  311.  
  312. this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
  313.  
  314. this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
  315.  
  316. if ( this.options.tap === true ) {
  317. this.options.tap = 'tap';
  318. }
  319.  
  320. if ( this.options.shrinkScrollbars == 'scale' ) {
  321. this.options.useTransition = false;
  322. }
  323.  
  324. this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
  325.  
  326. // INSERT POINT: NORMALIZATION
  327.  
  328. // Some defaults
  329. this.x = 0;
  330. this.y = 0;
  331. this.directionX = 0;
  332. this.directionY = 0;
  333. this._events = {};
  334.  
  335. this.scale = Math.min(Math.max(this.options.startZoom, this.options.zoomMin), this.options.zoomMax);
  336.  
  337. // INSERT POINT: DEFAULTS
  338.  
  339. this._init();
  340. this.refresh();
  341.  
  342. this.scrollTo(this.options.startX, this.options.startY);
  343. this.enable();
  344. }
  345.  
  346. IScroll.prototype = {
  347. version: '5.1.1',
  348.  
  349. _init: function () {
  350. this._initEvents();
  351.  
  352. if ( this.options.zoom ) {
  353. this._initZoom();
  354. }
  355.  
  356. if ( this.options.scrollbars || this.options.indicators ) {
  357. this._initIndicators();
  358. }
  359.  
  360. if ( this.options.mouseWheel ) {
  361. this._initWheel();
  362. }
  363.  
  364. if ( this.options.snap ) {
  365. this._initSnap();
  366. }
  367.  
  368. if ( this.options.keyBindings ) {
  369. this._initKeys();
  370. }
  371.  
  372. // INSERT POINT: _init
  373.  
  374. },
  375.  
  376. destroy: function () {
  377. this._initEvents(true);
  378.  
  379. this._execEvent('destroy');
  380. },
  381.  
  382. _transitionEnd: function (e) {
  383. if ( e.target != this.scroller || !this.isInTransition ) {
  384. return;
  385. }
  386.  
  387. this._transitionTime();
  388. if ( !this.resetPosition(this.options.bounceTime) ) {
  389. this.isInTransition = false;
  390. this._execEvent('scrollEnd');
  391. }
  392. },
  393.  
  394. _start: function (e) {
  395. // React to left mouse button only
  396. if ( utils.eventType[e.type] != 1 ) {
  397. if ( e.button !== 0 ) {
  398. return;
  399. }
  400. }
  401.  
  402. if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
  403. return;
  404. }
  405.  
  406. if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  407. e.preventDefault();
  408. }
  409.  
  410. var point = e.touches ? e.touches[0] : e,
  411. pos;
  412.  
  413. this.initiated = utils.eventType[e.type];
  414. this.moved = false;
  415. this.distX = 0;
  416. this.distY = 0;
  417. this.directionX = 0;
  418. this.directionY = 0;
  419. this.directionLocked = 0;
  420.  
  421. this._transitionTime();
  422.  
  423. this.startTime = utils.getTime();
  424.  
  425. if ( this.options.useTransition && this.isInTransition ) {
  426. this.isInTransition = false;
  427. pos = this.getComputedPosition();
  428. this._translate(Math.round(pos.x), Math.round(pos.y));
  429. this._execEvent('scrollEnd');
  430. } else if ( !this.options.useTransition && this.isAnimating ) {
  431. this.isAnimating = false;
  432. this._execEvent('scrollEnd');
  433. }
  434.  
  435. this.startX = this.x;
  436. this.startY = this.y;
  437. this.absStartX = this.x;
  438. this.absStartY = this.y;
  439. this.pointX = point.pageX;
  440. this.pointY = point.pageY;
  441.  
  442. this._execEvent('beforeScrollStart');
  443. },
  444.  
  445. _move: function (e) {
  446. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  447. return;
  448. }
  449.  
  450. if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
  451. e.preventDefault();
  452. }
  453.  
  454. var point = e.touches ? e.touches[0] : e,
  455. deltaX = point.pageX - this.pointX,
  456. deltaY = point.pageY - this.pointY,
  457. timestamp = utils.getTime(),
  458. newX, newY,
  459. absDistX, absDistY;
  460.  
  461. this.pointX = point.pageX;
  462. this.pointY = point.pageY;
  463.  
  464. this.distX += deltaX;
  465. this.distY += deltaY;
  466. absDistX = Math.abs(this.distX);
  467. absDistY = Math.abs(this.distY);
  468.  
  469. // We need to move at least 10 pixels for the scrolling to initiate
  470. if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
  471. return;
  472. }
  473.  
  474. // If you are scrolling in one direction lock the other
  475. if ( !this.directionLocked && !this.options.freeScroll ) {
  476. if ( absDistX > absDistY + this.options.directionLockThreshold ) {
  477. this.directionLocked = 'h'; // lock horizontally
  478. } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
  479. this.directionLocked = 'v'; // lock vertically
  480. } else {
  481. this.directionLocked = 'n'; // no lock
  482. }
  483. }
  484.  
  485. if ( this.directionLocked == 'h' ) {
  486. if ( this.options.eventPassthrough == 'vertical' ) {
  487. e.preventDefault();
  488. } else if ( this.options.eventPassthrough == 'horizontal' ) {
  489. this.initiated = false;
  490. return;
  491. }
  492.  
  493. deltaY = 0;
  494. } else if ( this.directionLocked == 'v' ) {
  495. if ( this.options.eventPassthrough == 'horizontal' ) {
  496. e.preventDefault();
  497. } else if ( this.options.eventPassthrough == 'vertical' ) {
  498. this.initiated = false;
  499. return;
  500. }
  501.  
  502. deltaX = 0;
  503. }
  504.  
  505. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  506. deltaY = this.hasVerticalScroll ? deltaY : 0;
  507.  
  508. newX = this.x + deltaX;
  509. newY = this.y + deltaY;
  510.  
  511. // Slow down if outside of the boundaries
  512. if ( newX > 0 || newX < this.maxScrollX ) {
  513. newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
  514. }
  515. if (newX == 0) {
  516. newX = this.x;
  517. }
  518. if ( newY > 0 || newY < this.maxScrollY ) {
  519. newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
  520. }
  521. if (newY == 0) {
  522. newY = this.y;
  523. }
  524.  
  525. this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
  526. this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
  527.  
  528. if ( !this.moved ) {
  529. this._execEvent('scrollStart');
  530. }
  531.  
  532. this.moved = true;
  533.  
  534. this._translate(newX, newY);
  535.  
  536. /* REPLACE START: _move */
  537.  
  538. if ( timestamp - this.startTime > 300 ) {
  539. this.startTime = timestamp;
  540. this.startX = this.x;
  541. this.startY = this.y;
  542. }
  543.  
  544. /* REPLACE END: _move */
  545.  
  546. },
  547.  
  548. _end: function (e) {
  549. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  550. return;
  551. }
  552.  
  553. if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  554. e.preventDefault();
  555. }
  556.  
  557. var point = e.changedTouches ? e.changedTouches[0] : e,
  558. momentumX,
  559. momentumY,
  560. duration = utils.getTime() - this.startTime,
  561. newX = Math.round(this.x),
  562. newY = Math.round(this.y),
  563. distanceX = Math.abs(newX - this.startX),
  564. distanceY = Math.abs(newY - this.startY),
  565. time = 0,
  566. easing = '';
  567.  
  568. this.isInTransition = 0;
  569. this.initiated = 0;
  570. this.endTime = utils.getTime();
  571.  
  572. // reset if we are outside of the boundaries
  573. if ( this.resetPosition(this.options.bounceTime) ) {
  574. return;
  575. }
  576.  
  577. this.scrollTo(newX, newY); // ensures that the last position is rounded
  578.  
  579. // we scrolled less than 10 pixels
  580. if ( !this.moved ) {
  581. if ( this.options.tap ) {
  582. utils.tap(e, this.options.tap);
  583. }
  584.  
  585. if ( this.options.click ) {
  586. utils.click(e);
  587. }
  588.  
  589. this._execEvent('scrollCancel');
  590. return;
  591. }
  592.  
  593. if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
  594. this._execEvent('flick');
  595. return;
  596. }
  597.  
  598. // start momentum animation if needed
  599. if ( this.options.momentum && duration < 300) {
  600. 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 };
  601. 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 };
  602. newX = momentumX.destination;
  603. newY = momentumY.destination;
  604. time = Math.max(momentumX.duration, momentumY.duration);
  605. this.isInTransition = 1;
  606. }
  607.  
  608.  
  609. if ( this.options.snap ) {
  610. var snap = this._nearestSnap(newX, newY);
  611. this.currentPage = snap;
  612. time = this.options.snapSpeed || Math.max(
  613. Math.max(
  614. Math.min(Math.abs(newX - snap.x), 1000),
  615. Math.min(Math.abs(newY - snap.y), 1000)
  616. ), 300);
  617. newX = snap.x;
  618. newY = snap.y;
  619.  
  620. this.directionX = 0;
  621. this.directionY = 0;
  622. easing = this.options.bounceEasing;
  623. }
  624.  
  625. // INSERT POINT: _end
  626.  
  627. if ( newX != this.x || newY != this.y ) {
  628. // change easing function when scroller goes out of the boundaries
  629. if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
  630. easing = utils.ease.quadratic;
  631. }
  632.  
  633. this.scrollTo(newX, newY, time, easing);
  634. return;
  635. }
  636.  
  637. this._execEvent('scrollEnd');
  638. },
  639.  
  640. _resize: function () {
  641. var that = this;
  642.  
  643. clearTimeout(this.resizeTimeout);
  644.  
  645. this.resizeTimeout = setTimeout(function () {
  646. that.refresh();
  647. }, this.options.resizePolling);
  648. },
  649.  
  650. resetPosition: function (time) {
  651. var x = this.x,
  652. y = this.y;
  653.  
  654. time = time || 0;
  655.  
  656. if ( !this.hasHorizontalScroll || this.x > 0 ) {
  657. x = 0;
  658. } else if ( this.x < this.maxScrollX ) {
  659. x = this.maxScrollX;
  660. }
  661.  
  662. if ( !this.hasVerticalScroll || this.y > 0 ) {
  663. y = 0;
  664. } else if ( this.y < this.maxScrollY ) {
  665. y = this.maxScrollY;
  666. }
  667.  
  668. if ( x == this.x && y == this.y ) {
  669. return false;
  670. }
  671.  
  672. this.scrollTo(x, y, time, this.options.bounceEasing);
  673.  
  674. return true;
  675. },
  676.  
  677. disable: function () {
  678. this.enabled = false;
  679. },
  680.  
  681. enable: function () {
  682. this.enabled = true;
  683. },
  684.  
  685. refresh: function (zoomEnd) {
  686. var rf = this.wrapper.offsetHeight; // Force reflow
  687.  
  688. this.wrapperWidth = this.wrapper.clientWidth;
  689. this.wrapperHeight = this.wrapper.clientHeight;
  690.  
  691. /* REPLACE START: refresh */
  692. this.scrollerWidth = Math.round(this.scroller.offsetWidth * this.scale);
  693. this.scrollerHeight = Math.round(this.scroller.offsetHeight * this.scale);
  694.  
  695. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  696. this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  697. /* REPLACE END: refresh */
  698.  
  699. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  700. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  701.  
  702. if ( !this.hasHorizontalScroll ) {
  703. this.maxScrollX = 0;
  704. this.scrollerWidth = this.wrapperWidth;
  705. }
  706.  
  707. if ( !this.hasVerticalScroll ) {
  708. this.maxScrollY = 0;
  709. this.scrollerHeight = this.wrapperHeight;
  710. }
  711.  
  712. this.endTime = 0;
  713. this.directionX = 0;
  714. this.directionY = 0;
  715.  
  716. this.wrapperOffset = utils.offset(this.wrapper);
  717.  
  718. this._execEvent('refresh');
  719.  
  720. this.resetPosition();
  721.  
  722. // INSERT POINT: _refresh
  723.  
  724. },
  725.  
  726. on: function (type, fn) {
  727. if ( !this._events[type] ) {
  728. this._events[type] = [];
  729. }
  730.  
  731. this._events[type].push(fn);
  732. },
  733.  
  734. off: function (type, fn) {
  735. if ( !this._events[type] ) {
  736. return;
  737. }
  738.  
  739. var index = this._events[type].indexOf(fn);
  740.  
  741. if ( index > -1 ) {
  742. this._events[type].splice(index, 1);
  743. }
  744. },
  745.  
  746. _execEvent: function (type) {
  747. if ( !this._events[type] ) {
  748. return;
  749. }
  750.  
  751. var i = 0,
  752. l = this._events[type].length;
  753.  
  754. if ( !l ) {
  755. return;
  756. }
  757.  
  758. for ( ; i < l; i++ ) {
  759. this._events[type][i].apply(this, [].slice.call(arguments, 1));
  760. }
  761. },
  762.  
  763. scrollBy: function (x, y, time, easing) {
  764. x = this.x + x;
  765. y = this.y + y;
  766. time = time || 0;
  767.  
  768. this.scrollTo(x, y, time, easing);
  769. },
  770.  
  771. scrollTo: function (x, y, time, easing) {
  772.  
  773. easing = easing || utils.ease.circular;
  774.  
  775. this.isInTransition = this.options.useTransition && time > 0;
  776.  
  777. if ( !time || (this.options.useTransition && easing.style) ) {
  778. if (x == 0 && this.wrapperWidth >= this.scrollerWidth) {
  779. x = ($('#wrapper').width() - ($('#scroller').width() * this.scale)) / 2;
  780. }
  781. if (y == 0 && this.wrapperHeight >= this.scrollerHeight) {
  782. y = ($('#wrapper').height() - ($('#scroller').height() * this.scale)) / 2;
  783. }
  784.  
  785. this._transitionTimingFunction(easing.style);
  786. this._transitionTime(time);
  787. this._translate(x, y);
  788. } else {
  789. this._animate(x, y, time, easing.fn);
  790. }
  791. },
  792.  
  793. scrollToElement: function (el, time, offsetX, offsetY, easing) {
  794. el = el.nodeType ? el : this.scroller.querySelector(el);
  795.  
  796. if ( !el ) {
  797. return;
  798. }
  799.  
  800. var pos = utils.offset(el);
  801.  
  802. pos.left -= this.wrapperOffset.left;
  803. pos.top -= this.wrapperOffset.top;
  804.  
  805. // if offsetX/Y are true we center the element to the screen
  806. if ( offsetX === true ) {
  807. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  808. }
  809. if ( offsetY === true ) {
  810. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  811. }
  812.  
  813. pos.left -= offsetX || 0;
  814. pos.top -= offsetY || 0;
  815.  
  816. pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  817. pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  818.  
  819. time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
  820.  
  821. this.scrollTo(pos.left, pos.top, time, easing);
  822. },
  823.  
  824. _transitionTime: function (time) {
  825. time = time || 0;
  826.  
  827. this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
  828.  
  829. if ( !time && utils.isBadAndroid ) {
  830. this.scrollerStyle[utils.style.transitionDuration] = '0.001s';
  831. }
  832.  
  833.  
  834. if ( this.indicators ) {
  835. for ( var i = this.indicators.length; i--; ) {
  836. this.indicators[i].transitionTime(time);
  837. }
  838. }
  839.  
  840.  
  841. // INSERT POINT: _transitionTime
  842.  
  843. },
  844.  
  845. _transitionTimingFunction: function (easing) {
  846. this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
  847.  
  848.  
  849. if ( this.indicators ) {
  850. for ( var i = this.indicators.length; i--; ) {
  851. this.indicators[i].transitionTimingFunction(easing);
  852. }
  853. }
  854.  
  855.  
  856. // INSERT POINT: _transitionTimingFunction
  857.  
  858. },
  859.  
  860. _translate: function (x, y) {
  861. if ( this.options.useTransform ) {
  862.  
  863. /* REPLACE START: _translate */ this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px) scale(' + this.scale + ') ' + this.translateZ;/* REPLACE END: _translate */
  864.  
  865. } else {
  866. x = Math.round(x);
  867. y = Math.round(y);
  868. this.scrollerStyle.left = x + 'px';
  869. this.scrollerStyle.top = y + 'px';
  870. }
  871.  
  872. this.x = x;
  873. this.y = y;
  874.  
  875.  
  876. if ( this.indicators ) {
  877. for ( var i = this.indicators.length; i--; ) {
  878. this.indicators[i].updatePosition();
  879. }
  880. }
  881.  
  882.  
  883. // INSERT POINT: _translate
  884.  
  885. },
  886.  
  887. _initEvents: function (remove) {
  888. var eventType = remove ? utils.removeEvent : utils.addEvent,
  889. target = this.options.bindToWrapper ? this.wrapper : window;
  890.  
  891. eventType(window, 'orientationchange', this);
  892. eventType(window, 'resize', this);
  893.  
  894. if ( this.options.click ) {
  895. eventType(this.wrapper, 'click', this, true);
  896. }
  897.  
  898. if ( !this.options.disableMouse ) {
  899. eventType(this.wrapper, 'mousedown', this);
  900. eventType(target, 'mousemove', this);
  901. eventType(target, 'mousecancel', this);
  902. eventType(target, 'mouseup', this);
  903. }
  904.  
  905. if ( utils.hasPointer && !this.options.disablePointer ) {
  906. eventType(this.wrapper, 'MSPointerDown', this);
  907. eventType(target, 'MSPointerMove', this);
  908. eventType(target, 'MSPointerCancel', this);
  909. eventType(target, 'MSPointerUp', this);
  910. }
  911.  
  912. if ( utils.hasTouch && !this.options.disableTouch ) {
  913. eventType(this.wrapper, 'touchstart', this);
  914. eventType(target, 'touchmove', this);
  915. eventType(target, 'touchcancel', this);
  916. eventType(target, 'touchend', this);
  917. }
  918.  
  919. eventType(this.scroller, 'transitionend', this);
  920. eventType(this.scroller, 'webkitTransitionEnd', this);
  921. eventType(this.scroller, 'oTransitionEnd', this);
  922. eventType(this.scroller, 'MSTransitionEnd', this);
  923. },
  924.  
  925. getComputedPosition: function () {
  926. var matrix = window.getComputedStyle(this.scroller, null),
  927. x, y;
  928.  
  929. if ( this.options.useTransform ) {
  930. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  931. x = +(matrix[12] || matrix[4]);
  932. y = +(matrix[13] || matrix[5]);
  933. } else {
  934. x = +matrix.left.replace(/[^-\d.]/g, '');
  935. y = +matrix.top.replace(/[^-\d.]/g, '');
  936. }
  937.  
  938. return { x: x, y: y };
  939. },
  940.  
  941. _initIndicators: function () {
  942. var interactive = this.options.interactiveScrollbars,
  943. customStyle = typeof this.options.scrollbars != 'string',
  944. indicators = [],
  945. indicator;
  946.  
  947. var that = this;
  948.  
  949. this.indicators = [];
  950.  
  951. if ( this.options.scrollbars ) {
  952. // Vertical scrollbar
  953. if ( this.options.scrollY ) {
  954. indicator = {
  955. el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
  956. interactive: interactive,
  957. defaultScrollbars: true,
  958. customStyle: customStyle,
  959. resize: this.options.resizeScrollbars,
  960. shrink: this.options.shrinkScrollbars,
  961. fade: this.options.fadeScrollbars,
  962. listenX: false
  963. };
  964.  
  965. this.wrapper.appendChild(indicator.el);
  966. indicators.push(indicator);
  967. }
  968.  
  969. // Horizontal scrollbar
  970. if ( this.options.scrollX ) {
  971. indicator = {
  972. el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
  973. interactive: interactive,
  974. defaultScrollbars: true,
  975. customStyle: customStyle,
  976. resize: this.options.resizeScrollbars,
  977. shrink: this.options.shrinkScrollbars,
  978. fade: this.options.fadeScrollbars,
  979. listenY: false
  980. };
  981.  
  982. this.wrapper.appendChild(indicator.el);
  983. indicators.push(indicator);
  984. }
  985. }
  986.  
  987. if ( this.options.indicators ) {
  988. // TODO: check concat compatibility
  989. indicators = indicators.concat(this.options.indicators);
  990. }
  991.  
  992. for ( var i = indicators.length; i--; ) {
  993. this.indicators.push( new Indicator(this, indicators[i]) );
  994. }
  995.  
  996. // TODO: check if we can use array.map (wide compatibility and performance issues)
  997. function _indicatorsMap (fn) {
  998. for ( var i = that.indicators.length; i--; ) {
  999. fn.call(that.indicators[i]);
  1000. }
  1001. }
  1002.  
  1003. if ( this.options.fadeScrollbars ) {
  1004. this.on('scrollEnd', function () {
  1005. _indicatorsMap(function () {
  1006. this.fade();
  1007. });
  1008. });
  1009.  
  1010. this.on('scrollCancel', function () {
  1011. _indicatorsMap(function () {
  1012. this.fade();
  1013. });
  1014. });
  1015.  
  1016. this.on('scrollStart', function () {
  1017. _indicatorsMap(function () {
  1018. this.fade(1);
  1019. });
  1020. });
  1021.  
  1022. this.on('beforeScrollStart', function () {
  1023. _indicatorsMap(function () {
  1024. this.fade(1, true);
  1025. });
  1026. });
  1027. }
  1028.  
  1029.  
  1030. this.on('refresh', function () {
  1031. _indicatorsMap(function () {
  1032. this.refresh();
  1033. });
  1034. });
  1035.  
  1036. this.on('destroy', function () {
  1037. _indicatorsMap(function () {
  1038. this.destroy();
  1039. });
  1040.  
  1041. delete this.indicators;
  1042. });
  1043. },
  1044.  
  1045. _initZoom: function () {
  1046. this.scrollerStyle[utils.style.transformOrigin] = '0 0';
  1047. },
  1048.  
  1049. _zoomStart: function (e) {
  1050. var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ),
  1051. c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY );
  1052.  
  1053. this.touchesDistanceStart = Math.sqrt(c1 * c1 + c2 * c2);
  1054. this.startScale = this.scale;
  1055.  
  1056. this.originX = Math.abs(e.touches[0].pageX + e.touches[1].pageX) / 2 + this.wrapperOffset.left - this.x;
  1057. this.originY = Math.abs(e.touches[0].pageY + e.touches[1].pageY) / 2 + this.wrapperOffset.top - this.y;
  1058.  
  1059. this._execEvent('zoomStart');
  1060. },
  1061.  
  1062. _zoom: function (e) {
  1063. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  1064. return;
  1065. }
  1066.  
  1067. if ( this.options.preventDefault ) {
  1068. e.preventDefault();
  1069. }
  1070.  
  1071. var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ),
  1072. c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ),
  1073. distance = Math.sqrt( c1 * c1 + c2 * c2 ),
  1074. scale = 1 / this.touchesDistanceStart * distance * this.startScale,
  1075. lastScale,
  1076. x, y;
  1077.  
  1078. this.scaled = true;
  1079.  
  1080. if ( scale < this.options.zoomMin ) {
  1081. scale = 0.5 * this.options.zoomMin * Math.pow(2.0, scale / this.options.zoomMin);
  1082. } else if ( scale > this.options.zoomMax ) {
  1083. scale = 2.0 * this.options.zoomMax * Math.pow(0.5, this.options.zoomMax / scale);
  1084. }
  1085.  
  1086. lastScale = scale / this.startScale;
  1087. x = this.originX - this.originX * lastScale + this.startX;
  1088. y = this.originY - this.originY * lastScale + this.startY;
  1089.  
  1090. this.scale = scale;
  1091.  
  1092. this.scrollTo(x, y, 0);
  1093. },
  1094.  
  1095. _zoomEnd: function (e) {
  1096. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  1097. return;
  1098. }
  1099.  
  1100. if ( this.options.preventDefault ) {
  1101. e.preventDefault();
  1102. }
  1103.  
  1104. var newX, newY,
  1105. lastScale;
  1106.  
  1107. this.isInTransition = 0;
  1108. this.initiated = 0;
  1109.  
  1110. if ( this.scale > this.options.zoomMax ) {
  1111. this.scale = this.options.zoomMax;
  1112. } else if ( this.scale < this.options.zoomMin ) {
  1113. this.scale = this.options.zoomMin;
  1114. }
  1115.  
  1116. // Update boundaries
  1117. this.refresh();
  1118.  
  1119. lastScale = this.scale / this.startScale;
  1120.  
  1121. newX = this.originX - this.originX * lastScale + this.startX;
  1122. newY = this.originY - this.originY * lastScale + this.startY;
  1123.  
  1124. if ( newX > 0 ) {
  1125. newX = 0;
  1126. } else if ( newX < this.maxScrollX ) {
  1127. newX = this.maxScrollX;
  1128. }
  1129.  
  1130. if ( newY > 0 ) {
  1131. newY = 0;
  1132. } else if ( newY < this.maxScrollY ) {
  1133. newY = this.maxScrollY;
  1134. }
  1135.  
  1136. if ( this.x != newX || this.y != newY ) {
  1137. this.scrollTo(newX, newY, this.options.bounceTime);
  1138. }
  1139.  
  1140. this.scaled = false;
  1141.  
  1142. this._execEvent('zoomEnd');
  1143. },
  1144.  
  1145. zoom: function (scale, x, y, time) {
  1146. if ( scale < this.options.zoomMin ) {
  1147. scale = this.options.zoomMin;
  1148. } else if ( scale > this.options.zoomMax ) {
  1149. scale = this.options.zoomMax;
  1150. }
  1151.  
  1152. if ( scale == this.scale ) {
  1153. return;
  1154. }
  1155.  
  1156. var relScale = scale / this.scale;
  1157.  
  1158. x = x === undefined ? this.wrapperWidth / 2 : x;
  1159. y = y === undefined ? this.wrapperHeight / 2 : y;
  1160. time = time === undefined ? 300 : time;
  1161.  
  1162. x = x + this.wrapperOffset.left - this.x;
  1163. y = y + this.wrapperOffset.top - this.y;
  1164.  
  1165. x = x - x * relScale + this.x;
  1166. y = y - y * relScale + this.y;
  1167.  
  1168. this.scale = scale;
  1169.  
  1170. this.refresh(); // update boundaries
  1171.  
  1172. if ( x > 0 ) {
  1173. x = 0;
  1174. } else if ( x < this.maxScrollX ) {
  1175. x = this.maxScrollX;
  1176. }
  1177.  
  1178. if ( y > 0 ) {
  1179. y = 0;
  1180. } else if ( y < this.maxScrollY ) {
  1181. y = this.maxScrollY;
  1182. }
  1183.  
  1184. this.scrollTo(x, y, time);
  1185. },
  1186.  
  1187. _wheelZoom: function (e) {
  1188. var wheelDeltaY,
  1189. deltaScale,
  1190. that = this;
  1191.  
  1192. // Execute the zoomEnd event after 400ms the wheel stopped scrolling
  1193. clearTimeout(this.wheelTimeout);
  1194. this.wheelTimeout = setTimeout(function () {
  1195. that._execEvent('zoomEnd');
  1196. }, 400);
  1197.  
  1198. if ( 'deltaX' in e ) {
  1199. wheelDeltaY = -e.deltaY / Math.abs(e.deltaY);
  1200. } else if ('wheelDeltaX' in e) {
  1201. wheelDeltaY = e.wheelDeltaY / Math.abs(e.wheelDeltaY);
  1202. } else if('wheelDelta' in e) {
  1203. wheelDeltaY = e.wheelDelta / Math.abs(e.wheelDelta);
  1204. } else if ('detail' in e) {
  1205. wheelDeltaY = -e.detail / Math.abs(e.wheelDelta);
  1206. } else {
  1207. return;
  1208. }
  1209.  
  1210. deltaScale = this.scale + wheelDeltaY / 5;
  1211.  
  1212. this.zoom(deltaScale, e.pageX, e.pageY, 0);
  1213. },
  1214.  
  1215. _initWheel: function () {
  1216. utils.addEvent(this.wrapper, 'wheel', this);
  1217. utils.addEvent(this.wrapper, 'mousewheel', this);
  1218. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  1219.  
  1220. this.on('destroy', function () {
  1221. utils.removeEvent(this.wrapper, 'wheel', this);
  1222. utils.removeEvent(this.wrapper, 'mousewheel', this);
  1223. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  1224. });
  1225. },
  1226.  
  1227. _wheel: function (e) {
  1228. if ( !this.enabled ) {
  1229. return;
  1230. }
  1231.  
  1232. e.preventDefault();
  1233. e.stopPropagation();
  1234.  
  1235. var wheelDeltaX, wheelDeltaY,
  1236. newX, newY,
  1237. that = this;
  1238.  
  1239. if ( this.wheelTimeout === undefined ) {
  1240. that._execEvent('scrollStart');
  1241. }
  1242.  
  1243. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  1244. clearTimeout(this.wheelTimeout);
  1245. this.wheelTimeout = setTimeout(function () {
  1246. that._execEvent('scrollEnd');
  1247. that.wheelTimeout = undefined;
  1248. }, 400);
  1249.  
  1250. if ( 'deltaX' in e ) {
  1251. wheelDeltaX = -e.deltaX;
  1252. wheelDeltaY = -e.deltaY;
  1253. } else if ( 'wheelDeltaX' in e ) {
  1254. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  1255. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  1256. } else if ( 'wheelDelta' in e ) {
  1257. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  1258. } else if ( 'detail' in e ) {
  1259. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  1260. } else {
  1261. return;
  1262. }
  1263.  
  1264. wheelDeltaX *= this.options.invertWheelDirection;
  1265. wheelDeltaY *= this.options.invertWheelDirection;
  1266.  
  1267. if ( !this.hasVerticalScroll ) {
  1268. wheelDeltaX = wheelDeltaY;
  1269. wheelDeltaY = 0;
  1270. }
  1271.  
  1272. if ( this.options.snap ) {
  1273. newX = this.currentPage.pageX;
  1274. newY = this.currentPage.pageY;
  1275.  
  1276. if ( wheelDeltaX > 0 ) {
  1277. newX--;
  1278. } else if ( wheelDeltaX < 0 ) {
  1279. newX++;
  1280. }
  1281.  
  1282. if ( wheelDeltaY > 0 ) {
  1283. newY--;
  1284. } else if ( wheelDeltaY < 0 ) {
  1285. newY++;
  1286. }
  1287.  
  1288. this.goToPage(newX, newY);
  1289.  
  1290. return;
  1291. }
  1292.  
  1293. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  1294. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  1295.  
  1296. if ( newX > 0 ) {
  1297. newX = 0;
  1298. } else if ( newX < this.maxScrollX ) {
  1299. newX = this.maxScrollX;
  1300. }
  1301.  
  1302. if ( newY > 0 ) {
  1303. newY = 0;
  1304. } else if ( newY < this.maxScrollY ) {
  1305. newY = this.maxScrollY;
  1306. }
  1307.  
  1308. this.scrollTo(newX, newY, 0);
  1309.  
  1310. // INSERT POINT: _wheel
  1311. },
  1312.  
  1313. _initSnap: function () {
  1314. this.currentPage = {};
  1315.  
  1316. if ( typeof this.options.snap == 'string' ) {
  1317. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  1318. }
  1319.  
  1320. this.on('refresh', function () {
  1321. var i = 0, l,
  1322. m = 0, n,
  1323. cx, cy,
  1324. x = 0, y,
  1325. stepX = this.options.snapStepX || this.wrapperWidth,
  1326. stepY = this.options.snapStepY || this.wrapperHeight,
  1327. el;
  1328.  
  1329. this.pages = [];
  1330.  
  1331. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  1332. return;
  1333. }
  1334.  
  1335. if ( this.options.snap === true ) {
  1336. cx = Math.round( stepX / 2 );
  1337. cy = Math.round( stepY / 2 );
  1338.  
  1339. while ( x > -this.scrollerWidth ) {
  1340. this.pages[i] = [];
  1341. l = 0;
  1342. y = 0;
  1343.  
  1344. while ( y > -this.scrollerHeight ) {
  1345. this.pages[i][l] = {
  1346. x: Math.max(x, this.maxScrollX),
  1347. y: Math.max(y, this.maxScrollY),
  1348. width: stepX,
  1349. height: stepY,
  1350. cx: x - cx,
  1351. cy: y - cy
  1352. };
  1353.  
  1354. y -= stepY;
  1355. l++;
  1356. }
  1357.  
  1358. x -= stepX;
  1359. i++;
  1360. }
  1361. } else {
  1362. el = this.options.snap;
  1363. l = el.length;
  1364. n = -1;
  1365.  
  1366. for ( ; i < l; i++ ) {
  1367. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  1368. m = 0;
  1369. n++;
  1370. }
  1371.  
  1372. if ( !this.pages[m] ) {
  1373. this.pages[m] = [];
  1374. }
  1375.  
  1376. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  1377. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  1378. cx = x - Math.round(el[i].offsetWidth / 2);
  1379. cy = y - Math.round(el[i].offsetHeight / 2);
  1380.  
  1381. this.pages[m][n] = {
  1382. x: x,
  1383. y: y,
  1384. width: el[i].offsetWidth,
  1385. height: el[i].offsetHeight,
  1386. cx: cx,
  1387. cy: cy
  1388. };
  1389.  
  1390. if ( x > this.maxScrollX ) {
  1391. m++;
  1392. }
  1393. }
  1394. }
  1395.  
  1396. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  1397.  
  1398. // Update snap threshold if needed
  1399. if ( this.options.snapThreshold % 1 === 0 ) {
  1400. this.snapThresholdX = this.options.snapThreshold;
  1401. this.snapThresholdY = this.options.snapThreshold;
  1402. } else {
  1403. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  1404. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  1405. }
  1406. });
  1407.  
  1408. this.on('flick', function () {
  1409. var time = this.options.snapSpeed || Math.max(
  1410. Math.max(
  1411. Math.min(Math.abs(this.x - this.startX), 1000),
  1412. Math.min(Math.abs(this.y - this.startY), 1000)
  1413. ), 300);
  1414.  
  1415. this.goToPage(
  1416. this.currentPage.pageX + this.directionX,
  1417. this.currentPage.pageY + this.directionY,
  1418. time
  1419. );
  1420. });
  1421. },
  1422.  
  1423. _nearestSnap: function (x, y) {
  1424. if ( !this.pages.length ) {
  1425. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1426. }
  1427.  
  1428. var i = 0,
  1429. l = this.pages.length,
  1430. m = 0;
  1431.  
  1432. // Check if we exceeded the snap threshold
  1433. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  1434. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  1435. return this.currentPage;
  1436. }
  1437.  
  1438. if ( x > 0 ) {
  1439. x = 0;
  1440. } else if ( x < this.maxScrollX ) {
  1441. x = this.maxScrollX;
  1442. }
  1443.  
  1444. if ( y > 0 ) {
  1445. y = 0;
  1446. } else if ( y < this.maxScrollY ) {
  1447. y = this.maxScrollY;
  1448. }
  1449.  
  1450. for ( ; i < l; i++ ) {
  1451. if ( x >= this.pages[i][0].cx ) {
  1452. x = this.pages[i][0].x;
  1453. break;
  1454. }
  1455. }
  1456.  
  1457. l = this.pages[i].length;
  1458.  
  1459. for ( ; m < l; m++ ) {
  1460. if ( y >= this.pages[0][m].cy ) {
  1461. y = this.pages[0][m].y;
  1462. break;
  1463. }
  1464. }
  1465.  
  1466. if ( i == this.currentPage.pageX ) {
  1467. i += this.directionX;
  1468.  
  1469. if ( i < 0 ) {
  1470. i = 0;
  1471. } else if ( i >= this.pages.length ) {
  1472. i = this.pages.length - 1;
  1473. }
  1474.  
  1475. x = this.pages[i][0].x;
  1476. }
  1477.  
  1478. if ( m == this.currentPage.pageY ) {
  1479. m += this.directionY;
  1480.  
  1481. if ( m < 0 ) {
  1482. m = 0;
  1483. } else if ( m >= this.pages[0].length ) {
  1484. m = this.pages[0].length - 1;
  1485. }
  1486.  
  1487. y = this.pages[0][m].y;
  1488. }
  1489.  
  1490. return {
  1491. x: x,
  1492. y: y,
  1493. pageX: i,
  1494. pageY: m
  1495. };
  1496. },
  1497.  
  1498. goToPage: function (x, y, time, easing) {
  1499. easing = easing || this.options.bounceEasing;
  1500.  
  1501. if ( x >= this.pages.length ) {
  1502. x = this.pages.length - 1;
  1503. } else if ( x < 0 ) {
  1504. x = 0;
  1505. }
  1506.  
  1507. if ( y >= this.pages[x].length ) {
  1508. y = this.pages[x].length - 1;
  1509. } else if ( y < 0 ) {
  1510. y = 0;
  1511. }
  1512.  
  1513. var posX = this.pages[x][y].x,
  1514. posY = this.pages[x][y].y;
  1515.  
  1516. time = time === undefined ? this.options.snapSpeed || Math.max(
  1517. Math.max(
  1518. Math.min(Math.abs(posX - this.x), 1000),
  1519. Math.min(Math.abs(posY - this.y), 1000)
  1520. ), 300) : time;
  1521.  
  1522. this.currentPage = {
  1523. x: posX,
  1524. y: posY,
  1525. pageX: x,
  1526. pageY: y
  1527. };
  1528.  
  1529. this.scrollTo(posX, posY, time, easing);
  1530. },
  1531.  
  1532. next: function (time, easing) {
  1533. var x = this.currentPage.pageX,
  1534. y = this.currentPage.pageY;
  1535.  
  1536. x++;
  1537.  
  1538. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  1539. x = 0;
  1540. y++;
  1541. }
  1542.  
  1543. this.goToPage(x, y, time, easing);
  1544. },
  1545.  
  1546. prev: function (time, easing) {
  1547. var x = this.currentPage.pageX,
  1548. y = this.currentPage.pageY;
  1549.  
  1550. x--;
  1551.  
  1552. if ( x < 0 && this.hasVerticalScroll ) {
  1553. x = 0;
  1554. y--;
  1555. }
  1556.  
  1557. this.goToPage(x, y, time, easing);
  1558. },
  1559.  
  1560. _initKeys: function (e) {
  1561. // default key bindings
  1562. var keys = {
  1563. pageUp: 33,
  1564. pageDown: 34,
  1565. end: 35,
  1566. home: 36,
  1567. left: 37,
  1568. up: 38,
  1569. right: 39,
  1570. down: 40
  1571. };
  1572. var i;
  1573.  
  1574. // if you give me characters I give you keycode
  1575. if ( typeof this.options.keyBindings == 'object' ) {
  1576. for ( i in this.options.keyBindings ) {
  1577. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1578. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1579. }
  1580. }
  1581. } else {
  1582. this.options.keyBindings = {};
  1583. }
  1584.  
  1585. for ( i in keys ) {
  1586. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1587. }
  1588.  
  1589. utils.addEvent(window, 'keydown', this);
  1590.  
  1591. this.on('destroy', function () {
  1592. utils.removeEvent(window, 'keydown', this);
  1593. });
  1594. },
  1595.  
  1596. _key: function (e) {
  1597. if ( !this.enabled ) {
  1598. return;
  1599. }
  1600.  
  1601. var snap = this.options.snap, // we are using this alot, better to cache it
  1602. newX = snap ? this.currentPage.pageX : this.x,
  1603. newY = snap ? this.currentPage.pageY : this.y,
  1604. now = utils.getTime(),
  1605. prevTime = this.keyTime || 0,
  1606. acceleration = 0.250,
  1607. pos;
  1608.  
  1609. if ( this.options.useTransition && this.isInTransition ) {
  1610. pos = this.getComputedPosition();
  1611.  
  1612. this._translate(Math.round(pos.x), Math.round(pos.y));
  1613. this.isInTransition = false;
  1614. }
  1615.  
  1616. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1617.  
  1618. switch ( e.keyCode ) {
  1619. case this.options.keyBindings.pageUp:
  1620. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1621. newX += snap ? 1 : this.wrapperWidth;
  1622. } else {
  1623. newY += snap ? 1 : this.wrapperHeight;
  1624. }
  1625. break;
  1626. case this.options.keyBindings.pageDown:
  1627. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1628. newX -= snap ? 1 : this.wrapperWidth;
  1629. } else {
  1630. newY -= snap ? 1 : this.wrapperHeight;
  1631. }
  1632. break;
  1633. case this.options.keyBindings.end:
  1634. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1635. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1636. break;
  1637. case this.options.keyBindings.home:
  1638. newX = 0;
  1639. newY = 0;
  1640. break;
  1641. case this.options.keyBindings.left:
  1642. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1643. break;
  1644. case this.options.keyBindings.up:
  1645. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1646. break;
  1647. case this.options.keyBindings.right:
  1648. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1649. break;
  1650. case this.options.keyBindings.down:
  1651. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1652. break;
  1653. default:
  1654. return;
  1655. }
  1656.  
  1657. if ( snap ) {
  1658. this.goToPage(newX, newY);
  1659. return;
  1660. }
  1661.  
  1662. if ( newX > 0 ) {
  1663. newX = 0;
  1664. this.keyAcceleration = 0;
  1665. } else if ( newX < this.maxScrollX ) {
  1666. newX = this.maxScrollX;
  1667. this.keyAcceleration = 0;
  1668. }
  1669.  
  1670. if ( newY > 0 ) {
  1671. newY = 0;
  1672. this.keyAcceleration = 0;
  1673. } else if ( newY < this.maxScrollY ) {
  1674. newY = this.maxScrollY;
  1675. this.keyAcceleration = 0;
  1676. }
  1677.  
  1678. this.scrollTo(newX, newY, 0);
  1679.  
  1680. this.keyTime = now;
  1681. },
  1682.  
  1683. _animate: function (destX, destY, duration, easingFn) {
  1684. var that = this,
  1685. startX = this.x,
  1686. startY = this.y,
  1687. startTime = utils.getTime(),
  1688. destTime = startTime + duration;
  1689.  
  1690. function step () {
  1691. var now = utils.getTime(),
  1692. newX, newY,
  1693. easing;
  1694.  
  1695. if ( now >= destTime ) {
  1696. that.isAnimating = false;
  1697. that._translate(destX, destY);
  1698.  
  1699. if ( !that.resetPosition(that.options.bounceTime) ) {
  1700. that._execEvent('scrollEnd');
  1701. }
  1702.  
  1703. return;
  1704. }
  1705.  
  1706. now = ( now - startTime ) / duration;
  1707. easing = easingFn(now);
  1708. newX = ( destX - startX ) * easing + startX;
  1709. newY = ( destY - startY ) * easing + startY;
  1710. that._translate(newX, newY);
  1711.  
  1712. if ( that.isAnimating ) {
  1713. rAF(step);
  1714. }
  1715. }
  1716.  
  1717. this.isAnimating = true;
  1718. step();
  1719. },
  1720. handleEvent: function (e) {
  1721. switch ( e.type ) {
  1722. case 'touchstart':
  1723. case 'MSPointerDown':
  1724. case 'mousedown':
  1725. this._start(e);
  1726.  
  1727. if ( this.options.zoom && e.touches && e.touches.length > 1 ) {
  1728. this._zoomStart(e);
  1729. }
  1730. break;
  1731. case 'touchmove':
  1732. case 'MSPointerMove':
  1733. case 'mousemove':
  1734. if ( this.options.zoom && e.touches && e.touches[1] ) {
  1735. this._zoom(e);
  1736. return;
  1737. }
  1738. this._move(e);
  1739. break;
  1740. case 'touchend':
  1741. case 'MSPointerUp':
  1742. case 'mouseup':
  1743. case 'touchcancel':
  1744. case 'MSPointerCancel':
  1745. case 'mousecancel':
  1746. if ( this.scaled ) {
  1747. this._zoomEnd(e);
  1748. return;
  1749. }
  1750. this._end(e);
  1751. break;
  1752. case 'orientationchange':
  1753. case 'resize':
  1754. this._resize();
  1755. break;
  1756. case 'transitionend':
  1757. case 'webkitTransitionEnd':
  1758. case 'oTransitionEnd':
  1759. case 'MSTransitionEnd':
  1760. this._transitionEnd(e);
  1761. break;
  1762. case 'wheel':
  1763. case 'DOMMouseScroll':
  1764. case 'mousewheel':
  1765. if ( this.options.wheelAction == 'zoom' ) {
  1766. this._wheelZoom(e);
  1767. return;
  1768. }
  1769. this._wheel(e);
  1770. break;
  1771. case 'keydown':
  1772. this._key(e);
  1773. break;
  1774. }
  1775. }
  1776.  
  1777. };
  1778. function createDefaultScrollbar (direction, interactive, type) {
  1779. var scrollbar = document.createElement('div'),
  1780. indicator = document.createElement('div');
  1781.  
  1782. if ( type === true ) {
  1783. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1784. 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';
  1785. }
  1786.  
  1787. indicator.className = 'iScrollIndicator';
  1788.  
  1789. if ( direction == 'h' ) {
  1790. if ( type === true ) {
  1791. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1792. indicator.style.height = '100%';
  1793. }
  1794. scrollbar.className = 'iScrollHorizontalScrollbar';
  1795. } else {
  1796. if ( type === true ) {
  1797. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1798. indicator.style.width = '100%';
  1799. }
  1800. scrollbar.className = 'iScrollVerticalScrollbar';
  1801. }
  1802.  
  1803. scrollbar.style.cssText += ';overflow:hidden';
  1804.  
  1805. if ( !interactive ) {
  1806. scrollbar.style.pointerEvents = 'none';
  1807. }
  1808.  
  1809. scrollbar.appendChild(indicator);
  1810.  
  1811. return scrollbar;
  1812. }
  1813.  
  1814. function Indicator (scroller, options) {
  1815. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1816. this.wrapperStyle = this.wrapper.style;
  1817. this.indicator = this.wrapper.children[0];
  1818. this.indicatorStyle = this.indicator.style;
  1819. this.scroller = scroller;
  1820.  
  1821. this.options = {
  1822. listenX: true,
  1823. listenY: true,
  1824. interactive: false,
  1825. resize: true,
  1826. defaultScrollbars: false,
  1827. shrink: false,
  1828. fade: false,
  1829. speedRatioX: 0,
  1830. speedRatioY: 0
  1831. };
  1832.  
  1833. for ( var i in options ) {
  1834. this.options[i] = options[i];
  1835. }
  1836.  
  1837. this.sizeRatioX = 1;
  1838. this.sizeRatioY = 1;
  1839. this.maxPosX = 0;
  1840. this.maxPosY = 0;
  1841.  
  1842. if ( this.options.interactive ) {
  1843. if ( !this.options.disableTouch ) {
  1844. utils.addEvent(this.indicator, 'touchstart', this);
  1845. utils.addEvent(window, 'touchend', this);
  1846. }
  1847. if ( !this.options.disablePointer ) {
  1848. utils.addEvent(this.indicator, 'MSPointerDown', this);
  1849. utils.addEvent(window, 'MSPointerUp', this);
  1850. }
  1851. if ( !this.options.disableMouse ) {
  1852. utils.addEvent(this.indicator, 'mousedown', this);
  1853. utils.addEvent(window, 'mouseup', this);
  1854. }
  1855. }
  1856.  
  1857. if ( this.options.fade ) {
  1858. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1859. this.wrapperStyle[utils.style.transitionDuration] = utils.isBadAndroid ? '0.001s' : '0ms';
  1860. this.wrapperStyle.opacity = '0';
  1861. }
  1862. }
  1863.  
  1864. Indicator.prototype = {
  1865. handleEvent: function (e) {
  1866. switch ( e.type ) {
  1867. case 'touchstart':
  1868. case 'MSPointerDown':
  1869. case 'mousedown':
  1870. this._start(e);
  1871. break;
  1872. case 'touchmove':
  1873. case 'MSPointerMove':
  1874. case 'mousemove':
  1875. this._move(e);
  1876. break;
  1877. case 'touchend':
  1878. case 'MSPointerUp':
  1879. case 'mouseup':
  1880. case 'touchcancel':
  1881. case 'MSPointerCancel':
  1882. case 'mousecancel':
  1883. this._end(e);
  1884. break;
  1885. }
  1886. },
  1887.  
  1888. destroy: function () {
  1889. if ( this.options.interactive ) {
  1890. utils.removeEvent(this.indicator, 'touchstart', this);
  1891. utils.removeEvent(this.indicator, 'MSPointerDown', this);
  1892. utils.removeEvent(this.indicator, 'mousedown', this);
  1893.  
  1894. utils.removeEvent(window, 'touchmove', this);
  1895. utils.removeEvent(window, 'MSPointerMove', this);
  1896. utils.removeEvent(window, 'mousemove', this);
  1897.  
  1898. utils.removeEvent(window, 'touchend', this);
  1899. utils.removeEvent(window, 'MSPointerUp', this);
  1900. utils.removeEvent(window, 'mouseup', this);
  1901. }
  1902.  
  1903. if ( this.options.defaultScrollbars ) {
  1904. this.wrapper.parentNode.removeChild(this.wrapper);
  1905. }
  1906. },
  1907.  
  1908. _start: function (e) {
  1909. var point = e.touches ? e.touches[0] : e;
  1910.  
  1911. e.preventDefault();
  1912. e.stopPropagation();
  1913.  
  1914. this.transitionTime();
  1915.  
  1916. this.initiated = true;
  1917. this.moved = false;
  1918. this.lastPointX = point.pageX;
  1919. this.lastPointY = point.pageY;
  1920.  
  1921. this.startTime = utils.getTime();
  1922.  
  1923. if ( !this.options.disableTouch ) {
  1924. utils.addEvent(window, 'touchmove', this);
  1925. }
  1926. if ( !this.options.disablePointer ) {
  1927. utils.addEvent(window, 'MSPointerMove', this);
  1928. }
  1929. if ( !this.options.disableMouse ) {
  1930. utils.addEvent(window, 'mousemove', this);
  1931. }
  1932.  
  1933. this.scroller._execEvent('beforeScrollStart');
  1934. },
  1935.  
  1936. _move: function (e) {
  1937. var point = e.touches ? e.touches[0] : e,
  1938. deltaX, deltaY,
  1939. newX, newY,
  1940. timestamp = utils.getTime();
  1941.  
  1942. if ( !this.moved ) {
  1943. this.scroller._execEvent('scrollStart');
  1944. }
  1945.  
  1946. this.moved = true;
  1947.  
  1948. deltaX = point.pageX - this.lastPointX;
  1949. this.lastPointX = point.pageX;
  1950.  
  1951. deltaY = point.pageY - this.lastPointY;
  1952. this.lastPointY = point.pageY;
  1953.  
  1954. newX = this.x + deltaX;
  1955. newY = this.y + deltaY;
  1956.  
  1957. this._pos(newX, newY);
  1958.  
  1959. // INSERT POINT: indicator._move
  1960.  
  1961. e.preventDefault();
  1962. e.stopPropagation();
  1963. },
  1964.  
  1965. _end: function (e) {
  1966. if ( !this.initiated ) {
  1967. return;
  1968. }
  1969.  
  1970. this.initiated = false;
  1971.  
  1972. e.preventDefault();
  1973. e.stopPropagation();
  1974.  
  1975. utils.removeEvent(window, 'touchmove', this);
  1976. utils.removeEvent(window, 'MSPointerMove', this);
  1977. utils.removeEvent(window, 'mousemove', this);
  1978.  
  1979. if ( this.scroller.options.snap ) {
  1980. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1981.  
  1982. var time = this.options.snapSpeed || Math.max(
  1983. Math.max(
  1984. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1985. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1986. ), 300);
  1987.  
  1988. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1989. this.scroller.directionX = 0;
  1990. this.scroller.directionY = 0;
  1991. this.scroller.currentPage = snap;
  1992. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1993. }
  1994. }
  1995.  
  1996. if ( this.moved ) {
  1997. this.scroller._execEvent('scrollEnd');
  1998. }
  1999. },
  2000.  
  2001. transitionTime: function (time) {
  2002. time = time || 0;
  2003. this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
  2004.  
  2005. if ( !time && utils.isBadAndroid ) {
  2006. this.indicatorStyle[utils.style.transitionDuration] = '0.001s';
  2007. }
  2008. },
  2009.  
  2010. transitionTimingFunction: function (easing) {
  2011. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  2012. },
  2013.  
  2014. refresh: function () {
  2015. this.transitionTime();
  2016.  
  2017. if ( this.options.listenX && !this.options.listenY ) {
  2018. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  2019. } else if ( this.options.listenY && !this.options.listenX ) {
  2020. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  2021. } else {
  2022. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  2023. }
  2024.  
  2025. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  2026. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  2027. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  2028.  
  2029. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  2030. if ( this.options.listenX ) {
  2031. this.wrapper.style.right = '8px';
  2032. } else {
  2033. this.wrapper.style.bottom = '8px';
  2034. }
  2035. }
  2036. } else {
  2037. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  2038. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  2039.  
  2040. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  2041. if ( this.options.listenX ) {
  2042. this.wrapper.style.right = '2px';
  2043. } else {
  2044. this.wrapper.style.bottom = '2px';
  2045. }
  2046. }
  2047. }
  2048.  
  2049. var r = this.wrapper.offsetHeight; // force refresh
  2050.  
  2051. if ( this.options.listenX ) {
  2052. this.wrapperWidth = this.wrapper.clientWidth;
  2053. if ( this.options.resize ) {
  2054. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  2055. this.indicatorStyle.width = this.indicatorWidth + 'px';
  2056. } else {
  2057. this.indicatorWidth = this.indicator.clientWidth;
  2058. }
  2059.  
  2060. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  2061.  
  2062. if ( this.options.shrink == 'clip' ) {
  2063. this.minBoundaryX = -this.indicatorWidth + 8;
  2064. this.maxBoundaryX = this.wrapperWidth - 8;
  2065. } else {
  2066. this.minBoundaryX = 0;
  2067. this.maxBoundaryX = this.maxPosX;
  2068. }
  2069.  
  2070. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  2071. }
  2072.  
  2073. if ( this.options.listenY ) {
  2074. this.wrapperHeight = this.wrapper.clientHeight;
  2075. if ( this.options.resize ) {
  2076. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  2077. this.indicatorStyle.height = this.indicatorHeight + 'px';
  2078. } else {
  2079. this.indicatorHeight = this.indicator.clientHeight;
  2080. }
  2081.  
  2082. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  2083.  
  2084. if ( this.options.shrink == 'clip' ) {
  2085. this.minBoundaryY = -this.indicatorHeight + 8;
  2086. this.maxBoundaryY = this.wrapperHeight - 8;
  2087. } else {
  2088. this.minBoundaryY = 0;
  2089. this.maxBoundaryY = this.maxPosY;
  2090. }
  2091.  
  2092. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  2093. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  2094. }
  2095.  
  2096. this.updatePosition();
  2097. },
  2098.  
  2099. updatePosition: function () {
  2100. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  2101. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  2102.  
  2103. if ( !this.options.ignoreBoundaries ) {
  2104. if ( x < this.minBoundaryX ) {
  2105. if ( this.options.shrink == 'scale' ) {
  2106. this.width = Math.max(this.indicatorWidth + x, 8);
  2107. this.indicatorStyle.width = this.width + 'px';
  2108. }
  2109. x = this.minBoundaryX;
  2110. } else if ( x > this.maxBoundaryX ) {
  2111. if ( this.options.shrink == 'scale' ) {
  2112. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  2113. this.indicatorStyle.width = this.width + 'px';
  2114. x = this.maxPosX + this.indicatorWidth - this.width;
  2115. } else {
  2116. x = this.maxBoundaryX;
  2117. }
  2118. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  2119. this.width = this.indicatorWidth;
  2120. this.indicatorStyle.width = this.width + 'px';
  2121. }
  2122.  
  2123. if ( y < this.minBoundaryY ) {
  2124. if ( this.options.shrink == 'scale' ) {
  2125. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  2126. this.indicatorStyle.height = this.height + 'px';
  2127. }
  2128. y = this.minBoundaryY;
  2129. } else if ( y > this.maxBoundaryY ) {
  2130. if ( this.options.shrink == 'scale' ) {
  2131. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  2132. this.indicatorStyle.height = this.height + 'px';
  2133. y = this.maxPosY + this.indicatorHeight - this.height;
  2134. } else {
  2135. y = this.maxBoundaryY;
  2136. }
  2137. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  2138. this.height = this.indicatorHeight;
  2139. this.indicatorStyle.height = this.height + 'px';
  2140. }
  2141. }
  2142.  
  2143. this.x = x;
  2144. this.y = y;
  2145.  
  2146. if ( this.scroller.options.useTransform ) {
  2147. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  2148. } else {
  2149. this.indicatorStyle.left = x + 'px';
  2150. this.indicatorStyle.top = y + 'px';
  2151. }
  2152. },
  2153.  
  2154. _pos: function (x, y) {
  2155. if ( x < 0 ) {
  2156. x = 0;
  2157. } else if ( x > this.maxPosX ) {
  2158. x = this.maxPosX;
  2159. }
  2160.  
  2161. if ( y < 0 ) {
  2162. y = 0;
  2163. } else if ( y > this.maxPosY ) {
  2164. y = this.maxPosY;
  2165. }
  2166.  
  2167. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  2168. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  2169.  
  2170. this.scroller.scrollTo(x, y);
  2171. },
  2172.  
  2173. fade: function (val, hold) {
  2174. if ( hold && !this.visible ) {
  2175. return;
  2176. }
  2177.  
  2178. clearTimeout(this.fadeTimeout);
  2179. this.fadeTimeout = null;
  2180.  
  2181. var time = val ? 250 : 500,
  2182. delay = val ? 0 : 300;
  2183.  
  2184. val = val ? '1' : '0';
  2185.  
  2186. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  2187.  
  2188. this.fadeTimeout = setTimeout((function (val) {
  2189. this.wrapperStyle.opacity = val;
  2190. this.visible = +val;
  2191. }).bind(this, val), delay);
  2192. }
  2193. };
  2194.  
  2195. IScroll.utils = utils;
  2196.  
  2197. if ( typeof module != 'undefined' && module.exports ) {
  2198. module.exports = IScroll;
  2199. } else {
  2200. window.IScroll = IScroll;
  2201. }
  2202.  
  2203. })(window, document, Math);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement