iscroll-probe.js 50 KB

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