iscroll-probe.js 50 KB

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