iscroll.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. // 50856845@qq.com
  229. topOffset : 0,
  230. formEleBlurOnMove: false,
  231. // TODO 尝试解决 华为P10等多款手机型号上下滑动的页面都会卡顿现象,比如管理员端的所有界面,以及代预约的页面问题
  232. disablePointer : true,
  233. disableTouch : false,
  234. disableMouse : false
  235. };
  236. for ( var i in options ) {
  237. this.options[i] = options[i];
  238. }
  239. // Normalize options
  240. this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
  241. this.options.useTransition = utils.hasTransition && this.options.useTransition;
  242. this.options.useTransform = utils.hasTransform && this.options.useTransform;
  243. this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
  244. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  245. // If you want eventPassthrough I have to lock one of the axes
  246. this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
  247. this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
  248. // With eventPassthrough we also need lockDirection mechanism
  249. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  250. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  251. this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
  252. this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
  253. if ( this.options.tap === true ) {
  254. this.options.tap = 'tap';
  255. }
  256. if ( this.options.shrinkScrollbars == 'scale' ) {
  257. this.options.useTransition = false;
  258. }
  259. this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
  260. if ( this.options.probeType == 3 ) {
  261. this.options.useTransition = false; }
  262. // INSERT POINT: NORMALIZATION
  263. // Some defaults
  264. this.x = 0;
  265. this.y = 0;
  266. this.directionX = 0;
  267. this.directionY = 0;
  268. this._events = {};
  269. // INSERT POINT: DEFAULTS
  270. this._init();
  271. this.refresh();
  272. this.scrollTo(this.options.startX, this.options.startY);
  273. this.enable();
  274. }
  275. IScroll.prototype = {
  276. version: '5.1.3',
  277. _init: function () {
  278. this._initEvents();
  279. if ( this.options.scrollbars || this.options.indicators ) {
  280. this._initIndicators();
  281. }
  282. if ( this.options.mouseWheel ) {
  283. this._initWheel();
  284. }
  285. if ( this.options.snap ) {
  286. this._initSnap();
  287. }
  288. if ( this.options.keyBindings ) {
  289. this._initKeys();
  290. }
  291. // INSERT POINT: _init
  292. },
  293. destroy: function () {
  294. this._initEvents(true);
  295. this._execEvent('destroy');
  296. },
  297. _transitionEnd: function (e) {
  298. if ( e.target != this.scroller || !this.isInTransition ) {
  299. return;
  300. }
  301. this._transitionTime();
  302. if ( !this.resetPosition(this.options.bounceTime) ) {
  303. this.isInTransition = false;
  304. this._execEvent('scrollEnd');
  305. }
  306. },
  307. _start: function (e) {
  308. // React to left mouse button only
  309. if ( utils.eventType[e.type] != 1 ) {
  310. if ( e.button !== 0 ) {
  311. return;
  312. }
  313. }
  314. if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
  315. return;
  316. }
  317. if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  318. e.preventDefault();
  319. }
  320. var point = e.touches ? e.touches[0] : e,
  321. pos;
  322. this.initiated = utils.eventType[e.type];
  323. this.moved = false;
  324. this.distX = 0;
  325. this.distY = 0;
  326. this.directionX = 0;
  327. this.directionY = 0;
  328. this.directionLocked = 0;
  329. this._transitionTime();
  330. this.startTime = utils.getTime();
  331. if ( this.options.useTransition && this.isInTransition ) {
  332. this.isInTransition = false;
  333. pos = this.getComputedPosition();
  334. this._translate(Math.round(pos.x), Math.round(pos.y));
  335. this._execEvent('scrollEnd');
  336. } else if ( !this.options.useTransition && this.isAnimating ) {
  337. this.isAnimating = false;
  338. this._execEvent('scrollEnd');
  339. }
  340. this.startX = this.x;
  341. this.startY = this.y;
  342. this.absStartX = this.x;
  343. this.absStartY = this.y;
  344. this.pointX = point.pageX;
  345. this.pointY = point.pageY;
  346. this._execEvent('beforeScrollStart');
  347. },
  348. _move: function (e) {
  349. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  350. return;
  351. }
  352. if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
  353. e.preventDefault();
  354. }
  355. var point = e.touches ? e.touches[0] : e,
  356. deltaX = point.pageX - this.pointX,
  357. deltaY = point.pageY - this.pointY,
  358. timestamp = utils.getTime(),
  359. newX, newY,
  360. absDistX, absDistY;
  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. if(this.options.formEleBlurOnMove) {
  430. $(this.wrapper).find('input,textarea').blur();
  431. }
  432. },
  433. _end: function (e) {
  434. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  435. return;
  436. }
  437. if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  438. e.preventDefault();
  439. }
  440. var point = e.changedTouches ? e.changedTouches[0] : e,
  441. momentumX,
  442. momentumY,
  443. duration = utils.getTime() - this.startTime,
  444. newX = Math.round(this.x),
  445. newY = Math.round(this.y),
  446. distanceX = Math.abs(newX - this.startX),
  447. distanceY = Math.abs(newY - this.startY),
  448. time = 0,
  449. easing = '';
  450. this.isInTransition = 0;
  451. this.initiated = 0;
  452. this.endTime = utils.getTime();
  453. // reset if we are outside of the boundaries
  454. if ( this.resetPosition(this.options.bounceTime) ) {
  455. return;
  456. }
  457. this.scrollTo(newX, newY); // ensures that the last position is rounded
  458. // we scrolled less than 10 pixels
  459. if ( !this.moved ) {
  460. if ( this.options.tap ) {
  461. utils.tap(e, this.options.tap);
  462. }
  463. if ( this.options.click ) {
  464. utils.click(e);
  465. }
  466. this._execEvent('scrollCancel');
  467. return;
  468. }
  469. if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
  470. this._execEvent('flick');
  471. return;
  472. }
  473. // start momentum animation if needed
  474. if ( this.options.momentum && duration < 300 ) {
  475. 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 };
  476. 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 };
  477. newX = momentumX.destination;
  478. newY = momentumY.destination;
  479. time = Math.max(momentumX.duration, momentumY.duration);
  480. this.isInTransition = 1;
  481. }
  482. if ( this.options.snap ) {
  483. var snap = this._nearestSnap(newX, newY);
  484. this.currentPage = snap;
  485. time = this.options.snapSpeed || Math.max(
  486. Math.max(
  487. Math.min(Math.abs(newX - snap.x), 1000),
  488. Math.min(Math.abs(newY - snap.y), 1000)
  489. ), 300);
  490. newX = snap.x;
  491. newY = snap.y;
  492. this.directionX = 0;
  493. this.directionY = 0;
  494. easing = this.options.bounceEasing;
  495. }
  496. // INSERT POINT: _end
  497. if ( newX != this.x || newY != this.y ) {
  498. // change easing function when scroller goes out of the boundaries
  499. if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
  500. easing = utils.ease.quadratic;
  501. }
  502. this.scrollTo(newX, newY, time, easing);
  503. return;
  504. }
  505. this._execEvent('scrollEnd');
  506. },
  507. _resize: function () {
  508. var that = this;
  509. clearTimeout(this.resizeTimeout);
  510. this.resizeTimeout = setTimeout(function () {
  511. that.refresh();
  512. }, this.options.resizePolling);
  513. },
  514. resetPosition: function (time) {
  515. var x = this.x,
  516. y = this.y;
  517. time = time || 0;
  518. if ( !this.hasHorizontalScroll || this.x > 0 ) {
  519. x = 0;
  520. } else if ( this.x < this.maxScrollX ) {
  521. x = this.maxScrollX;
  522. }
  523. if ( !this.hasVerticalScroll || this.y > 0 ) {
  524. y = 0;
  525. } else if ( this.y < this.maxScrollY ) {
  526. y = this.maxScrollY;
  527. }
  528. //50856485@qq.com
  529. if(this.y >= this.minScrollY || this.maxScrollY > 0){
  530. y = this.minScrollY;
  531. }else{
  532. if(this.y < this.maxScrollY){
  533. //若当前可滚动高度与可视高度一样,则说明到底了,要直接回弹到初始状态(否则会先回弹到0,再定位到初始位置)
  534. if(this.scrollerHeight==this.wrapperHeight){
  535. y = this.minScrollY;
  536. }else{//若不一样高,则按最大滚动值
  537. y = this.maxScrollY;
  538. }
  539. }else{
  540. y = this.y;
  541. }
  542. }
  543. //end add
  544. if ( x == this.x && y == this.y ) {
  545. return false;
  546. }
  547. this.scrollTo(x, y, time, this.options.bounceEasing);
  548. return true;
  549. },
  550. disable: function () {
  551. this.enabled = false;
  552. },
  553. enable: function () {
  554. this.enabled = true;
  555. },
  556. refresh: function () {
  557. var rf = this.wrapper.offsetHeight; // Force reflow
  558. this.wrapperWidth = this.wrapper.clientWidth;
  559. this.wrapperHeight = this.wrapper.clientHeight;
  560. // 50856845@qq.com
  561. this.minScrollY = -this.options.topOffset || 0;
  562. /* REPLACE START: refresh */
  563. this.scrollerWidth = this.scroller.offsetWidth;
  564. //this.scrollerHeight = this.scroller.offsetHeight;
  565. // 50856845@qq.com
  566. this.scrollerHeight = this.scroller.offsetHeight+ this.minScrollY;
  567. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  568. //this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  569. // 50856845@qq.com
  570. this.maxScrollY = this.wrapperHeight - this.scrollerHeight+ this.minScrollY;
  571. /* REPLACE END: refresh */
  572. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  573. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  574. if ( !this.hasHorizontalScroll ) {
  575. this.maxScrollX = 0;
  576. this.scrollerWidth = this.wrapperWidth;
  577. }
  578. if ( !this.hasVerticalScroll ) {
  579. this.maxScrollY = 0;
  580. this.scrollerHeight = this.wrapperHeight;
  581. }
  582. this.endTime = 0;
  583. this.directionX = 0;
  584. this.directionY = 0;
  585. this.wrapperOffset = utils.offset(this.wrapper);
  586. this._execEvent('refresh');
  587. this.resetPosition();
  588. // INSERT POINT: _refresh
  589. },
  590. on: function (type, fn) {
  591. if ( !this._events[type] ) {
  592. this._events[type] = [];
  593. }
  594. this._events[type].push(fn);
  595. },
  596. off: function (type, fn) {
  597. if ( !this._events[type] ) {
  598. return;
  599. }
  600. var index = this._events[type].indexOf(fn);
  601. if ( index > -1 ) {
  602. this._events[type].splice(index, 1);
  603. }
  604. },
  605. _execEvent: function (type) {
  606. if ( !this._events[type] ) {
  607. return;
  608. }
  609. var i = 0,
  610. l = this._events[type].length;
  611. if ( !l ) {
  612. return;
  613. }
  614. for ( ; i < l; i++ ) {
  615. this._events[type][i].apply(this, [].slice.call(arguments, 1));
  616. }
  617. },
  618. scrollBy: function (x, y, time, easing) {
  619. x = this.x + x;
  620. y = this.y + y;
  621. time = time || 0;
  622. this.scrollTo(x, y, time, easing);
  623. },
  624. scrollTo: function (x, y, time, easing) {
  625. easing = easing || utils.ease.circular;
  626. this.isInTransition = this.options.useTransition && time > 0;
  627. if ( !time || (this.options.useTransition && easing.style) ) {
  628. this._transitionTimingFunction(easing.style);
  629. this._transitionTime(time);
  630. this._translate(x, y);
  631. } else {
  632. this._animate(x, y, time, easing.fn);
  633. }
  634. },
  635. scrollToElement: function (el, time, offsetX, offsetY, easing) {
  636. el = el.nodeType ? el : this.scroller.querySelector(el);
  637. if ( !el ) {
  638. return;
  639. }
  640. var pos = utils.offset(el);
  641. pos.left -= this.wrapperOffset.left;
  642. pos.top -= this.wrapperOffset.top;
  643. // if offsetX/Y are true we center the element to the screen
  644. if ( offsetX === true ) {
  645. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  646. }
  647. if ( offsetY === true ) {
  648. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  649. }
  650. pos.left -= offsetX || 0;
  651. pos.top -= offsetY || 0;
  652. pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  653. pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  654. time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
  655. this.scrollTo(pos.left, pos.top, time, easing);
  656. },
  657. _transitionTime: function (time) {
  658. time = time || 0;
  659. this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
  660. if ( !time && utils.isBadAndroid ) {
  661. this.scrollerStyle[utils.style.transitionDuration] = '0.001s';
  662. }
  663. if ( this.indicators ) {
  664. for ( var i = this.indicators.length; i--; ) {
  665. this.indicators[i].transitionTime(time);
  666. }
  667. }
  668. // INSERT POINT: _transitionTime
  669. },
  670. _transitionTimingFunction: function (easing) {
  671. this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
  672. if ( this.indicators ) {
  673. for ( var i = this.indicators.length; i--; ) {
  674. this.indicators[i].transitionTimingFunction(easing);
  675. }
  676. }
  677. // INSERT POINT: _transitionTimingFunction
  678. },
  679. _translate: function (x, y) {
  680. if ( this.options.useTransform ) {
  681. /* REPLACE START: _translate */
  682. this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
  683. /* REPLACE END: _translate */
  684. } else {
  685. x = Math.round(x);
  686. y = Math.round(y);
  687. this.scrollerStyle.left = x + 'px';
  688. this.scrollerStyle.top = y + 'px';
  689. }
  690. this.x = x;
  691. this.y = y;
  692. if ( this.indicators ) {
  693. for ( var i = this.indicators.length; i--; ) {
  694. this.indicators[i].updatePosition();
  695. }
  696. }
  697. // INSERT POINT: _translate
  698. },
  699. _initEvents: function (remove) {
  700. var eventType = remove ? utils.removeEvent : utils.addEvent,
  701. target = this.options.bindToWrapper ? this.wrapper : window;
  702. eventType(window, 'orientationchange', this);
  703. eventType(window, 'resize', this);
  704. if ( this.options.click ) {
  705. eventType(this.wrapper, 'click', this, true);
  706. }
  707. if ( !this.options.disableMouse ) {
  708. eventType(this.wrapper, 'mousedown', this);
  709. eventType(target, 'mousemove', this);
  710. eventType(target, 'mousecancel', this);
  711. eventType(target, 'mouseup', this);
  712. }
  713. if ( utils.hasPointer && !this.options.disablePointer ) {
  714. eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
  715. eventType(target, utils.prefixPointerEvent('pointermove'), this);
  716. eventType(target, utils.prefixPointerEvent('pointercancel'), this);
  717. eventType(target, utils.prefixPointerEvent('pointerup'), this);
  718. }
  719. if ( utils.hasTouch && !this.options.disableTouch ) {
  720. eventType(this.wrapper, 'touchstart', this);
  721. eventType(target, 'touchmove', this);
  722. eventType(target, 'touchcancel', this);
  723. eventType(target, 'touchend', this);
  724. }
  725. eventType(this.scroller, 'transitionend', this);
  726. eventType(this.scroller, 'webkitTransitionEnd', this);
  727. eventType(this.scroller, 'oTransitionEnd', this);
  728. eventType(this.scroller, 'MSTransitionEnd', this);
  729. },
  730. getComputedPosition: function () {
  731. var matrix = window.getComputedStyle(this.scroller, null),
  732. x, y;
  733. if ( this.options.useTransform ) {
  734. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  735. x = +(matrix[12] || matrix[4]);
  736. y = +(matrix[13] || matrix[5]);
  737. } else {
  738. x = +matrix.left.replace(/[^-\d.]/g, '');
  739. y = +matrix.top.replace(/[^-\d.]/g, '');
  740. }
  741. return { x: x, y: y };
  742. },
  743. _initIndicators: function () {
  744. var interactive = this.options.interactiveScrollbars,
  745. customStyle = typeof this.options.scrollbars != 'string',
  746. indicators = [],
  747. indicator;
  748. var that = this;
  749. this.indicators = [];
  750. if ( this.options.scrollbars ) {
  751. // Vertical scrollbar
  752. if ( this.options.scrollY ) {
  753. indicator = {
  754. el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
  755. interactive: interactive,
  756. defaultScrollbars: true,
  757. customStyle: customStyle,
  758. resize: this.options.resizeScrollbars,
  759. shrink: this.options.shrinkScrollbars,
  760. fade: this.options.fadeScrollbars,
  761. listenX: false
  762. };
  763. this.wrapper.appendChild(indicator.el);
  764. indicators.push(indicator);
  765. }
  766. // Horizontal scrollbar
  767. if ( this.options.scrollX ) {
  768. indicator = {
  769. el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
  770. interactive: interactive,
  771. defaultScrollbars: true,
  772. customStyle: customStyle,
  773. resize: this.options.resizeScrollbars,
  774. shrink: this.options.shrinkScrollbars,
  775. fade: this.options.fadeScrollbars,
  776. listenY: false
  777. };
  778. this.wrapper.appendChild(indicator.el);
  779. indicators.push(indicator);
  780. }
  781. }
  782. if ( this.options.indicators ) {
  783. // TODO: check concat compatibility
  784. indicators = indicators.concat(this.options.indicators);
  785. }
  786. for ( var i = indicators.length; i--; ) {
  787. this.indicators.push( new Indicator(this, indicators[i]) );
  788. }
  789. // TODO: check if we can use array.map (wide compatibility and performance issues)
  790. function _indicatorsMap (fn) {
  791. for ( var i = that.indicators.length; i--; ) {
  792. fn.call(that.indicators[i]);
  793. }
  794. }
  795. if ( this.options.fadeScrollbars ) {
  796. this.on('scrollEnd', function () {
  797. _indicatorsMap(function () {
  798. this.fade();
  799. });
  800. });
  801. this.on('scrollCancel', function () {
  802. _indicatorsMap(function () {
  803. this.fade();
  804. });
  805. });
  806. this.on('scrollStart', function () {
  807. _indicatorsMap(function () {
  808. this.fade(1);
  809. });
  810. });
  811. this.on('beforeScrollStart', function () {
  812. _indicatorsMap(function () {
  813. this.fade(1, true);
  814. });
  815. });
  816. }
  817. this.on('refresh', function () {
  818. _indicatorsMap(function () {
  819. this.refresh();
  820. });
  821. });
  822. this.on('destroy', function () {
  823. _indicatorsMap(function () {
  824. this.destroy();
  825. });
  826. delete this.indicators;
  827. });
  828. },
  829. _initWheel: function () {
  830. utils.addEvent(this.wrapper, 'wheel', this);
  831. utils.addEvent(this.wrapper, 'mousewheel', this);
  832. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  833. this.on('destroy', function () {
  834. utils.removeEvent(this.wrapper, 'wheel', this);
  835. utils.removeEvent(this.wrapper, 'mousewheel', this);
  836. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  837. });
  838. },
  839. _wheel: function (e) {
  840. if ( !this.enabled ) {
  841. return;
  842. }
  843. e.preventDefault();
  844. e.stopPropagation();
  845. var wheelDeltaX, wheelDeltaY,
  846. newX, newY,
  847. that = this;
  848. if ( this.wheelTimeout === undefined ) {
  849. that._execEvent('scrollStart');
  850. }
  851. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  852. clearTimeout(this.wheelTimeout);
  853. this.wheelTimeout = setTimeout(function () {
  854. that._execEvent('scrollEnd');
  855. that.wheelTimeout = undefined;
  856. }, 400);
  857. if ( 'deltaX' in e ) {
  858. if (e.deltaMode === 1) {
  859. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  860. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  861. } else {
  862. wheelDeltaX = -e.deltaX;
  863. wheelDeltaY = -e.deltaY;
  864. }
  865. } else if ( 'wheelDeltaX' in e ) {
  866. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  867. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  868. } else if ( 'wheelDelta' in e ) {
  869. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  870. } else if ( 'detail' in e ) {
  871. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  872. } else {
  873. return;
  874. }
  875. wheelDeltaX *= this.options.invertWheelDirection;
  876. wheelDeltaY *= this.options.invertWheelDirection;
  877. if ( !this.hasVerticalScroll ) {
  878. wheelDeltaX = wheelDeltaY;
  879. wheelDeltaY = 0;
  880. }
  881. if ( this.options.snap ) {
  882. newX = this.currentPage.pageX;
  883. newY = this.currentPage.pageY;
  884. if ( wheelDeltaX > 0 ) {
  885. newX--;
  886. } else if ( wheelDeltaX < 0 ) {
  887. newX++;
  888. }
  889. if ( wheelDeltaY > 0 ) {
  890. newY--;
  891. } else if ( wheelDeltaY < 0 ) {
  892. newY++;
  893. }
  894. this.goToPage(newX, newY);
  895. return;
  896. }
  897. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  898. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  899. if ( newX > 0 ) {
  900. newX = 0;
  901. } else if ( newX < this.maxScrollX ) {
  902. newX = this.maxScrollX;
  903. }
  904. if ( newY > 0 ) {
  905. newY = 0;
  906. } else if ( newY < this.maxScrollY ) {
  907. newY = this.maxScrollY;
  908. }
  909. this.scrollTo(newX, newY, 0);
  910. if ( this.options.probeType > 1 ) {
  911. this._execEvent('scroll');
  912. }
  913. // INSERT POINT: _wheel
  914. },
  915. _initSnap: function () {
  916. this.currentPage = {};
  917. if ( typeof this.options.snap == 'string' ) {
  918. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  919. }
  920. this.on('refresh', function () {
  921. var i = 0, l,
  922. m = 0, n,
  923. cx, cy,
  924. x = 0, y,
  925. stepX = this.options.snapStepX || this.wrapperWidth,
  926. stepY = this.options.snapStepY || this.wrapperHeight,
  927. el;
  928. this.pages = [];
  929. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  930. return;
  931. }
  932. if ( this.options.snap === true ) {
  933. cx = Math.round( stepX / 2 );
  934. cy = Math.round( stepY / 2 );
  935. while ( x > -this.scrollerWidth ) {
  936. this.pages[i] = [];
  937. l = 0;
  938. y = 0;
  939. while ( y > -this.scrollerHeight ) {
  940. this.pages[i][l] = {
  941. x: Math.max(x, this.maxScrollX),
  942. y: Math.max(y, this.maxScrollY),
  943. width: stepX,
  944. height: stepY,
  945. cx: x - cx,
  946. cy: y - cy
  947. };
  948. y -= stepY;
  949. l++;
  950. }
  951. x -= stepX;
  952. i++;
  953. }
  954. } else {
  955. el = this.options.snap;
  956. l = el.length;
  957. n = -1;
  958. for ( ; i < l; i++ ) {
  959. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  960. m = 0;
  961. n++;
  962. }
  963. if ( !this.pages[m] ) {
  964. this.pages[m] = [];
  965. }
  966. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  967. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  968. cx = x - Math.round(el[i].offsetWidth / 2);
  969. cy = y - Math.round(el[i].offsetHeight / 2);
  970. this.pages[m][n] = {
  971. x: x,
  972. y: y,
  973. width: el[i].offsetWidth,
  974. height: el[i].offsetHeight,
  975. cx: cx,
  976. cy: cy
  977. };
  978. if ( x > this.maxScrollX ) {
  979. m++;
  980. }
  981. }
  982. }
  983. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  984. // Update snap threshold if needed
  985. if ( this.options.snapThreshold % 1 === 0 ) {
  986. this.snapThresholdX = this.options.snapThreshold;
  987. this.snapThresholdY = this.options.snapThreshold;
  988. } else {
  989. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  990. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  991. }
  992. });
  993. this.on('flick', function () {
  994. var time = this.options.snapSpeed || Math.max(
  995. Math.max(
  996. Math.min(Math.abs(this.x - this.startX), 1000),
  997. Math.min(Math.abs(this.y - this.startY), 1000)
  998. ), 300);
  999. this.goToPage(
  1000. this.currentPage.pageX + this.directionX,
  1001. this.currentPage.pageY + this.directionY,
  1002. time
  1003. );
  1004. });
  1005. },
  1006. _nearestSnap: function (x, y) {
  1007. if ( !this.pages.length ) {
  1008. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1009. }
  1010. var i = 0,
  1011. l = this.pages.length,
  1012. m = 0;
  1013. // Check if we exceeded the snap threshold
  1014. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  1015. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  1016. return this.currentPage;
  1017. }
  1018. if ( x > 0 ) {
  1019. x = 0;
  1020. } else if ( x < this.maxScrollX ) {
  1021. x = this.maxScrollX;
  1022. }
  1023. if ( y > 0 ) {
  1024. y = 0;
  1025. } else if ( y < this.maxScrollY ) {
  1026. y = this.maxScrollY;
  1027. }
  1028. for ( ; i < l; i++ ) {
  1029. if ( x >= this.pages[i][0].cx ) {
  1030. x = this.pages[i][0].x;
  1031. break;
  1032. }
  1033. }
  1034. l = this.pages[i].length;
  1035. for ( ; m < l; m++ ) {
  1036. if ( y >= this.pages[0][m].cy ) {
  1037. y = this.pages[0][m].y;
  1038. break;
  1039. }
  1040. }
  1041. if ( i == this.currentPage.pageX ) {
  1042. i += this.directionX;
  1043. if ( i < 0 ) {
  1044. i = 0;
  1045. } else if ( i >= this.pages.length ) {
  1046. i = this.pages.length - 1;
  1047. }
  1048. x = this.pages[i][0].x;
  1049. }
  1050. if ( m == this.currentPage.pageY ) {
  1051. m += this.directionY;
  1052. if ( m < 0 ) {
  1053. m = 0;
  1054. } else if ( m >= this.pages[0].length ) {
  1055. m = this.pages[0].length - 1;
  1056. }
  1057. y = this.pages[0][m].y;
  1058. }
  1059. return {
  1060. x: x,
  1061. y: y,
  1062. pageX: i,
  1063. pageY: m
  1064. };
  1065. },
  1066. goToPage: function (x, y, time, easing) {
  1067. easing = easing || this.options.bounceEasing;
  1068. if ( x >= this.pages.length ) {
  1069. x = this.pages.length - 1;
  1070. } else if ( x < 0 ) {
  1071. x = 0;
  1072. }
  1073. if ( y >= this.pages[x].length ) {
  1074. y = this.pages[x].length - 1;
  1075. } else if ( y < 0 ) {
  1076. y = 0;
  1077. }
  1078. var posX = this.pages[x][y].x,
  1079. posY = this.pages[x][y].y;
  1080. time = time === undefined ? this.options.snapSpeed || Math.max(
  1081. Math.max(
  1082. Math.min(Math.abs(posX - this.x), 1000),
  1083. Math.min(Math.abs(posY - this.y), 1000)
  1084. ), 300) : time;
  1085. this.currentPage = {
  1086. x: posX,
  1087. y: posY,
  1088. pageX: x,
  1089. pageY: y
  1090. };
  1091. this.scrollTo(posX, posY, time, easing);
  1092. },
  1093. next: function (time, easing) {
  1094. var x = this.currentPage.pageX,
  1095. y = this.currentPage.pageY;
  1096. x++;
  1097. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  1098. x = 0;
  1099. y++;
  1100. }
  1101. this.goToPage(x, y, time, easing);
  1102. },
  1103. prev: function (time, easing) {
  1104. var x = this.currentPage.pageX,
  1105. y = this.currentPage.pageY;
  1106. x--;
  1107. if ( x < 0 && this.hasVerticalScroll ) {
  1108. x = 0;
  1109. y--;
  1110. }
  1111. this.goToPage(x, y, time, easing);
  1112. },
  1113. _initKeys: function (e) {
  1114. // default key bindings
  1115. var keys = {
  1116. pageUp: 33,
  1117. pageDown: 34,
  1118. end: 35,
  1119. home: 36,
  1120. left: 37,
  1121. up: 38,
  1122. right: 39,
  1123. down: 40
  1124. };
  1125. var i;
  1126. // if you give me characters I give you keycode
  1127. if ( typeof this.options.keyBindings == 'object' ) {
  1128. for ( i in this.options.keyBindings ) {
  1129. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1130. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1131. }
  1132. }
  1133. } else {
  1134. this.options.keyBindings = {};
  1135. }
  1136. for ( i in keys ) {
  1137. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1138. }
  1139. utils.addEvent(window, 'keydown', this);
  1140. this.on('destroy', function () {
  1141. utils.removeEvent(window, 'keydown', this);
  1142. });
  1143. },
  1144. _key: function (e) {
  1145. if ( !this.enabled ) {
  1146. return;
  1147. }
  1148. var snap = this.options.snap, // we are using this alot, better to cache it
  1149. newX = snap ? this.currentPage.pageX : this.x,
  1150. newY = snap ? this.currentPage.pageY : this.y,
  1151. now = utils.getTime(),
  1152. prevTime = this.keyTime || 0,
  1153. acceleration = 0.250,
  1154. pos;
  1155. if ( this.options.useTransition && this.isInTransition ) {
  1156. pos = this.getComputedPosition();
  1157. this._translate(Math.round(pos.x), Math.round(pos.y));
  1158. this.isInTransition = false;
  1159. }
  1160. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1161. switch ( e.keyCode ) {
  1162. case this.options.keyBindings.pageUp:
  1163. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1164. newX += snap ? 1 : this.wrapperWidth;
  1165. } else {
  1166. newY += snap ? 1 : this.wrapperHeight;
  1167. }
  1168. break;
  1169. case this.options.keyBindings.pageDown:
  1170. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1171. newX -= snap ? 1 : this.wrapperWidth;
  1172. } else {
  1173. newY -= snap ? 1 : this.wrapperHeight;
  1174. }
  1175. break;
  1176. case this.options.keyBindings.end:
  1177. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1178. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1179. break;
  1180. case this.options.keyBindings.home:
  1181. newX = 0;
  1182. newY = 0;
  1183. break;
  1184. case this.options.keyBindings.left:
  1185. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1186. break;
  1187. case this.options.keyBindings.up:
  1188. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1189. break;
  1190. case this.options.keyBindings.right:
  1191. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1192. break;
  1193. case this.options.keyBindings.down:
  1194. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1195. break;
  1196. default:
  1197. return;
  1198. }
  1199. if ( snap ) {
  1200. this.goToPage(newX, newY);
  1201. return;
  1202. }
  1203. if ( newX > 0 ) {
  1204. newX = 0;
  1205. this.keyAcceleration = 0;
  1206. } else if ( newX < this.maxScrollX ) {
  1207. newX = this.maxScrollX;
  1208. this.keyAcceleration = 0;
  1209. }
  1210. if ( newY > 0 ) {
  1211. newY = 0;
  1212. this.keyAcceleration = 0;
  1213. } else if ( newY < this.maxScrollY ) {
  1214. newY = this.maxScrollY;
  1215. this.keyAcceleration = 0;
  1216. }
  1217. this.scrollTo(newX, newY, 0);
  1218. this.keyTime = now;
  1219. },
  1220. _animate: function (destX, destY, duration, easingFn) {
  1221. var that = this,
  1222. startX = this.x,
  1223. startY = this.y,
  1224. startTime = utils.getTime(),
  1225. destTime = startTime + duration;
  1226. function step () {
  1227. var now = utils.getTime(),
  1228. newX, newY,
  1229. easing;
  1230. if ( now >= destTime ) {
  1231. that.isAnimating = false;
  1232. that._translate(destX, destY);
  1233. if ( !that.resetPosition(that.options.bounceTime) ) {
  1234. that._execEvent('scrollEnd');
  1235. }
  1236. return;
  1237. }
  1238. now = ( now - startTime ) / duration;
  1239. easing = easingFn(now);
  1240. newX = ( destX - startX ) * easing + startX;
  1241. newY = ( destY - startY ) * easing + startY;
  1242. that._translate(newX, newY);
  1243. if ( that.isAnimating ) {
  1244. rAF(step);
  1245. }
  1246. if ( that.options.probeType == 3 ) {
  1247. that._execEvent('scroll');
  1248. }
  1249. }
  1250. this.isAnimating = true;
  1251. step();
  1252. },
  1253. handleEvent: function (e) {
  1254. switch ( e.type ) {
  1255. case 'touchstart':
  1256. case 'pointerdown':
  1257. case 'MSPointerDown':
  1258. case 'mousedown':
  1259. this._start(e);
  1260. break;
  1261. case 'touchmove':
  1262. case 'pointermove':
  1263. case 'MSPointerMove':
  1264. case 'mousemove':
  1265. this._move(e);
  1266. break;
  1267. case 'touchend':
  1268. case 'pointerup':
  1269. case 'MSPointerUp':
  1270. case 'mouseup':
  1271. case 'touchcancel':
  1272. case 'pointercancel':
  1273. case 'MSPointerCancel':
  1274. case 'mousecancel':
  1275. this._end(e);
  1276. break;
  1277. case 'orientationchange':
  1278. case 'resize':
  1279. this._resize();
  1280. break;
  1281. case 'transitionend':
  1282. case 'webkitTransitionEnd':
  1283. case 'oTransitionEnd':
  1284. case 'MSTransitionEnd':
  1285. this._transitionEnd(e);
  1286. break;
  1287. case 'wheel':
  1288. case 'DOMMouseScroll':
  1289. case 'mousewheel':
  1290. this._wheel(e);
  1291. break;
  1292. case 'keydown':
  1293. this._key(e);
  1294. break;
  1295. case 'click':
  1296. if ( !e._constructed ) {
  1297. e.preventDefault();
  1298. e.stopPropagation();
  1299. }
  1300. break;
  1301. }
  1302. }
  1303. };
  1304. function createDefaultScrollbar (direction, interactive, type) {
  1305. var scrollbar = document.createElement('div'),
  1306. indicator = document.createElement('div');
  1307. if ( type === true ) {
  1308. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1309. 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';
  1310. }
  1311. indicator.className = 'iScrollIndicator';
  1312. if ( direction == 'h' ) {
  1313. if ( type === true ) {
  1314. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1315. indicator.style.height = '100%';
  1316. }
  1317. scrollbar.className = 'iScrollHorizontalScrollbar';
  1318. } else {
  1319. if ( type === true ) {
  1320. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1321. indicator.style.width = '100%';
  1322. }
  1323. scrollbar.className = 'iScrollVerticalScrollbar';
  1324. }
  1325. scrollbar.style.cssText += ';overflow:hidden';
  1326. if ( !interactive ) {
  1327. scrollbar.style.pointerEvents = 'none';
  1328. }
  1329. scrollbar.appendChild(indicator);
  1330. return scrollbar;
  1331. }
  1332. function Indicator (scroller, options) {
  1333. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1334. this.wrapperStyle = this.wrapper.style;
  1335. this.indicator = this.wrapper.children[0];
  1336. this.indicatorStyle = this.indicator.style;
  1337. this.scroller = scroller;
  1338. this.options = {
  1339. listenX: true,
  1340. listenY: true,
  1341. interactive: false,
  1342. resize: true,
  1343. defaultScrollbars: false,
  1344. shrink: false,
  1345. fade: false,
  1346. speedRatioX: 0,
  1347. speedRatioY: 0
  1348. };
  1349. for ( var i in options ) {
  1350. this.options[i] = options[i];
  1351. }
  1352. this.sizeRatioX = 1;
  1353. this.sizeRatioY = 1;
  1354. this.maxPosX = 0;
  1355. this.maxPosY = 0;
  1356. if ( this.options.interactive ) {
  1357. if ( !this.options.disableTouch ) {
  1358. utils.addEvent(this.indicator, 'touchstart', this);
  1359. utils.addEvent(window, 'touchend', this);
  1360. }
  1361. if ( !this.options.disablePointer ) {
  1362. utils.addEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1363. utils.addEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1364. }
  1365. if ( !this.options.disableMouse ) {
  1366. utils.addEvent(this.indicator, 'mousedown', this);
  1367. utils.addEvent(window, 'mouseup', this);
  1368. }
  1369. }
  1370. if ( this.options.fade ) {
  1371. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1372. this.wrapperStyle[utils.style.transitionDuration] = utils.isBadAndroid ? '0.001s' : '0ms';
  1373. this.wrapperStyle.opacity = '0';
  1374. }
  1375. }
  1376. Indicator.prototype = {
  1377. handleEvent: function (e) {
  1378. switch ( e.type ) {
  1379. case 'touchstart':
  1380. case 'pointerdown':
  1381. case 'MSPointerDown':
  1382. case 'mousedown':
  1383. this._start(e);
  1384. break;
  1385. case 'touchmove':
  1386. case 'pointermove':
  1387. case 'MSPointerMove':
  1388. case 'mousemove':
  1389. this._move(e);
  1390. break;
  1391. case 'touchend':
  1392. case 'pointerup':
  1393. case 'MSPointerUp':
  1394. case 'mouseup':
  1395. case 'touchcancel':
  1396. case 'pointercancel':
  1397. case 'MSPointerCancel':
  1398. case 'mousecancel':
  1399. this._end(e);
  1400. break;
  1401. }
  1402. },
  1403. destroy: function () {
  1404. if ( this.options.interactive ) {
  1405. utils.removeEvent(this.indicator, 'touchstart', this);
  1406. utils.removeEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1407. utils.removeEvent(this.indicator, 'mousedown', this);
  1408. utils.removeEvent(window, 'touchmove', this);
  1409. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1410. utils.removeEvent(window, 'mousemove', this);
  1411. utils.removeEvent(window, 'touchend', this);
  1412. utils.removeEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1413. utils.removeEvent(window, 'mouseup', this);
  1414. }
  1415. if ( this.options.defaultScrollbars ) {
  1416. this.wrapper.parentNode.removeChild(this.wrapper);
  1417. }
  1418. },
  1419. _start: function (e) {
  1420. var point = e.touches ? e.touches[0] : e;
  1421. e.preventDefault();
  1422. e.stopPropagation();
  1423. this.transitionTime();
  1424. this.initiated = true;
  1425. this.moved = false;
  1426. this.lastPointX = point.pageX;
  1427. this.lastPointY = point.pageY;
  1428. this.startTime = utils.getTime();
  1429. if ( !this.options.disableTouch ) {
  1430. utils.addEvent(window, 'touchmove', this);
  1431. }
  1432. if ( !this.options.disablePointer ) {
  1433. utils.addEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1434. }
  1435. if ( !this.options.disableMouse ) {
  1436. utils.addEvent(window, 'mousemove', this);
  1437. }
  1438. this.scroller._execEvent('beforeScrollStart');
  1439. },
  1440. _move: function (e) {
  1441. var point = e.touches ? e.touches[0] : e,
  1442. deltaX, deltaY,
  1443. newX, newY,
  1444. timestamp = utils.getTime();
  1445. if ( !this.moved ) {
  1446. this.scroller._execEvent('scrollStart');
  1447. }
  1448. this.moved = true;
  1449. deltaX = point.pageX - this.lastPointX;
  1450. this.lastPointX = point.pageX;
  1451. deltaY = point.pageY - this.lastPointY;
  1452. this.lastPointY = point.pageY;
  1453. newX = this.x + deltaX;
  1454. newY = this.y + deltaY;
  1455. this._pos(newX, newY);
  1456. if ( this.scroller.options.probeType == 1 && timestamp - this.startTime > 300 ) {
  1457. this.startTime = timestamp;
  1458. this.scroller._execEvent('scroll');
  1459. } else if ( this.scroller.options.probeType > 1 ) {
  1460. this.scroller._execEvent('scroll');
  1461. }
  1462. // INSERT POINT: indicator._move
  1463. e.preventDefault();
  1464. e.stopPropagation();
  1465. },
  1466. _end: function (e) {
  1467. if ( !this.initiated ) {
  1468. return;
  1469. }
  1470. this.initiated = false;
  1471. e.preventDefault();
  1472. e.stopPropagation();
  1473. utils.removeEvent(window, 'touchmove', this);
  1474. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1475. utils.removeEvent(window, 'mousemove', this);
  1476. if ( this.scroller.options.snap ) {
  1477. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1478. var time = this.options.snapSpeed || Math.max(
  1479. Math.max(
  1480. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1481. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1482. ), 300);
  1483. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1484. this.scroller.directionX = 0;
  1485. this.scroller.directionY = 0;
  1486. this.scroller.currentPage = snap;
  1487. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1488. }
  1489. }
  1490. if ( this.moved ) {
  1491. this.scroller._execEvent('scrollEnd');
  1492. }
  1493. },
  1494. transitionTime: function (time) {
  1495. time = time || 0;
  1496. this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
  1497. if ( !time && utils.isBadAndroid ) {
  1498. this.indicatorStyle[utils.style.transitionDuration] = '0.001s';
  1499. }
  1500. },
  1501. transitionTimingFunction: function (easing) {
  1502. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  1503. },
  1504. refresh: function () {
  1505. this.transitionTime();
  1506. if ( this.options.listenX && !this.options.listenY ) {
  1507. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  1508. } else if ( this.options.listenY && !this.options.listenX ) {
  1509. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  1510. } else {
  1511. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  1512. }
  1513. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  1514. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  1515. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  1516. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1517. if ( this.options.listenX ) {
  1518. this.wrapper.style.right = '8px';
  1519. } else {
  1520. this.wrapper.style.bottom = '8px';
  1521. }
  1522. }
  1523. } else {
  1524. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  1525. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  1526. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1527. if ( this.options.listenX ) {
  1528. this.wrapper.style.right = '2px';
  1529. } else {
  1530. this.wrapper.style.bottom = '2px';
  1531. }
  1532. }
  1533. }
  1534. var r = this.wrapper.offsetHeight; // force refresh
  1535. if ( this.options.listenX ) {
  1536. this.wrapperWidth = this.wrapper.clientWidth;
  1537. if ( this.options.resize ) {
  1538. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  1539. this.indicatorStyle.width = this.indicatorWidth + 'px';
  1540. } else {
  1541. this.indicatorWidth = this.indicator.clientWidth;
  1542. }
  1543. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  1544. if ( this.options.shrink == 'clip' ) {
  1545. this.minBoundaryX = -this.indicatorWidth + 8;
  1546. this.maxBoundaryX = this.wrapperWidth - 8;
  1547. } else {
  1548. this.minBoundaryX = 0;
  1549. this.maxBoundaryX = this.maxPosX;
  1550. }
  1551. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  1552. }
  1553. if ( this.options.listenY ) {
  1554. this.wrapperHeight = this.wrapper.clientHeight;
  1555. if ( this.options.resize ) {
  1556. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  1557. this.indicatorStyle.height = this.indicatorHeight + 'px';
  1558. } else {
  1559. this.indicatorHeight = this.indicator.clientHeight;
  1560. }
  1561. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1562. if ( this.options.shrink == 'clip' ) {
  1563. this.minBoundaryY = -this.indicatorHeight + 8;
  1564. this.maxBoundaryY = this.wrapperHeight - 8;
  1565. } else {
  1566. this.minBoundaryY = 0;
  1567. this.maxBoundaryY = this.maxPosY;
  1568. }
  1569. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1570. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  1571. }
  1572. this.updatePosition();
  1573. },
  1574. updatePosition: function () {
  1575. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  1576. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  1577. if ( !this.options.ignoreBoundaries ) {
  1578. if ( x < this.minBoundaryX ) {
  1579. if ( this.options.shrink == 'scale' ) {
  1580. this.width = Math.max(this.indicatorWidth + x, 8);
  1581. this.indicatorStyle.width = this.width + 'px';
  1582. }
  1583. x = this.minBoundaryX;
  1584. } else if ( x > this.maxBoundaryX ) {
  1585. if ( this.options.shrink == 'scale' ) {
  1586. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  1587. this.indicatorStyle.width = this.width + 'px';
  1588. x = this.maxPosX + this.indicatorWidth - this.width;
  1589. } else {
  1590. x = this.maxBoundaryX;
  1591. }
  1592. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  1593. this.width = this.indicatorWidth;
  1594. this.indicatorStyle.width = this.width + 'px';
  1595. }
  1596. if ( y < this.minBoundaryY ) {
  1597. if ( this.options.shrink == 'scale' ) {
  1598. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  1599. this.indicatorStyle.height = this.height + 'px';
  1600. }
  1601. y = this.minBoundaryY;
  1602. } else if ( y > this.maxBoundaryY ) {
  1603. if ( this.options.shrink == 'scale' ) {
  1604. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  1605. this.indicatorStyle.height = this.height + 'px';
  1606. y = this.maxPosY + this.indicatorHeight - this.height;
  1607. } else {
  1608. y = this.maxBoundaryY;
  1609. }
  1610. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  1611. this.height = this.indicatorHeight;
  1612. this.indicatorStyle.height = this.height + 'px';
  1613. }
  1614. }
  1615. this.x = x;
  1616. this.y = y;
  1617. if ( this.scroller.options.useTransform ) {
  1618. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  1619. } else {
  1620. this.indicatorStyle.left = x + 'px';
  1621. this.indicatorStyle.top = y + 'px';
  1622. }
  1623. },
  1624. _pos: function (x, y) {
  1625. if ( x < 0 ) {
  1626. x = 0;
  1627. } else if ( x > this.maxPosX ) {
  1628. x = this.maxPosX;
  1629. }
  1630. if ( y < 0 ) {
  1631. y = 0;
  1632. } else if ( y > this.maxPosY ) {
  1633. y = this.maxPosY;
  1634. }
  1635. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  1636. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  1637. this.scroller.scrollTo(x, y);
  1638. },
  1639. fade: function (val, hold) {
  1640. if ( hold && !this.visible ) {
  1641. return;
  1642. }
  1643. clearTimeout(this.fadeTimeout);
  1644. this.fadeTimeout = null;
  1645. var time = val ? 250 : 500,
  1646. delay = val ? 0 : 300;
  1647. val = val ? '1' : '0';
  1648. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  1649. this.fadeTimeout = setTimeout((function (val) {
  1650. this.wrapperStyle.opacity = val;
  1651. this.visible = +val;
  1652. }).bind(this, val), delay);
  1653. }
  1654. };
  1655. IScroll.utils = utils;
  1656. if ( typeof module != 'undefined' && module.exports ) {
  1657. module.exports = IScroll;
  1658. } else {
  1659. window.IScroll = IScroll;
  1660. }
  1661. })(window, document, Math);
  1662. ;(function($,window){
  1663. //模块对外提供的公共方法
  1664. var exportsMethods = {
  1665. /**
  1666. * 新建一个竖直滚动实例,并做一些处理,整合上拉下拉的功能
  1667. * wrapper 要渲染滚动实例的位置
  1668. * pulldownAction 下拉执行的逻辑
  1669. * pullupAction 上拉执行的逻辑
  1670. * opts 滚动个性化参数
  1671. * pullText 拉动时不同状态要显示的文字
  1672. */
  1673. newVerScrollForPull : function(wrapper,pulldownAction,pullupAction,opts,pullText){
  1674. var $wrapper ;
  1675. if(typeof wrapper === 'string'){
  1676. $wrapper = $(wrapper);
  1677. }else if(typeof wrapper === 'object'){
  1678. $wrapper = wrapper;
  1679. }
  1680. var pulldownRefresh = pullText && pullText['pulldownRefresh'] ? pullText['pulldownRefresh'] : '下拉刷新...',
  1681. pullupLoadingMore = pullText && pullText['pullupLoadingMore'] ? pullText['pullupLoadingMore'] : '上拉加载更多...',
  1682. releaseToRefresh = pullText && pullText['releaseToRefresh'] ? pullText['releaseToRefresh'] : '松手开始刷新...',
  1683. releaseToLoading = pullText && pullText['releaseToLoading'] ? pullText['releaseToLoading'] : '松手开始加载...',
  1684. loading = pullText && pullText['loading'] ? pullText['loading'] : '加载中...';
  1685. if(pullupAction!=null){
  1686. var $pullup = $('<div class="pullUp" />').appendTo($wrapper.children('.pull-iscroll-box')),
  1687. $pullUpIcon = $('<span class="pullUpIcon"><i></i></span>').appendTo($pullup),
  1688. $pullUpLabel = $('<span class="pullUpLabel">Pull up to refresh...</span>').appendTo($pullup);
  1689. }
  1690. if(pulldownAction!=null){
  1691. var $pulldown = $('<div class="pullDown" />').prependTo($wrapper.children('.pull-iscroll-box')),
  1692. $pullDownIcon = $('<span class="pullDownIcon"><i></i></span>').appendTo($pulldown),
  1693. $pullDownLabel = $('<span class="pullDownLabel">Pull down to refresh...</span>').appendTo($pulldown);
  1694. }
  1695. var pullupOffset = 0,
  1696. pulldownOffset = 0;
  1697. if($pulldown){
  1698. pulldownOffset = $pulldown.outerHeight();
  1699. $pullDownLabel.html(pulldownRefresh);
  1700. }
  1701. if($pullup){
  1702. pullupOffset = $pullup.outerHeight();
  1703. $pullUpLabel.html(pullupLoadingMore);
  1704. }
  1705. //这个属性很重要,目前V5版本不支持,需修改源码
  1706. var options = {
  1707. topOffset : pulldownOffset,
  1708. useTransition : false,
  1709. probeType : 3
  1710. };
  1711. $.extend(true,options,opts);
  1712. var scrollObj = this.newVerScroll($wrapper[0],options);
  1713. //滚动刷新触发的事件
  1714. scrollObj.on('refresh',function(){
  1715. if ($pulldown && $pulldown.hasClass('loading')) {
  1716. $pulldown.removeClass('loading');
  1717. $pullDownLabel.html(pulldownRefresh);
  1718. } else if ($pullup){
  1719. $pullUpIcon.show();
  1720. if($pullup.hasClass('loading')){
  1721. $pullUpIcon.show();
  1722. $pullup.removeClass('loading');
  1723. $pullUpLabel.html(pullupLoadingMore);
  1724. }
  1725. }
  1726. });
  1727. //滚动的时候触发的事件
  1728. scrollObj.on('scroll',function(){
  1729. if ($pulldown && this.y > 5 && !$pulldown.hasClass('flip')) {
  1730. $pulldown.addClass('flip');
  1731. $pullDownLabel.html(releaseToRefresh);
  1732. this.minScrollY = 0;
  1733. } else if ($pulldown && this.y < -10 && $pulldown.hasClass('flip')) {
  1734. $pulldown.removeClass('flip');
  1735. $pullDownLabel.html(pulldownRefresh);
  1736. this.minScrollY = -pulldownOffset;
  1737. //this.y < this.minScrollY代表是上拉,以防下拉的时候未拉到尽头时进入上拉的逻辑中
  1738. } else if ($pullup && this.y < this.minScrollY && this.y < (this.maxScrollY - 5) && !$pullup.hasClass('flip')) {
  1739. $pullup.addClass('flip');
  1740. $pullUpLabel.html(releaseToLoading);
  1741. this.maxScrollY = this.maxScrollY;
  1742. } else if ($pullup && (this.y > (this.maxScrollY + 5)) && $pullup.hasClass('flip')) {
  1743. $pullup.removeClass('flip');
  1744. $pullUpLabel.html(pullupLoadingMore);
  1745. }
  1746. });
  1747. //滚动结束之后触发的事件
  1748. scrollObj.on('scrollEnd',function(){
  1749. if ($pulldown && $pulldown.hasClass('flip')) {
  1750. $pulldown.removeClass('flip').addClass('loading');
  1751. $pullDownLabel.html(loading);
  1752. if(typeof pulldownAction === 'function'){
  1753. pulldownAction.call(scrollObj);
  1754. }
  1755. } else if ($pullup && $pullup.hasClass('flip')) {
  1756. $pullup.removeClass('flip').addClass('loading');
  1757. $pullUpLabel.html(loading);
  1758. if(typeof pullupAction === 'function' && $pullup.parent().length>0){
  1759. pullupAction.call(scrollObj);
  1760. }
  1761. }
  1762. });
  1763. return scrollObj;
  1764. },
  1765. /**
  1766. * 创建一个竖直方向的滚动实例
  1767. * @param obj dom对象或者选择字符串
  1768. * @param option 滚动其他属性
  1769. * @return IScroll实例对象
  1770. */
  1771. newVerScroll : function(dom,option){
  1772. var opt = {
  1773. scrollbars : true, //是否有滚动条
  1774. useTransition: true,
  1775. fadeScrollbars : true,
  1776. click: true
  1777. };
  1778. if(option){
  1779. $.extend(opt,option);
  1780. }
  1781. var iSObj = new IScroll(dom,opt);
  1782. return iSObj;
  1783. }
  1784. };
  1785. window.iscrollAssist = exportsMethods;
  1786. })(jQuery,window);