iscroll-zoom.js 53 KB

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