bscroll.js 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472
  1. /*!
  2. * better-normal-scroll v1.15.2
  3. * (c) 2016-2019 ustbhuangyi
  4. * Released under the MIT License.
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global.BScroll = factory());
  10. }(this, (function () { 'use strict';
  11. // As of V8 6.6, depending on the size of the array, this is anywhere
  12. // between 1.5-10x faster than the two-arg version of Array#splice()
  13. function spliceOne(list, index) {
  14. for (; index + 1 < list.length; index++) {
  15. list[index] = list[index + 1];
  16. }
  17. list.pop();
  18. }
  19. var slicedToArray = function () {
  20. function sliceIterator(arr, i) {
  21. var _arr = [];
  22. var _n = true;
  23. var _d = false;
  24. var _e = undefined;
  25. try {
  26. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  27. _arr.push(_s.value);
  28. if (i && _arr.length === i) break;
  29. }
  30. } catch (err) {
  31. _d = true;
  32. _e = err;
  33. } finally {
  34. try {
  35. if (!_n && _i["return"]) _i["return"]();
  36. } finally {
  37. if (_d) throw _e;
  38. }
  39. }
  40. return _arr;
  41. }
  42. return function (arr, i) {
  43. if (Array.isArray(arr)) {
  44. return arr;
  45. } else if (Symbol.iterator in Object(arr)) {
  46. return sliceIterator(arr, i);
  47. } else {
  48. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  49. }
  50. };
  51. }();
  52. var toConsumableArray = function (arr) {
  53. if (Array.isArray(arr)) {
  54. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  55. return arr2;
  56. } else {
  57. return Array.from(arr);
  58. }
  59. };
  60. function eventMixin(BScroll) {
  61. BScroll.prototype.on = function (type, fn) {
  62. var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this;
  63. if (!this._events[type]) {
  64. this._events[type] = [];
  65. }
  66. this._events[type].push([fn, context]);
  67. };
  68. BScroll.prototype.once = function (type, fn) {
  69. var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this;
  70. function magic() {
  71. this.off(type, magic);
  72. fn.apply(context, arguments);
  73. }
  74. // To expose the corresponding function method in order to execute the off method
  75. magic.fn = fn;
  76. this.on(type, magic);
  77. };
  78. BScroll.prototype.off = function (type, fn) {
  79. var _events = this._events[type];
  80. if (!_events) {
  81. return;
  82. }
  83. var count = _events.length;
  84. while (count--) {
  85. if (_events[count][0] === fn || _events[count][0] && _events[count][0].fn === fn) {
  86. spliceOne(_events, count);
  87. }
  88. }
  89. };
  90. BScroll.prototype.trigger = function (type) {
  91. var events = this._events[type];
  92. if (!events) {
  93. return;
  94. }
  95. var len = events.length;
  96. var eventsCopy = [].concat(toConsumableArray(events));
  97. for (var i = 0; i < len; i++) {
  98. var event = eventsCopy[i];
  99. var _event = slicedToArray(event, 2),
  100. fn = _event[0],
  101. context = _event[1];
  102. if (fn) {
  103. fn.apply(context, [].slice.call(arguments, 1));
  104. }
  105. }
  106. };
  107. }
  108. // ssr support
  109. var inBrowser = typeof window !== 'undefined';
  110. var ua = inBrowser && navigator.userAgent.toLowerCase();
  111. var isWeChatDevTools = ua && /wechatdevtools/.test(ua);
  112. var isAndroid = ua && ua.indexOf('android') > 0;
  113. function getNow() {
  114. return window.performance && window.performance.now ? window.performance.now() + window.performance.timing.navigationStart : +new Date();
  115. }
  116. function extend(target) {
  117. for (var _len = arguments.length, rest = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  118. rest[_key - 1] = arguments[_key];
  119. }
  120. for (var i = 0; i < rest.length; i++) {
  121. var source = rest[i];
  122. for (var key in source) {
  123. target[key] = source[key];
  124. }
  125. }
  126. return target;
  127. }
  128. function isUndef(v) {
  129. return v === undefined || v === null;
  130. }
  131. function getDistance(x, y) {
  132. return Math.sqrt(x * x + y * y);
  133. }
  134. var elementStyle = inBrowser && document.createElement('div').style;
  135. var vendor = function () {
  136. if (!inBrowser) {
  137. return false;
  138. }
  139. // first pick up standard to fix #743
  140. var transformNames = {
  141. standard: 'transform',
  142. webkit: 'webkitTransform',
  143. Moz: 'MozTransform',
  144. O: 'OTransform',
  145. ms: 'msTransform'
  146. };
  147. for (var key in transformNames) {
  148. if (elementStyle[transformNames[key]] !== undefined) {
  149. return key;
  150. }
  151. }
  152. return false;
  153. }();
  154. function prefixStyle(style) {
  155. if (vendor === false) {
  156. return false;
  157. }
  158. if (vendor === 'standard') {
  159. if (style === 'transitionEnd') {
  160. return 'transitionend';
  161. }
  162. return style;
  163. }
  164. return vendor + style.charAt(0).toUpperCase() + style.substr(1);
  165. }
  166. function addEvent(el, type, fn, capture) {
  167. el.addEventListener(type, fn, { passive: false, capture: !!capture });
  168. }
  169. function removeEvent(el, type, fn, capture) {
  170. el.removeEventListener(type, fn, { passive: false, capture: !!capture });
  171. }
  172. function offset(el) {
  173. var left = 0;
  174. var top = 0;
  175. while (el) {
  176. left -= el.offsetLeft;
  177. top -= el.offsetTop;
  178. el = el.offsetParent;
  179. }
  180. return {
  181. left: left,
  182. top: top
  183. };
  184. }
  185. function offsetToBody(el) {
  186. var rect = el.getBoundingClientRect();
  187. return {
  188. left: -(rect.left + window.pageXOffset),
  189. top: -(rect.top + window.pageYOffset)
  190. };
  191. }
  192. var cssVendor = vendor && vendor !== 'standard' ? '-' + vendor.toLowerCase() + '-' : '';
  193. var transform = prefixStyle('transform');
  194. var transition = prefixStyle('transition');
  195. var hasPerspective = inBrowser && prefixStyle('perspective') in elementStyle;
  196. // fix issue #361
  197. var hasTouch = inBrowser && ('ontouchstart' in window || isWeChatDevTools);
  198. var hasTransform = transform !== false;
  199. var hasTransition = inBrowser && transition in elementStyle;
  200. var style = {
  201. transform: transform,
  202. transition: transition,
  203. transitionTimingFunction: prefixStyle('transitionTimingFunction'),
  204. transitionDuration: prefixStyle('transitionDuration'),
  205. transitionDelay: prefixStyle('transitionDelay'),
  206. transformOrigin: prefixStyle('transformOrigin'),
  207. transitionEnd: prefixStyle('transitionEnd')
  208. };
  209. var TOUCH_EVENT = 1;
  210. var MOUSE_EVENT = 2;
  211. var eventType = {
  212. touchstart: TOUCH_EVENT,
  213. touchmove: TOUCH_EVENT,
  214. touchend: TOUCH_EVENT,
  215. mousedown: MOUSE_EVENT,
  216. mousemove: MOUSE_EVENT,
  217. mouseup: MOUSE_EVENT
  218. };
  219. function getRect(el) {
  220. if (el instanceof window.SVGElement) {
  221. var rect = el.getBoundingClientRect();
  222. return {
  223. top: rect.top,
  224. left: rect.left,
  225. width: rect.width,
  226. height: rect.height
  227. };
  228. } else {
  229. return {
  230. top: el.offsetTop,
  231. left: el.offsetLeft,
  232. width: el.offsetWidth,
  233. height: el.offsetHeight
  234. };
  235. }
  236. }
  237. function preventDefaultException(el, exceptions) {
  238. for (var i in exceptions) {
  239. if (exceptions[i].test(el[i])) {
  240. return true;
  241. }
  242. }
  243. return false;
  244. }
  245. function tap(e, eventName) {
  246. var ev = document.createEvent('Event');
  247. ev.initEvent(eventName, true, true);
  248. ev.pageX = e.pageX;
  249. ev.pageY = e.pageY;
  250. e.target.dispatchEvent(ev);
  251. }
  252. function click(e) {
  253. var event = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'click';
  254. var eventSource = void 0;
  255. if (e.type === 'mouseup' || e.type === 'mousecancel') {
  256. eventSource = e;
  257. } else if (e.type === 'touchend' || e.type === 'touchcancel') {
  258. eventSource = e.changedTouches[0];
  259. }
  260. var posSrc = {};
  261. if (eventSource) {
  262. posSrc.screenX = eventSource.screenX || 0;
  263. posSrc.screenY = eventSource.screenY || 0;
  264. posSrc.clientX = eventSource.clientX || 0;
  265. posSrc.clientY = eventSource.clientY || 0;
  266. }
  267. var ev = void 0;
  268. var bubbles = true;
  269. var cancelable = true;
  270. if (typeof MouseEvent !== 'undefined') {
  271. try {
  272. ev = new MouseEvent(event, extend({
  273. bubbles: bubbles,
  274. cancelable: cancelable
  275. }, posSrc));
  276. } catch (e) {
  277. createEvent();
  278. }
  279. } else {
  280. createEvent();
  281. }
  282. function createEvent() {
  283. ev = document.createEvent('Event');
  284. ev.initEvent(event, bubbles, cancelable);
  285. extend(ev, posSrc);
  286. }
  287. // forwardedTouchEvent set to true in case of the conflict with fastclick
  288. ev.forwardedTouchEvent = true;
  289. ev._constructed = true;
  290. e.target.dispatchEvent(ev);
  291. }
  292. function dblclick(e) {
  293. click(e, 'dblclick');
  294. }
  295. function prepend(el, target) {
  296. if (target.firstChild) {
  297. before(el, target.firstChild);
  298. } else {
  299. target.appendChild(el);
  300. }
  301. }
  302. function before(el, target) {
  303. target.parentNode.insertBefore(el, target);
  304. }
  305. function removeChild(el, child) {
  306. el.removeChild(child);
  307. }
  308. var DEFAULT_OPTIONS = {
  309. startX: 0,
  310. startY: 0,
  311. scrollX: false,
  312. scrollY: true,
  313. freeScroll: false,
  314. directionLockThreshold: 5,
  315. eventPassthrough: '',
  316. click: false,
  317. tap: false,
  318. /**
  319. * support any side
  320. * bounce: {
  321. * top: true,
  322. * bottom: true,
  323. * left: true,
  324. * right: true
  325. * }
  326. */
  327. bounce: true,
  328. bounceTime: 800,
  329. momentum: true,
  330. momentumLimitTime: 300,
  331. momentumLimitDistance: 15,
  332. swipeTime: 2500,
  333. swipeBounceTime: 500,
  334. deceleration: 0.0015,
  335. flickLimitTime: 200,
  336. flickLimitDistance: 100,
  337. resizePolling: 60,
  338. probeType: 0,
  339. preventDefault: true,
  340. preventDefaultException: {
  341. tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT|AUDIO)$/
  342. },
  343. HWCompositing: true,
  344. useTransition: true,
  345. useTransform: true,
  346. bindToWrapper: false,
  347. disableMouse: hasTouch,
  348. disableTouch: !hasTouch,
  349. observeDOM: true,
  350. autoBlur: true,
  351. /**
  352. * for picker
  353. * wheel: {
  354. * selectedIndex: 0,
  355. * rotate: 25,
  356. * adjustTime: 400
  357. * wheelWrapperClass: 'wheel-scroll',
  358. * wheelItemClass: 'wheel-item'
  359. * }
  360. */
  361. wheel: false,
  362. /**
  363. * for slide
  364. * snap: {
  365. * loop: false,
  366. * el: domEl,
  367. * threshold: 0.1,
  368. * stepX: 100,
  369. * stepY: 100,
  370. * speed: 400,
  371. * easing: {
  372. * style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  373. * fn: function (t) {
  374. * return t * (2 - t)
  375. * }
  376. * }
  377. * listenFlick: true
  378. * }
  379. */
  380. snap: false,
  381. /**
  382. * for scrollbar
  383. * scrollbar: {
  384. * fade: true,
  385. * interactive: false
  386. * }
  387. */
  388. scrollbar: false,
  389. /**
  390. * for pull down and refresh
  391. * pullDownRefresh: {
  392. * threshold: 50,
  393. * stop: 20
  394. * }
  395. */
  396. pullDownRefresh: false,
  397. /**
  398. * for pull up and load
  399. * pullUpLoad: {
  400. * threshold: 50
  401. * }
  402. */
  403. pullUpLoad: false,
  404. /**
  405. * for mouse wheel
  406. * mouseWheel: {
  407. * speed: 20,
  408. * invert: false,
  409. * easeTime: 300
  410. * }
  411. */
  412. mouseWheel: false,
  413. stopPropagation: false,
  414. /**
  415. * for zoom
  416. * zoom: {
  417. * start: 1,
  418. * min: 1,
  419. * max: 4
  420. * }
  421. */
  422. zoom: false,
  423. /**
  424. * for infinity
  425. * infinity: {
  426. * render(item, div) {
  427. * },
  428. * createTombstone() {
  429. * },
  430. * fetch(count) {
  431. * }
  432. * }
  433. */
  434. infinity: false,
  435. /**
  436. * for double click
  437. * dblclick: {
  438. * delay: 300
  439. * }
  440. */
  441. dblclick: false
  442. };
  443. function initMixin(BScroll) {
  444. BScroll.prototype._init = function (options) {
  445. this._handleOptions(options);
  446. // init private custom events
  447. this._events = {};
  448. this.x = 0;
  449. this.y = 0;
  450. this.directionX = 0;
  451. this.directionY = 0;
  452. this.setScale(1);
  453. this._addDOMEvents();
  454. this._initExtFeatures();
  455. this._watchTransition();
  456. if (this.options.observeDOM) {
  457. this._initDOMObserver();
  458. }
  459. if (this.options.autoBlur) {
  460. this._handleAutoBlur();
  461. }
  462. this.refresh();
  463. if (!this.options.snap) {
  464. this.scrollTo(this.options.startX, this.options.startY);
  465. }
  466. this.enable();
  467. };
  468. BScroll.prototype.setScale = function (scale) {
  469. this.lastScale = isUndef(this.scale) ? scale : this.scale;
  470. this.scale = scale;
  471. };
  472. BScroll.prototype._handleOptions = function (options) {
  473. this.options = extend({}, DEFAULT_OPTIONS, options);
  474. this.translateZ = this.options.HWCompositing && hasPerspective ? ' translateZ(0)' : '';
  475. this.options.useTransition = this.options.useTransition && hasTransition;
  476. this.options.useTransform = this.options.useTransform && hasTransform;
  477. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  478. // If you want eventPassthrough I have to lock one of the axes
  479. this.options.scrollX = this.options.eventPassthrough === 'horizontal' ? false : this.options.scrollX;
  480. this.options.scrollY = this.options.eventPassthrough === 'vertical' ? false : this.options.scrollY;
  481. // With eventPassthrough we also need lockDirection mechanism
  482. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  483. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  484. if (this.options.tap === true) {
  485. this.options.tap = 'tap';
  486. }
  487. };
  488. BScroll.prototype._addDOMEvents = function () {
  489. var eventOperation = addEvent;
  490. this._handleDOMEvents(eventOperation);
  491. };
  492. BScroll.prototype._removeDOMEvents = function () {
  493. var eventOperation = removeEvent;
  494. this._handleDOMEvents(eventOperation);
  495. };
  496. BScroll.prototype._handleDOMEvents = function (eventOperation) {
  497. var target = this.options.bindToWrapper ? this.wrapper : window;
  498. eventOperation(window, 'orientationchange', this);
  499. eventOperation(window, 'resize', this);
  500. if (this.options.click) {
  501. eventOperation(this.wrapper, 'click', this, true);
  502. }
  503. if (!this.options.disableMouse) {
  504. eventOperation(this.wrapper, 'mousedown', this);
  505. eventOperation(target, 'mousemove', this);
  506. eventOperation(target, 'mousecancel', this);
  507. eventOperation(target, 'mouseup', this);
  508. }
  509. if (hasTouch && !this.options.disableTouch) {
  510. eventOperation(this.wrapper, 'touchstart', this);
  511. eventOperation(target, 'touchmove', this);
  512. eventOperation(target, 'touchcancel', this);
  513. eventOperation(target, 'touchend', this);
  514. }
  515. eventOperation(this.scroller, style.transitionEnd, this);
  516. };
  517. BScroll.prototype._initExtFeatures = function () {
  518. if (this.options.snap) {
  519. this._initSnap();
  520. }
  521. if (this.options.scrollbar) {
  522. this._initScrollbar();
  523. }
  524. if (this.options.pullUpLoad) {
  525. this._initPullUp();
  526. }
  527. if (this.options.pullDownRefresh) {
  528. this._initPullDown();
  529. }
  530. if (this.options.wheel) {
  531. this._initWheel();
  532. }
  533. if (this.options.mouseWheel) {
  534. this._initMouseWheel();
  535. }
  536. if (this.options.zoom) {
  537. this._initZoom();
  538. }
  539. if (this.options.infinity) {
  540. this._initInfinite();
  541. }
  542. };
  543. BScroll.prototype._watchTransition = function () {
  544. if (typeof Object.defineProperty !== 'function') {
  545. return;
  546. }
  547. var me = this;
  548. var isInTransition = false;
  549. var key = this.options.useTransition ? 'isInTransition' : 'isAnimating';
  550. Object.defineProperty(this, key, {
  551. get: function get() {
  552. return isInTransition;
  553. },
  554. set: function set(newVal) {
  555. isInTransition = newVal;
  556. // fix issue #359
  557. var el = me.scroller.children.length ? me.scroller.children : [me.scroller];
  558. var pointerEvents = isInTransition && !me.pulling ? 'none' : 'auto';
  559. for (var i = 0; i < el.length; i++) {
  560. el[i].style.pointerEvents = pointerEvents;
  561. }
  562. }
  563. });
  564. };
  565. BScroll.prototype._handleAutoBlur = function () {
  566. this.on('scrollStart', function () {
  567. var activeElement = document.activeElement;
  568. if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
  569. activeElement.blur();
  570. }
  571. });
  572. };
  573. BScroll.prototype._initDOMObserver = function () {
  574. var _this = this;
  575. if (typeof MutationObserver !== 'undefined') {
  576. var timer = void 0;
  577. var observer = new MutationObserver(function (mutations) {
  578. // don't do any refresh during the transition, or outside of the boundaries
  579. if (_this._shouldNotRefresh()) {
  580. return;
  581. }
  582. var immediateRefresh = false;
  583. var deferredRefresh = false;
  584. for (var i = 0; i < mutations.length; i++) {
  585. var mutation = mutations[i];
  586. if (mutation.type !== 'attributes') {
  587. immediateRefresh = true;
  588. break;
  589. } else {
  590. if (mutation.target !== _this.scroller) {
  591. deferredRefresh = true;
  592. break;
  593. }
  594. }
  595. }
  596. if (immediateRefresh) {
  597. _this.refresh();
  598. } else if (deferredRefresh) {
  599. // attributes changes too often
  600. clearTimeout(timer);
  601. timer = setTimeout(function () {
  602. if (!_this._shouldNotRefresh()) {
  603. _this.refresh();
  604. }
  605. }, 60);
  606. }
  607. });
  608. var config = {
  609. attributes: true,
  610. childList: true,
  611. subtree: true
  612. };
  613. observer.observe(this.scroller, config);
  614. this.on('destroy', function () {
  615. observer.disconnect();
  616. });
  617. } else {
  618. this._checkDOMUpdate();
  619. }
  620. };
  621. BScroll.prototype._shouldNotRefresh = function () {
  622. var outsideBoundaries = this.x > this.minScrollX || this.x < this.maxScrollX || this.y > this.minScrollY || this.y < this.maxScrollY;
  623. return this.isInTransition || this.stopFromTransition || outsideBoundaries;
  624. };
  625. BScroll.prototype._checkDOMUpdate = function () {
  626. var scrollerRect = getRect(this.scroller);
  627. var oldWidth = scrollerRect.width;
  628. var oldHeight = scrollerRect.height;
  629. function check() {
  630. if (this.destroyed) {
  631. return;
  632. }
  633. scrollerRect = getRect(this.scroller);
  634. var newWidth = scrollerRect.width;
  635. var newHeight = scrollerRect.height;
  636. if (oldWidth !== newWidth || oldHeight !== newHeight) {
  637. this.refresh();
  638. }
  639. oldWidth = newWidth;
  640. oldHeight = newHeight;
  641. next.call(this);
  642. }
  643. function next() {
  644. var _this2 = this;
  645. setTimeout(function () {
  646. check.call(_this2);
  647. }, 1000);
  648. }
  649. next.call(this);
  650. };
  651. BScroll.prototype.handleEvent = function (e) {
  652. switch (e.type) {
  653. case 'touchstart':
  654. case 'mousedown':
  655. this._start(e);
  656. if (this.options.zoom && e.touches && e.touches.length > 1) {
  657. this._zoomStart(e);
  658. }
  659. break;
  660. case 'touchmove':
  661. case 'mousemove':
  662. if (this.options.zoom && e.touches && e.touches.length > 1) {
  663. this._zoom(e);
  664. } else {
  665. this._move(e);
  666. }
  667. break;
  668. case 'touchend':
  669. case 'mouseup':
  670. case 'touchcancel':
  671. case 'mousecancel':
  672. if (this.scaled) {
  673. this._zoomEnd(e);
  674. } else {
  675. this._end(e);
  676. }
  677. break;
  678. case 'orientationchange':
  679. case 'resize':
  680. this._resize();
  681. break;
  682. case 'transitionend':
  683. case 'webkitTransitionEnd':
  684. case 'oTransitionEnd':
  685. case 'MSTransitionEnd':
  686. this._transitionEnd(e);
  687. break;
  688. case 'click':
  689. if (this.enabled && !e._constructed) {
  690. if (!preventDefaultException(e.target, this.options.preventDefaultException)) {
  691. e.preventDefault();
  692. e.stopPropagation();
  693. }
  694. }
  695. break;
  696. case 'wheel':
  697. case 'DOMMouseScroll':
  698. case 'mousewheel':
  699. this._onMouseWheel(e);
  700. break;
  701. }
  702. };
  703. BScroll.prototype.refresh = function () {
  704. var isWrapperStatic = window.getComputedStyle(this.wrapper, null).position === 'static';
  705. var wrapperRect = getRect(this.wrapper);
  706. this.wrapperWidth = wrapperRect.width;
  707. this.wrapperHeight = wrapperRect.height;
  708. var scrollerRect = getRect(this.scroller);
  709. this.scrollerWidth = Math.round(scrollerRect.width * this.scale);
  710. this.scrollerHeight = Math.round(scrollerRect.height * this.scale);
  711. this.relativeX = scrollerRect.left;
  712. this.relativeY = scrollerRect.top;
  713. if (isWrapperStatic) {
  714. this.relativeX -= wrapperRect.left;
  715. this.relativeY -= wrapperRect.top;
  716. }
  717. this.minScrollX = 0;
  718. this.minScrollY = 0;
  719. var wheel = this.options.wheel;
  720. if (wheel) {
  721. this.items = this.scroller.children;
  722. // check whether there are all disable items or not when refresh
  723. this._checkWheelAllDisabled();
  724. this.options.itemHeight = this.itemHeight = this.items.length ? this.scrollerHeight / this.items.length : 0;
  725. if (this.selectedIndex === undefined) {
  726. this.selectedIndex = wheel.selectedIndex || 0;
  727. }
  728. this.options.startY = -this.selectedIndex * this.itemHeight;
  729. this.maxScrollX = 0;
  730. this.maxScrollY = -this.itemHeight * (this.items.length - 1);
  731. } else {
  732. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  733. if (!this.options.infinity) {
  734. this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  735. }
  736. if (this.maxScrollX < 0) {
  737. this.maxScrollX -= this.relativeX;
  738. this.minScrollX = -this.relativeX;
  739. } else if (this.scale > 1) {
  740. this.maxScrollX = this.maxScrollX / 2 - this.relativeX;
  741. this.minScrollX = this.maxScrollX;
  742. }
  743. if (this.maxScrollY < 0) {
  744. this.maxScrollY -= this.relativeY;
  745. this.minScrollY = -this.relativeY;
  746. } else if (this.scale > 1) {
  747. this.maxScrollY = this.maxScrollY / 2 - this.relativeY;
  748. this.minScrollY = this.maxScrollY;
  749. }
  750. }
  751. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < this.minScrollX;
  752. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < this.minScrollY;
  753. if (!this.hasHorizontalScroll) {
  754. this.maxScrollX = this.minScrollX;
  755. this.scrollerWidth = this.wrapperWidth;
  756. }
  757. if (!this.hasVerticalScroll) {
  758. this.maxScrollY = this.minScrollY;
  759. this.scrollerHeight = this.wrapperHeight;
  760. }
  761. this.endTime = 0;
  762. this.directionX = 0;
  763. this.directionY = 0;
  764. this.wrapperOffset = offset(this.wrapper);
  765. this.trigger('refresh');
  766. !this.scaled && this.resetPosition();
  767. };
  768. BScroll.prototype.enable = function () {
  769. this.enabled = true;
  770. };
  771. BScroll.prototype.disable = function () {
  772. this.enabled = false;
  773. };
  774. }
  775. var ease = {
  776. // easeOutQuint
  777. swipe: {
  778. style: 'cubic-bezier(0.23, 1, 0.32, 1)',
  779. fn: function fn(t) {
  780. return 1 + --t * t * t * t * t;
  781. }
  782. },
  783. // easeOutQuard
  784. swipeBounce: {
  785. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  786. fn: function fn(t) {
  787. return t * (2 - t);
  788. }
  789. },
  790. // easeOutQuart
  791. bounce: {
  792. style: 'cubic-bezier(0.165, 0.84, 0.44, 1)',
  793. fn: function fn(t) {
  794. return 1 - --t * t * t * t;
  795. }
  796. }
  797. };
  798. function momentum(current, start, time, lowerMargin, upperMargin, wrapperSize, options, scroll) {
  799. var distance = current - start;
  800. var speed = Math.abs(distance) / time;
  801. var deceleration = options.deceleration,
  802. itemHeight = options.itemHeight,
  803. swipeBounceTime = options.swipeBounceTime,
  804. wheel = options.wheel,
  805. swipeTime = options.swipeTime;
  806. var duration = swipeTime;
  807. var rate = wheel ? 4 : 15;
  808. var destination = current + speed / deceleration * (distance < 0 ? -1 : 1);
  809. if (wheel && itemHeight) {
  810. destination = scroll._findNearestValidWheel(destination).y;
  811. }
  812. if (destination < lowerMargin) {
  813. destination = wrapperSize ? Math.max(lowerMargin - wrapperSize / 4, lowerMargin - wrapperSize / rate * speed) : lowerMargin;
  814. duration = swipeBounceTime;
  815. } else if (destination > upperMargin) {
  816. destination = wrapperSize ? Math.min(upperMargin + wrapperSize / 4, upperMargin + wrapperSize / rate * speed) : upperMargin;
  817. duration = swipeBounceTime;
  818. }
  819. return {
  820. destination: Math.round(destination),
  821. duration: duration
  822. };
  823. }
  824. var DEFAULT_INTERVAL = 100 / 60;
  825. function noop() {}
  826. var requestAnimationFrame = function () {
  827. if (!inBrowser) {
  828. /* istanbul ignore if */
  829. return noop;
  830. }
  831. return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame ||
  832. // if all else fails, use setTimeout
  833. function (callback) {
  834. return window.setTimeout(callback, (callback.interval || DEFAULT_INTERVAL) / 2); // make interval as precise as possible.
  835. };
  836. }();
  837. var cancelAnimationFrame = function () {
  838. if (!inBrowser) {
  839. /* istanbul ignore if */
  840. return noop;
  841. }
  842. return window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || function (id) {
  843. window.clearTimeout(id);
  844. };
  845. }();
  846. var DIRECTION_UP = 1;
  847. var DIRECTION_DOWN = -1;
  848. var DIRECTION_LEFT = 1;
  849. var DIRECTION_RIGHT = -1;
  850. var PROBE_DEBOUNCE = 1;
  851. var PROBE_REALTIME = 3;
  852. function warn(msg) {
  853. console.error('[BScroll warn]: ' + msg);
  854. }
  855. function assert(condition, msg) {
  856. if (!condition) {
  857. throw new Error('[BScroll] ' + msg);
  858. }
  859. }
  860. function coreMixin(BScroll) {
  861. BScroll.prototype._start = function (e) {
  862. var _eventType = eventType[e.type];
  863. if (_eventType !== TOUCH_EVENT) {
  864. if (e.button !== 0) {
  865. return;
  866. }
  867. }
  868. if (!this.enabled || this.destroyed || this.initiated && this.initiated !== _eventType) {
  869. return;
  870. }
  871. this.initiated = _eventType;
  872. if (this.options.preventDefault && !preventDefaultException(e.target, this.options.preventDefaultException)) {
  873. e.preventDefault();
  874. }
  875. if (this.options.stopPropagation) {
  876. e.stopPropagation();
  877. }
  878. this.moved = false;
  879. this.distX = 0;
  880. this.distY = 0;
  881. this.directionX = 0;
  882. this.directionY = 0;
  883. this.movingDirectionX = 0;
  884. this.movingDirectionY = 0;
  885. this.directionLocked = 0;
  886. this._transitionTime();
  887. this.startTime = getNow();
  888. if (this.options.wheel) {
  889. this.target = e.target;
  890. }
  891. this.stop();
  892. var point = e.touches ? e.touches[0] : e;
  893. this.startX = this.x;
  894. this.startY = this.y;
  895. this.absStartX = this.x;
  896. this.absStartY = this.y;
  897. this.pointX = point.pageX;
  898. this.pointY = point.pageY;
  899. this.trigger('beforeScrollStart');
  900. };
  901. BScroll.prototype._move = function (e) {
  902. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  903. return;
  904. }
  905. if (this.options.preventDefault) {
  906. e.preventDefault();
  907. }
  908. if (this.options.stopPropagation) {
  909. e.stopPropagation();
  910. }
  911. var point = e.touches ? e.touches[0] : e;
  912. var deltaX = point.pageX - this.pointX;
  913. var deltaY = point.pageY - this.pointY;
  914. this.pointX = point.pageX;
  915. this.pointY = point.pageY;
  916. this.distX += deltaX;
  917. this.distY += deltaY;
  918. var absDistX = Math.abs(this.distX);
  919. var absDistY = Math.abs(this.distY);
  920. var timestamp = getNow();
  921. // We need to move at least momentumLimitDistance pixels for the scrolling to initiate
  922. if (timestamp - this.endTime > this.options.momentumLimitTime && !this.moved && absDistY < this.options.momentumLimitDistance && absDistX < this.options.momentumLimitDistance) {
  923. return;
  924. }
  925. // If you are scrolling in one direction lock the other
  926. if (!this.directionLocked && !this.options.freeScroll) {
  927. if (absDistX > absDistY + this.options.directionLockThreshold) {
  928. this.directionLocked = 'h'; // lock horizontally
  929. } else if (absDistY >= absDistX + this.options.directionLockThreshold) {
  930. this.directionLocked = 'v'; // lock vertically
  931. } else {
  932. this.directionLocked = 'n'; // no lock
  933. }
  934. }
  935. if (this.directionLocked === 'h') {
  936. if (this.options.eventPassthrough === 'vertical') {
  937. e.preventDefault();
  938. } else if (this.options.eventPassthrough === 'horizontal') {
  939. this.initiated = false;
  940. return;
  941. }
  942. deltaY = 0;
  943. } else if (this.directionLocked === 'v') {
  944. if (this.options.eventPassthrough === 'horizontal') {
  945. e.preventDefault();
  946. } else if (this.options.eventPassthrough === 'vertical') {
  947. this.initiated = false;
  948. return;
  949. }
  950. deltaX = 0;
  951. }
  952. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  953. deltaY = this.hasVerticalScroll ? deltaY : 0;
  954. this.movingDirectionX = deltaX > 0 ? DIRECTION_RIGHT : deltaX < 0 ? DIRECTION_LEFT : 0;
  955. this.movingDirectionY = deltaY > 0 ? DIRECTION_DOWN : deltaY < 0 ? DIRECTION_UP : 0;
  956. var newX = this.x + deltaX;
  957. var newY = this.y + deltaY;
  958. var top = false;
  959. var bottom = false;
  960. var left = false;
  961. var right = false;
  962. // Slow down or stop if outside of the boundaries
  963. var bounce = this.options.bounce;
  964. if (bounce !== false) {
  965. top = bounce.top === undefined ? true : bounce.top;
  966. bottom = bounce.bottom === undefined ? true : bounce.bottom;
  967. left = bounce.left === undefined ? true : bounce.left;
  968. right = bounce.right === undefined ? true : bounce.right;
  969. }
  970. if (newX > this.minScrollX || newX < this.maxScrollX) {
  971. if (newX > this.minScrollX && left || newX < this.maxScrollX && right) {
  972. newX = this.x + deltaX / 3;
  973. } else {
  974. newX = newX > this.minScrollX ? this.minScrollX : this.maxScrollX;
  975. }
  976. }
  977. if (newY > this.minScrollY || newY < this.maxScrollY) {
  978. if (newY > this.minScrollY && top || newY < this.maxScrollY && bottom) {
  979. newY = this.y + deltaY / 3;
  980. } else {
  981. newY = newY > this.minScrollY ? this.minScrollY : this.maxScrollY;
  982. }
  983. }
  984. if (!this.moved) {
  985. this.moved = true;
  986. this.trigger('scrollStart');
  987. }
  988. this._translate(newX, newY);
  989. if (timestamp - this.startTime > this.options.momentumLimitTime) {
  990. this.startTime = timestamp;
  991. this.startX = this.x;
  992. this.startY = this.y;
  993. if (this.options.probeType === PROBE_DEBOUNCE) {
  994. this.trigger('scroll', {
  995. x: this.x,
  996. y: this.y
  997. });
  998. }
  999. }
  1000. if (this.options.probeType > PROBE_DEBOUNCE) {
  1001. this.trigger('scroll', {
  1002. x: this.x,
  1003. y: this.y
  1004. });
  1005. }
  1006. var scrollLeft = document.documentElement.scrollLeft || window.pageXOffset || document.body.scrollLeft;
  1007. var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  1008. var pX = this.pointX - scrollLeft;
  1009. var pY = this.pointY - scrollTop;
  1010. if (pX > document.documentElement.clientWidth - this.options.momentumLimitDistance || pX < this.options.momentumLimitDistance || pY < this.options.momentumLimitDistance || pY > document.documentElement.clientHeight - this.options.momentumLimitDistance) {
  1011. this._end(e);
  1012. }
  1013. };
  1014. BScroll.prototype._end = function (e) {
  1015. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  1016. return;
  1017. }
  1018. this.initiated = false;
  1019. if (this.options.preventDefault && !preventDefaultException(e.target, this.options.preventDefaultException)) {
  1020. e.preventDefault();
  1021. }
  1022. if (this.options.stopPropagation) {
  1023. e.stopPropagation();
  1024. }
  1025. this.trigger('touchEnd', {
  1026. x: this.x,
  1027. y: this.y
  1028. });
  1029. this.isInTransition = false;
  1030. // ensures that the last position is rounded
  1031. var newX = Math.round(this.x);
  1032. var newY = Math.round(this.y);
  1033. var deltaX = newX - this.absStartX;
  1034. var deltaY = newY - this.absStartY;
  1035. this.directionX = deltaX > 0 ? DIRECTION_RIGHT : deltaX < 0 ? DIRECTION_LEFT : 0;
  1036. this.directionY = deltaY > 0 ? DIRECTION_DOWN : deltaY < 0 ? DIRECTION_UP : 0;
  1037. // if configure pull down refresh, check it first
  1038. if (this.options.pullDownRefresh && this._checkPullDown()) {
  1039. return;
  1040. }
  1041. // check if it is a click operation
  1042. if (this._checkClick(e)) {
  1043. this.trigger('scrollCancel');
  1044. return;
  1045. }
  1046. // reset if we are outside of the boundaries
  1047. if (this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1048. return;
  1049. }
  1050. this._translate(newX, newY);
  1051. this.endTime = getNow();
  1052. var duration = this.endTime - this.startTime;
  1053. var absDistX = Math.abs(newX - this.startX);
  1054. var absDistY = Math.abs(newY - this.startY);
  1055. // flick
  1056. if (this._events.flick && duration < this.options.flickLimitTime && absDistX < this.options.flickLimitDistance && absDistY < this.options.flickLimitDistance) {
  1057. this.trigger('flick');
  1058. return;
  1059. }
  1060. var time = 0;
  1061. // start momentum animation if needed
  1062. if (this.options.momentum && duration < this.options.momentumLimitTime && (absDistY > this.options.momentumLimitDistance || absDistX > this.options.momentumLimitDistance)) {
  1063. var top = false;
  1064. var bottom = false;
  1065. var left = false;
  1066. var right = false;
  1067. var bounce = this.options.bounce;
  1068. if (bounce !== false) {
  1069. top = bounce.top === undefined ? true : bounce.top;
  1070. bottom = bounce.bottom === undefined ? true : bounce.bottom;
  1071. left = bounce.left === undefined ? true : bounce.left;
  1072. right = bounce.right === undefined ? true : bounce.right;
  1073. }
  1074. var wrapperWidth = this.directionX === DIRECTION_RIGHT && left || this.directionX === DIRECTION_LEFT && right ? this.wrapperWidth : 0;
  1075. var wrapperHeight = this.directionY === DIRECTION_DOWN && top || this.directionY === DIRECTION_UP && bottom ? this.wrapperHeight : 0;
  1076. var momentumX = this.hasHorizontalScroll ? momentum(this.x, this.startX, duration, this.maxScrollX, this.minScrollX, wrapperWidth, this.options, this) : { destination: newX, duration: 0 };
  1077. var momentumY = this.hasVerticalScroll ? momentum(this.y, this.startY, duration, this.maxScrollY, this.minScrollY, wrapperHeight, this.options, this) : { destination: newY, duration: 0 };
  1078. newX = momentumX.destination;
  1079. newY = momentumY.destination;
  1080. time = Math.max(momentumX.duration, momentumY.duration);
  1081. this.isInTransition = true;
  1082. } else {
  1083. if (this.options.wheel) {
  1084. newY = this._findNearestValidWheel(newY).y;
  1085. time = this.options.wheel.adjustTime || 400;
  1086. }
  1087. }
  1088. var easing = ease.swipe;
  1089. if (this.options.snap) {
  1090. var snap = this._nearestSnap(newX, newY);
  1091. this.currentPage = snap;
  1092. time = this.options.snapSpeed || Math.max(Math.max(Math.min(Math.abs(newX - snap.x), 1000), Math.min(Math.abs(newY - snap.y), 1000)), 300);
  1093. newX = snap.x;
  1094. newY = snap.y;
  1095. this.directionX = 0;
  1096. this.directionY = 0;
  1097. easing = this.options.snap.easing || ease.bounce;
  1098. }
  1099. if (newX !== this.x || newY !== this.y) {
  1100. // change easing function when scroller goes out of the boundaries
  1101. if (newX > this.minScrollX || newX < this.maxScrollX || newY > this.minScrollY || newY < this.maxScrollY) {
  1102. easing = ease.swipeBounce;
  1103. }
  1104. this.scrollTo(newX, newY, time, easing);
  1105. return;
  1106. }
  1107. if (this.options.wheel) {
  1108. this.selectedIndex = this._findNearestValidWheel(this.y).index;
  1109. }
  1110. this.trigger('scrollEnd', {
  1111. x: this.x,
  1112. y: this.y
  1113. });
  1114. };
  1115. BScroll.prototype._checkClick = function (e) {
  1116. // when in the process of pulling down, it should not prevent click
  1117. var preventClick = this.stopFromTransition && !this.pulling;
  1118. this.stopFromTransition = false;
  1119. // we scrolled less than 15 pixels
  1120. if (!this.moved) {
  1121. if (this.options.wheel) {
  1122. if (this.target && this.target.className === this.options.wheel.wheelWrapperClass) {
  1123. var index = this._findNearestValidWheel(this.y).index;
  1124. var _offset = Math.round((this.pointY + offsetToBody(this.wrapper).top - this.wrapperHeight / 2) / this.itemHeight);
  1125. this.target = this.items[index + _offset];
  1126. }
  1127. var top = offset(this.target).top;
  1128. var left = offset(this.target).left;
  1129. top -= this.wrapperOffset.top;
  1130. top -= Math.round(this.target.offsetHeight / 2 - this.wrapper.offsetHeight / 2) || 0;
  1131. left -= this.wrapperOffset.left;
  1132. left -= Math.round(this.target.offsetWidth / 2 - this.wrapper.offsetWidth / 2) || 0;
  1133. top = this._findNearestValidWheel(top).y;
  1134. this.scrollTo(left, top, this.options.wheel.adjustTime || 400, ease.swipe);
  1135. return true;
  1136. } else {
  1137. if (!preventClick) {
  1138. var _dblclick = this.options.dblclick;
  1139. var dblclickTrigged = false;
  1140. if (_dblclick && this.lastClickTime) {
  1141. var _dblclick$delay = _dblclick.delay,
  1142. delay = _dblclick$delay === undefined ? 300 : _dblclick$delay;
  1143. if (getNow() - this.lastClickTime < delay) {
  1144. dblclickTrigged = true;
  1145. dblclick(e);
  1146. }
  1147. }
  1148. if (this.options.tap) {
  1149. tap(e, this.options.tap);
  1150. }
  1151. if (this.options.click && !preventDefaultException(e.target, this.options.preventDefaultException)) {
  1152. click(e);
  1153. }
  1154. this.lastClickTime = dblclickTrigged ? null : getNow();
  1155. return true;
  1156. }
  1157. return false;
  1158. }
  1159. }
  1160. return false;
  1161. };
  1162. BScroll.prototype._resize = function () {
  1163. var _this = this;
  1164. if (!this.enabled) {
  1165. return;
  1166. }
  1167. // fix a scroll problem under Android condition
  1168. if (isAndroid) {
  1169. this.wrapper.scrollTop = 0;
  1170. }
  1171. clearTimeout(this.resizeTimeout);
  1172. this.resizeTimeout = setTimeout(function () {
  1173. _this.refresh();
  1174. }, this.options.resizePolling);
  1175. };
  1176. BScroll.prototype._startProbe = function () {
  1177. cancelAnimationFrame(this.probeTimer);
  1178. this.probeTimer = requestAnimationFrame(probe);
  1179. var me = this;
  1180. function probe() {
  1181. var pos = me.getComputedPosition();
  1182. me.trigger('scroll', pos);
  1183. if (!me.isInTransition) {
  1184. me.trigger('scrollEnd', pos);
  1185. return;
  1186. }
  1187. me.probeTimer = requestAnimationFrame(probe);
  1188. }
  1189. };
  1190. BScroll.prototype._transitionTime = function () {
  1191. var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1192. this.scrollerStyle[style.transitionDuration] = time + 'ms';
  1193. if (this.options.wheel) {
  1194. for (var i = 0; i < this.items.length; i++) {
  1195. this.items[i].style[style.transitionDuration] = time + 'ms';
  1196. }
  1197. }
  1198. if (this.indicators) {
  1199. for (var _i = 0; _i < this.indicators.length; _i++) {
  1200. this.indicators[_i].transitionTime(time);
  1201. }
  1202. }
  1203. };
  1204. BScroll.prototype._transitionTimingFunction = function (easing) {
  1205. this.scrollerStyle[style.transitionTimingFunction] = easing;
  1206. if (this.options.wheel) {
  1207. for (var i = 0; i < this.items.length; i++) {
  1208. this.items[i].style[style.transitionTimingFunction] = easing;
  1209. }
  1210. }
  1211. if (this.indicators) {
  1212. for (var _i2 = 0; _i2 < this.indicators.length; _i2++) {
  1213. this.indicators[_i2].transitionTimingFunction(easing);
  1214. }
  1215. }
  1216. };
  1217. BScroll.prototype._transitionEnd = function (e) {
  1218. if (e.target !== this.scroller || !this.isInTransition) {
  1219. return;
  1220. }
  1221. this._transitionTime();
  1222. var needReset = !this.pulling || this.movingDirectionY === DIRECTION_UP;
  1223. if (needReset && !this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1224. this.isInTransition = false;
  1225. if (this.options.probeType !== PROBE_REALTIME) {
  1226. this.trigger('scrollEnd', {
  1227. x: this.x,
  1228. y: this.y
  1229. });
  1230. }
  1231. }
  1232. };
  1233. BScroll.prototype._translate = function (x, y, scale) {
  1234. assert(!isUndef(x) && !isUndef(y), 'Translate x or y is null or undefined.');
  1235. if (isUndef(scale)) {
  1236. scale = this.scale;
  1237. }
  1238. if (this.options.useTransform) {
  1239. this.scrollerStyle[style.transform] = 'translate(' + x + 'px,' + y + 'px) scale(' + scale + ')' + this.translateZ;
  1240. } else {
  1241. x = Math.round(x);
  1242. y = Math.round(y);
  1243. this.scrollerStyle.left = x + 'px';
  1244. this.scrollerStyle.top = y + 'px';
  1245. }
  1246. if (this.options.wheel) {
  1247. var _options$wheel$rotate = this.options.wheel.rotate,
  1248. rotate = _options$wheel$rotate === undefined ? 25 : _options$wheel$rotate;
  1249. for (var i = 0; i < this.items.length; i++) {
  1250. var deg = rotate * (y / this.itemHeight + i);
  1251. this.items[i].style[style.transform] = 'rotateX(' + deg + 'deg)';
  1252. }
  1253. }
  1254. this.x = x;
  1255. this.y = y;
  1256. this.setScale(scale);
  1257. if (this.indicators) {
  1258. for (var _i3 = 0; _i3 < this.indicators.length; _i3++) {
  1259. this.indicators[_i3].updatePosition();
  1260. }
  1261. }
  1262. };
  1263. BScroll.prototype._animate = function (destX, destY, duration, easingFn) {
  1264. var me = this;
  1265. var startX = this.x;
  1266. var startY = this.y;
  1267. var startScale = this.lastScale;
  1268. var destScale = this.scale;
  1269. var startTime = getNow();
  1270. var destTime = startTime + duration;
  1271. function step() {
  1272. var now = getNow();
  1273. if (now >= destTime) {
  1274. me.isAnimating = false;
  1275. me._translate(destX, destY, destScale);
  1276. me.trigger('scroll', {
  1277. x: me.x,
  1278. y: me.y
  1279. });
  1280. if (!me.pulling && !me.resetPosition(me.options.bounceTime)) {
  1281. me.trigger('scrollEnd', {
  1282. x: me.x,
  1283. y: me.y
  1284. });
  1285. }
  1286. return;
  1287. }
  1288. now = (now - startTime) / duration;
  1289. var easing = easingFn(now);
  1290. var newX = (destX - startX) * easing + startX;
  1291. var newY = (destY - startY) * easing + startY;
  1292. var newScale = (destScale - startScale) * easing + startScale;
  1293. me._translate(newX, newY, newScale);
  1294. if (me.isAnimating) {
  1295. me.animateTimer = requestAnimationFrame(step);
  1296. }
  1297. if (me.options.probeType === PROBE_REALTIME) {
  1298. me.trigger('scroll', {
  1299. x: me.x,
  1300. y: me.y
  1301. });
  1302. }
  1303. }
  1304. this.isAnimating = true;
  1305. cancelAnimationFrame(this.animateTimer);
  1306. step();
  1307. };
  1308. BScroll.prototype.scrollBy = function (x, y) {
  1309. var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  1310. var easing = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ease.bounce;
  1311. x = this.x + x;
  1312. y = this.y + y;
  1313. this.scrollTo(x, y, time, easing);
  1314. };
  1315. BScroll.prototype.scrollTo = function (x, y) {
  1316. var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  1317. var easing = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ease.bounce;
  1318. var isSilent = arguments[4];
  1319. if (this.options.wheel) {
  1320. y = this._findNearestValidWheel(y).y;
  1321. }
  1322. if (x === this.x && y === this.y) {
  1323. return;
  1324. }
  1325. this.isInTransition = this.options.useTransition && time > 0 && (this.x !== x || this.y !== y);
  1326. if (!time || this.options.useTransition) {
  1327. this._transitionTimingFunction(easing.style);
  1328. this._transitionTime(time);
  1329. this._translate(x, y);
  1330. if (time && this.options.probeType === PROBE_REALTIME) {
  1331. this._startProbe();
  1332. }
  1333. if (!time && !isSilent) {
  1334. // don't trigger resetPosition when zoom feature is open, fix #748
  1335. if (this.options.zoom) return;
  1336. this.trigger('scroll', {
  1337. x: x,
  1338. y: y
  1339. });
  1340. // force reflow to put everything in position
  1341. this._reflow = document.body.offsetHeight;
  1342. if (!this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1343. this.trigger('scrollEnd', {
  1344. x: x,
  1345. y: y
  1346. });
  1347. }
  1348. }
  1349. if (this.options.wheel) {
  1350. this.selectedIndex = this._findNearestValidWheel(y).index;
  1351. }
  1352. } else {
  1353. this._animate(x, y, time, easing.fn);
  1354. }
  1355. };
  1356. BScroll.prototype.scrollToElement = function (el, time, offsetX, offsetY, easing) {
  1357. if (!el) {
  1358. return;
  1359. }
  1360. el = el.nodeType ? el : this.scroller.querySelector(el);
  1361. if (this.options.wheel && !el.classList.contains(this.options.wheel.wheelItemClass)) {
  1362. return;
  1363. }
  1364. var pos = offset(el);
  1365. pos.left -= this.wrapperOffset.left;
  1366. pos.top -= this.wrapperOffset.top;
  1367. // if offsetX/Y are true we center the element to the screen
  1368. if (offsetX === true) {
  1369. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  1370. }
  1371. if (offsetY === true) {
  1372. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  1373. }
  1374. pos.left -= offsetX || 0;
  1375. pos.top -= offsetY || 0;
  1376. pos.left = pos.left > this.minScrollX ? this.minScrollX : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  1377. pos.top = pos.top > this.minScrollY ? this.minScrollY : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  1378. if (this.options.wheel) {
  1379. pos.top = this._findNearestValidWheel(pos.top).y;
  1380. }
  1381. this.scrollTo(pos.left, pos.top, time, easing);
  1382. };
  1383. BScroll.prototype.resetPosition = function () {
  1384. var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1385. var easeing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ease.bounce;
  1386. var x = this.x;
  1387. var roundX = Math.round(x);
  1388. if (!this.hasHorizontalScroll || roundX > this.minScrollX) {
  1389. x = this.minScrollX;
  1390. } else if (roundX < this.maxScrollX) {
  1391. x = this.maxScrollX;
  1392. }
  1393. var y = this.y;
  1394. var roundY = Math.round(y);
  1395. if (!this.hasVerticalScroll || roundY > this.minScrollY) {
  1396. y = this.minScrollY;
  1397. } else if (roundY < this.maxScrollY) {
  1398. y = this.maxScrollY;
  1399. }
  1400. if (x === this.x && y === this.y) {
  1401. return false;
  1402. }
  1403. this.scrollTo(x, y, time, easeing);
  1404. return true;
  1405. };
  1406. BScroll.prototype.getComputedPosition = function () {
  1407. var matrix = window.getComputedStyle(this.scroller, null);
  1408. var x = void 0;
  1409. var y = void 0;
  1410. if (this.options.useTransform) {
  1411. matrix = matrix[style.transform].split(')')[0].split(', ');
  1412. x = +(matrix[12] || matrix[4]);
  1413. y = +(matrix[13] || matrix[5]);
  1414. } else {
  1415. x = +matrix.left.replace(/[^-\d.]/g, '');
  1416. y = +matrix.top.replace(/[^-\d.]/g, '');
  1417. }
  1418. return {
  1419. x: x,
  1420. y: y
  1421. };
  1422. };
  1423. BScroll.prototype.stop = function () {
  1424. if (this.options.useTransition && this.isInTransition) {
  1425. this.isInTransition = false;
  1426. cancelAnimationFrame(this.probeTimer);
  1427. var pos = this.getComputedPosition();
  1428. this._translate(pos.x, pos.y);
  1429. if (this.options.wheel) {
  1430. this.target = this.items[this._findNearestValidWheel(pos.y).index];
  1431. } else {
  1432. this.trigger('scrollEnd', {
  1433. x: this.x,
  1434. y: this.y
  1435. });
  1436. }
  1437. this.stopFromTransition = true;
  1438. } else if (!this.options.useTransition && this.isAnimating) {
  1439. this.isAnimating = false;
  1440. cancelAnimationFrame(this.animateTimer);
  1441. this.trigger('scrollEnd', {
  1442. x: this.x,
  1443. y: this.y
  1444. });
  1445. this.stopFromTransition = true;
  1446. }
  1447. };
  1448. BScroll.prototype.destroy = function () {
  1449. this.destroyed = true;
  1450. this.trigger('destroy');
  1451. if (this.options.useTransition) {
  1452. cancelAnimationFrame(this.probeTimer);
  1453. } else {
  1454. cancelAnimationFrame(this.animateTimer);
  1455. }
  1456. this._removeDOMEvents();
  1457. // remove custom events
  1458. this._events = {};
  1459. };
  1460. }
  1461. function snapMixin(BScroll) {
  1462. BScroll.prototype._initSnap = function () {
  1463. var _this = this;
  1464. this.currentPage = {};
  1465. var snap = this.options.snap;
  1466. if (snap.loop) {
  1467. var children = this.scroller.children;
  1468. if (children.length > 1) {
  1469. prepend(children[children.length - 1].cloneNode(true), this.scroller);
  1470. this.scroller.appendChild(children[1].cloneNode(true));
  1471. } else {
  1472. // Loop does not make any sense if there is only one child.
  1473. snap.loop = false;
  1474. }
  1475. }
  1476. var el = snap.el;
  1477. if (typeof el === 'string') {
  1478. el = this.scroller.querySelectorAll(el);
  1479. }
  1480. this.on('refresh', function () {
  1481. _this.pages = [];
  1482. if (!_this.wrapperWidth || !_this.wrapperHeight || !_this.scrollerWidth || !_this.scrollerHeight) {
  1483. return;
  1484. }
  1485. var stepX = snap.stepX || _this.wrapperWidth;
  1486. var stepY = snap.stepY || _this.wrapperHeight;
  1487. var x = 0;
  1488. var y = void 0;
  1489. var cx = void 0;
  1490. var cy = void 0;
  1491. var i = 0;
  1492. var l = void 0;
  1493. var m = 0;
  1494. var n = void 0;
  1495. var rect = void 0;
  1496. if (!el) {
  1497. cx = Math.round(stepX / 2);
  1498. cy = Math.round(stepY / 2);
  1499. while (x > -_this.scrollerWidth) {
  1500. _this.pages[i] = [];
  1501. l = 0;
  1502. y = 0;
  1503. while (y > -_this.scrollerHeight) {
  1504. _this.pages[i][l] = {
  1505. x: Math.max(x, _this.maxScrollX),
  1506. y: Math.max(y, _this.maxScrollY),
  1507. width: stepX,
  1508. height: stepY,
  1509. cx: x - cx,
  1510. cy: y - cy
  1511. };
  1512. y -= stepY;
  1513. l++;
  1514. }
  1515. x -= stepX;
  1516. i++;
  1517. }
  1518. } else {
  1519. l = el.length;
  1520. n = -1;
  1521. for (; i < l; i++) {
  1522. rect = getRect(el[i]);
  1523. if (i === 0 || rect.left <= getRect(el[i - 1]).left) {
  1524. m = 0;
  1525. n++;
  1526. }
  1527. if (!_this.pages[m]) {
  1528. _this.pages[m] = [];
  1529. }
  1530. x = Math.max(-rect.left, _this.maxScrollX);
  1531. y = Math.max(-rect.top, _this.maxScrollY);
  1532. cx = x - Math.round(rect.width / 2);
  1533. cy = y - Math.round(rect.height / 2);
  1534. _this.pages[m][n] = {
  1535. x: x,
  1536. y: y,
  1537. width: rect.width,
  1538. height: rect.height,
  1539. cx: cx,
  1540. cy: cy
  1541. };
  1542. if (x > _this.maxScrollX) {
  1543. m++;
  1544. }
  1545. }
  1546. }
  1547. _this._checkSnapLoop();
  1548. var initPageX = snap._loopX ? 1 : 0;
  1549. var initPageY = snap._loopY ? 1 : 0;
  1550. _this._goToPage(_this.currentPage.pageX || initPageX, _this.currentPage.pageY || initPageY, 0, undefined, true);
  1551. // Update snap threshold if needed.
  1552. var snapThreshold = snap.threshold;
  1553. if (snapThreshold % 1 === 0) {
  1554. _this.snapThresholdX = snapThreshold;
  1555. _this.snapThresholdY = snapThreshold;
  1556. } else {
  1557. _this.snapThresholdX = Math.round(_this.pages[_this.currentPage.pageX][_this.currentPage.pageY].width * snapThreshold);
  1558. _this.snapThresholdY = Math.round(_this.pages[_this.currentPage.pageX][_this.currentPage.pageY].height * snapThreshold);
  1559. }
  1560. });
  1561. this.on('scrollEnd', function () {
  1562. if (snap.loop) {
  1563. if (snap._loopX) {
  1564. if (_this.currentPage.pageX === 0) {
  1565. _this._goToPage(_this.pages.length - 2, _this.currentPage.pageY, 0, undefined, true);
  1566. }
  1567. if (_this.currentPage.pageX === _this.pages.length - 1) {
  1568. _this._goToPage(1, _this.currentPage.pageY, 0, undefined, true);
  1569. }
  1570. } else {
  1571. if (_this.currentPage.pageY === 0) {
  1572. _this._goToPage(_this.currentPage.pageX, _this.pages[0].length - 2, 0, undefined, true);
  1573. }
  1574. if (_this.currentPage.pageY === _this.pages[0].length - 1) {
  1575. _this._goToPage(_this.currentPage.pageX, 1, 0, undefined, true);
  1576. }
  1577. }
  1578. }
  1579. });
  1580. if (snap.listenFlick !== false) {
  1581. this.on('flick', function () {
  1582. var time = snap.speed || Math.max(Math.max(Math.min(Math.abs(_this.x - _this.startX), 1000), Math.min(Math.abs(_this.y - _this.startY), 1000)), 300);
  1583. _this._goToPage(_this.currentPage.pageX + _this.directionX, _this.currentPage.pageY + _this.directionY, time);
  1584. });
  1585. }
  1586. this.on('destroy', function () {
  1587. if (snap.loop) {
  1588. var _children = _this.scroller.children;
  1589. if (_children.length > 2) {
  1590. removeChild(_this.scroller, _children[_children.length - 1]);
  1591. removeChild(_this.scroller, _children[0]);
  1592. }
  1593. }
  1594. });
  1595. };
  1596. BScroll.prototype._checkSnapLoop = function () {
  1597. var snap = this.options.snap;
  1598. if (!snap.loop || !this.pages || !this.pages.length) {
  1599. return;
  1600. }
  1601. if (this.pages.length > 1) {
  1602. snap._loopX = true;
  1603. }
  1604. if (this.pages[0] && this.pages[0].length > 1) {
  1605. snap._loopY = true;
  1606. }
  1607. if (snap._loopX && snap._loopY) {
  1608. warn('Loop does not support two direction at the same time.');
  1609. }
  1610. };
  1611. BScroll.prototype._nearestSnap = function (x, y) {
  1612. if (!this.pages.length) {
  1613. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1614. }
  1615. var i = 0;
  1616. // Check if we exceeded the snap threshold
  1617. if (Math.abs(x - this.absStartX) <= this.snapThresholdX && Math.abs(y - this.absStartY) <= this.snapThresholdY) {
  1618. return this.currentPage;
  1619. }
  1620. if (x > this.minScrollX) {
  1621. x = this.minScrollX;
  1622. } else if (x < this.maxScrollX) {
  1623. x = this.maxScrollX;
  1624. }
  1625. if (y > this.minScrollY) {
  1626. y = this.minScrollY;
  1627. } else if (y < this.maxScrollY) {
  1628. y = this.maxScrollY;
  1629. }
  1630. var l = this.pages.length;
  1631. for (; i < l; i++) {
  1632. if (x >= this.pages[i][0].cx) {
  1633. x = this.pages[i][0].x;
  1634. break;
  1635. }
  1636. }
  1637. l = this.pages[i].length;
  1638. var m = 0;
  1639. for (; m < l; m++) {
  1640. if (y >= this.pages[0][m].cy) {
  1641. y = this.pages[0][m].y;
  1642. break;
  1643. }
  1644. }
  1645. if (i === this.currentPage.pageX) {
  1646. i += this.directionX;
  1647. if (i < 0) {
  1648. i = 0;
  1649. } else if (i >= this.pages.length) {
  1650. i = this.pages.length - 1;
  1651. }
  1652. x = this.pages[i][0].x;
  1653. }
  1654. if (m === this.currentPage.pageY) {
  1655. m += this.directionY;
  1656. if (m < 0) {
  1657. m = 0;
  1658. } else if (m >= this.pages[0].length) {
  1659. m = this.pages[0].length - 1;
  1660. }
  1661. y = this.pages[0][m].y;
  1662. }
  1663. return {
  1664. x: x,
  1665. y: y,
  1666. pageX: i,
  1667. pageY: m
  1668. };
  1669. };
  1670. BScroll.prototype._goToPage = function (x) {
  1671. var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  1672. var time = arguments[2];
  1673. var easing = arguments[3];
  1674. var isSilent = arguments[4];
  1675. var snap = this.options.snap;
  1676. if (!snap || !this.pages || !this.pages.length) {
  1677. return;
  1678. }
  1679. easing = easing || snap.easing || ease.bounce;
  1680. if (x >= this.pages.length) {
  1681. x = this.pages.length - 1;
  1682. } else if (x < 0) {
  1683. x = 0;
  1684. }
  1685. if (!this.pages[x]) {
  1686. return;
  1687. }
  1688. if (y >= this.pages[x].length) {
  1689. y = this.pages[x].length - 1;
  1690. } else if (y < 0) {
  1691. y = 0;
  1692. }
  1693. var posX = this.pages[x][y].x;
  1694. var posY = this.pages[x][y].y;
  1695. time = time === undefined ? snap.speed || Math.max(Math.max(Math.min(Math.abs(posX - this.x), 1000), Math.min(Math.abs(posY - this.y), 1000)), 300) : time;
  1696. this.currentPage = {
  1697. x: posX,
  1698. y: posY,
  1699. pageX: x,
  1700. pageY: y
  1701. };
  1702. this.scrollTo(posX, posY, time, easing, isSilent);
  1703. };
  1704. BScroll.prototype.goToPage = function (x, y, time, easing) {
  1705. var snap = this.options.snap;
  1706. if (!snap || !this.pages || !this.pages.length) {
  1707. return;
  1708. }
  1709. if (snap.loop) {
  1710. var len = void 0;
  1711. if (snap._loopX) {
  1712. len = this.pages.length - 2;
  1713. if (x >= len) {
  1714. x = len - 1;
  1715. } else if (x < 0) {
  1716. x = 0;
  1717. }
  1718. x += 1;
  1719. } else {
  1720. len = this.pages[0].length - 2;
  1721. if (y >= len) {
  1722. y = len - 1;
  1723. } else if (y < 0) {
  1724. y = 0;
  1725. }
  1726. y += 1;
  1727. }
  1728. }
  1729. this._goToPage(x, y, time, easing);
  1730. };
  1731. BScroll.prototype.next = function (time, easing) {
  1732. var snap = this.options.snap;
  1733. if (!snap) {
  1734. return;
  1735. }
  1736. var x = this.currentPage.pageX;
  1737. var y = this.currentPage.pageY;
  1738. x++;
  1739. if (x >= this.pages.length && this.hasVerticalScroll) {
  1740. x = 0;
  1741. y++;
  1742. }
  1743. this._goToPage(x, y, time, easing);
  1744. };
  1745. BScroll.prototype.prev = function (time, easing) {
  1746. var snap = this.options.snap;
  1747. if (!snap) {
  1748. return;
  1749. }
  1750. var x = this.currentPage.pageX;
  1751. var y = this.currentPage.pageY;
  1752. x--;
  1753. if (x < 0 && this.hasVerticalScroll) {
  1754. x = 0;
  1755. y--;
  1756. }
  1757. this._goToPage(x, y, time, easing);
  1758. };
  1759. BScroll.prototype.getCurrentPage = function () {
  1760. var snap = this.options.snap;
  1761. if (!snap) {
  1762. return null;
  1763. }
  1764. if (snap.loop) {
  1765. var currentPage = void 0;
  1766. if (snap._loopX) {
  1767. currentPage = extend({}, this.currentPage, {
  1768. pageX: this.currentPage.pageX - 1
  1769. });
  1770. } else {
  1771. currentPage = extend({}, this.currentPage, {
  1772. pageY: this.currentPage.pageY - 1
  1773. });
  1774. }
  1775. return currentPage;
  1776. }
  1777. return this.currentPage;
  1778. };
  1779. }
  1780. function wheelMixin(BScroll) {
  1781. BScroll.prototype.wheelTo = function () {
  1782. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1783. if (this.options.wheel) {
  1784. var y = -index * this.itemHeight;
  1785. this.scrollTo(0, y);
  1786. }
  1787. };
  1788. BScroll.prototype.getSelectedIndex = function () {
  1789. return this.options.wheel && this.selectedIndex;
  1790. };
  1791. BScroll.prototype._initWheel = function () {
  1792. var wheel = this.options.wheel;
  1793. if (!wheel.wheelWrapperClass) {
  1794. wheel.wheelWrapperClass = 'wheel-scroll';
  1795. }
  1796. if (!wheel.wheelItemClass) {
  1797. wheel.wheelItemClass = 'wheel-item';
  1798. }
  1799. if (!wheel.wheelDisabledItemClass) {
  1800. wheel.wheelDisabledItemClass = 'wheel-disabled-item';
  1801. }
  1802. if (wheel.selectedIndex === undefined) {
  1803. wheel.selectedIndex = 0;
  1804. }
  1805. };
  1806. BScroll.prototype._findNearestValidWheel = function (y) {
  1807. y = y > 0 ? 0 : y < this.maxScrollY ? this.maxScrollY : y;
  1808. var wheel = this.options.wheel;
  1809. var currentIndex = Math.abs(Math.round(-y / this.itemHeight));
  1810. var cacheIndex = currentIndex;
  1811. var items = this.items;
  1812. // Impersonation web native select
  1813. // first, check whether there is a enable item whose index is smaller than currentIndex
  1814. // then, check whether there is a enable item whose index is bigger than currentIndex
  1815. // otherwise, there are all disabled items, just keep currentIndex unchange
  1816. while (currentIndex >= 0) {
  1817. if (items[currentIndex].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
  1818. break;
  1819. }
  1820. currentIndex--;
  1821. }
  1822. if (currentIndex < 0) {
  1823. currentIndex = cacheIndex;
  1824. while (currentIndex <= items.length - 1) {
  1825. if (items[currentIndex].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
  1826. break;
  1827. }
  1828. currentIndex++;
  1829. }
  1830. }
  1831. // keep it unchange when all the items are disabled
  1832. if (currentIndex === items.length) {
  1833. currentIndex = cacheIndex;
  1834. }
  1835. // when all the items are disabled, this.selectedIndex should always be -1
  1836. return {
  1837. index: this.wheelItemsAllDisabled ? -1 : currentIndex,
  1838. y: -currentIndex * this.itemHeight
  1839. };
  1840. };
  1841. BScroll.prototype._checkWheelAllDisabled = function () {
  1842. var wheel = this.options.wheel;
  1843. var items = this.items;
  1844. this.wheelItemsAllDisabled = true;
  1845. for (var i = 0; i < items.length; i++) {
  1846. if (items[i].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
  1847. this.wheelItemsAllDisabled = false;
  1848. break;
  1849. }
  1850. }
  1851. };
  1852. }
  1853. var INDICATOR_MIN_LEN = 8;
  1854. function scrollbarMixin(BScroll) {
  1855. BScroll.prototype._initScrollbar = function () {
  1856. var _this = this;
  1857. var _options$scrollbar = this.options.scrollbar,
  1858. _options$scrollbar$fa = _options$scrollbar.fade,
  1859. fade = _options$scrollbar$fa === undefined ? true : _options$scrollbar$fa,
  1860. _options$scrollbar$in = _options$scrollbar.interactive,
  1861. interactive = _options$scrollbar$in === undefined ? false : _options$scrollbar$in;
  1862. this.indicators = [];
  1863. var indicator = void 0;
  1864. if (this.options.scrollX) {
  1865. indicator = {
  1866. el: createScrollbar('horizontal'),
  1867. direction: 'horizontal',
  1868. fade: fade,
  1869. interactive: interactive
  1870. };
  1871. this._insertScrollBar(indicator.el);
  1872. this.indicators.push(new Indicator(this, indicator));
  1873. }
  1874. if (this.options.scrollY) {
  1875. indicator = {
  1876. el: createScrollbar('vertical'),
  1877. direction: 'vertical',
  1878. fade: fade,
  1879. interactive: interactive
  1880. };
  1881. this._insertScrollBar(indicator.el);
  1882. this.indicators.push(new Indicator(this, indicator));
  1883. }
  1884. this.on('refresh', function () {
  1885. for (var i = 0; i < _this.indicators.length; i++) {
  1886. _this.indicators[i].refresh();
  1887. }
  1888. });
  1889. if (fade) {
  1890. this.on('scrollEnd', function () {
  1891. for (var i = 0; i < _this.indicators.length; i++) {
  1892. _this.indicators[i].fade();
  1893. }
  1894. });
  1895. this.on('scrollCancel', function () {
  1896. for (var i = 0; i < _this.indicators.length; i++) {
  1897. _this.indicators[i].fade();
  1898. }
  1899. });
  1900. this.on('scrollStart', function () {
  1901. for (var i = 0; i < _this.indicators.length; i++) {
  1902. _this.indicators[i].fade(true);
  1903. }
  1904. });
  1905. this.on('beforeScrollStart', function () {
  1906. for (var i = 0; i < _this.indicators.length; i++) {
  1907. _this.indicators[i].fade(true, true);
  1908. }
  1909. });
  1910. }
  1911. this.on('destroy', function () {
  1912. _this._removeScrollBars();
  1913. });
  1914. };
  1915. BScroll.prototype._insertScrollBar = function (scrollbar) {
  1916. this.wrapper.appendChild(scrollbar);
  1917. };
  1918. BScroll.prototype._removeScrollBars = function () {
  1919. for (var i = 0; i < this.indicators.length; i++) {
  1920. this.indicators[i].destroy();
  1921. }
  1922. };
  1923. }
  1924. function createScrollbar(direction) {
  1925. var scrollbar = document.createElement('div');
  1926. var indicator = document.createElement('div');
  1927. scrollbar.style.cssText = 'position:absolute;z-index:9999;pointerEvents:none';
  1928. indicator.style.cssText = '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;';
  1929. indicator.className = 'bscroll-indicator';
  1930. if (direction === 'horizontal') {
  1931. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1932. indicator.style.height = '100%';
  1933. scrollbar.className = 'bscroll-horizontal-scrollbar';
  1934. } else {
  1935. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1936. indicator.style.width = '100%';
  1937. scrollbar.className = 'bscroll-vertical-scrollbar';
  1938. }
  1939. scrollbar.style.cssText += ';overflow:hidden';
  1940. scrollbar.appendChild(indicator);
  1941. return scrollbar;
  1942. }
  1943. function Indicator(scroller, options) {
  1944. this.wrapper = options.el;
  1945. this.wrapperStyle = this.wrapper.style;
  1946. this.indicator = this.wrapper.children[0];
  1947. this.indicatorStyle = this.indicator.style;
  1948. this.scroller = scroller;
  1949. this.direction = options.direction;
  1950. if (options.fade) {
  1951. this.visible = 0;
  1952. this.wrapperStyle.opacity = '0';
  1953. } else {
  1954. this.visible = 1;
  1955. }
  1956. this.sizeRatioX = 1;
  1957. this.sizeRatioY = 1;
  1958. this.maxPosX = 0;
  1959. this.maxPosY = 0;
  1960. this.x = 0;
  1961. this.y = 0;
  1962. if (options.interactive) {
  1963. this._addDOMEvents();
  1964. }
  1965. }
  1966. Indicator.prototype.handleEvent = function (e) {
  1967. switch (e.type) {
  1968. case 'touchstart':
  1969. case 'mousedown':
  1970. this._start(e);
  1971. break;
  1972. case 'touchmove':
  1973. case 'mousemove':
  1974. this._move(e);
  1975. break;
  1976. case 'touchend':
  1977. case 'mouseup':
  1978. case 'touchcancel':
  1979. case 'mousecancel':
  1980. this._end(e);
  1981. break;
  1982. }
  1983. };
  1984. Indicator.prototype.refresh = function () {
  1985. if (this._shouldShow()) {
  1986. this.transitionTime();
  1987. this._calculate();
  1988. this.updatePosition();
  1989. }
  1990. };
  1991. Indicator.prototype.fade = function (visible, hold) {
  1992. var _this2 = this;
  1993. if (hold && !this.visible) {
  1994. return;
  1995. }
  1996. var time = visible ? 250 : 500;
  1997. visible = visible ? '1' : '0';
  1998. this.wrapperStyle[style.transitionDuration] = time + 'ms';
  1999. clearTimeout(this.fadeTimeout);
  2000. this.fadeTimeout = setTimeout(function () {
  2001. _this2.wrapperStyle.opacity = visible;
  2002. _this2.visible = +visible;
  2003. }, 0);
  2004. };
  2005. Indicator.prototype.updatePosition = function () {
  2006. if (this.direction === 'vertical') {
  2007. var y = Math.round(this.sizeRatioY * this.scroller.y);
  2008. if (y < 0) {
  2009. this.transitionTime(500);
  2010. var height = Math.max(this.indicatorHeight + y * 3, INDICATOR_MIN_LEN);
  2011. this.indicatorStyle.height = height + 'px';
  2012. y = 0;
  2013. } else if (y > this.maxPosY) {
  2014. this.transitionTime(500);
  2015. var _height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, INDICATOR_MIN_LEN);
  2016. this.indicatorStyle.height = _height + 'px';
  2017. y = this.maxPosY + this.indicatorHeight - _height;
  2018. } else {
  2019. this.indicatorStyle.height = this.indicatorHeight + 'px';
  2020. }
  2021. this.y = y;
  2022. if (this.scroller.options.useTransform) {
  2023. this.indicatorStyle[style.transform] = 'translateY(' + y + 'px)' + this.scroller.translateZ;
  2024. } else {
  2025. this.indicatorStyle.top = y + 'px';
  2026. }
  2027. } else {
  2028. var x = Math.round(this.sizeRatioX * this.scroller.x);
  2029. if (x < 0) {
  2030. this.transitionTime(500);
  2031. var width = Math.max(this.indicatorWidth + x * 3, INDICATOR_MIN_LEN);
  2032. this.indicatorStyle.width = width + 'px';
  2033. x = 0;
  2034. } else if (x > this.maxPosX) {
  2035. this.transitionTime(500);
  2036. var _width = Math.max(this.indicatorWidth - (x - this.maxPosX) * 3, INDICATOR_MIN_LEN);
  2037. this.indicatorStyle.width = _width + 'px';
  2038. x = this.maxPosX + this.indicatorWidth - _width;
  2039. } else {
  2040. this.indicatorStyle.width = this.indicatorWidth + 'px';
  2041. }
  2042. this.x = x;
  2043. if (this.scroller.options.useTransform) {
  2044. this.indicatorStyle[style.transform] = 'translateX(' + x + 'px)' + this.scroller.translateZ;
  2045. } else {
  2046. this.indicatorStyle.left = x + 'px';
  2047. }
  2048. }
  2049. };
  2050. Indicator.prototype.transitionTime = function () {
  2051. var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  2052. this.indicatorStyle[style.transitionDuration] = time + 'ms';
  2053. };
  2054. Indicator.prototype.transitionTimingFunction = function (easing) {
  2055. this.indicatorStyle[style.transitionTimingFunction] = easing;
  2056. };
  2057. Indicator.prototype.destroy = function () {
  2058. this._removeDOMEvents();
  2059. this.wrapper.parentNode.removeChild(this.wrapper);
  2060. };
  2061. Indicator.prototype._start = function (e) {
  2062. var point = e.touches ? e.touches[0] : e;
  2063. e.preventDefault();
  2064. e.stopPropagation();
  2065. this.transitionTime();
  2066. this.initiated = true;
  2067. this.moved = false;
  2068. this.lastPointX = point.pageX;
  2069. this.lastPointY = point.pageY;
  2070. this.startTime = getNow();
  2071. this._handleMoveEvents(addEvent);
  2072. this.scroller.trigger('beforeScrollStart');
  2073. };
  2074. Indicator.prototype._move = function (e) {
  2075. var point = e.touches ? e.touches[0] : e;
  2076. e.preventDefault();
  2077. e.stopPropagation();
  2078. if (!this.moved) {
  2079. this.scroller.trigger('scrollStart');
  2080. }
  2081. this.moved = true;
  2082. var deltaX = point.pageX - this.lastPointX;
  2083. this.lastPointX = point.pageX;
  2084. var deltaY = point.pageY - this.lastPointY;
  2085. this.lastPointY = point.pageY;
  2086. var newX = this.x + deltaX;
  2087. var newY = this.y + deltaY;
  2088. this._pos(newX, newY);
  2089. };
  2090. Indicator.prototype._end = function (e) {
  2091. if (!this.initiated) {
  2092. return;
  2093. }
  2094. this.initiated = false;
  2095. e.preventDefault();
  2096. e.stopPropagation();
  2097. this._handleMoveEvents(removeEvent);
  2098. var snapOption = this.scroller.options.snap;
  2099. if (snapOption) {
  2100. var speed = snapOption.speed,
  2101. _snapOption$easing = snapOption.easing,
  2102. easing = _snapOption$easing === undefined ? ease.bounce : _snapOption$easing;
  2103. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  2104. var time = speed || Math.max(Math.max(Math.min(Math.abs(this.scroller.x - snap.x), 1000), Math.min(Math.abs(this.scroller.y - snap.y), 1000)), 300);
  2105. if (this.scroller.x !== snap.x || this.scroller.y !== snap.y) {
  2106. this.scroller.directionX = 0;
  2107. this.scroller.directionY = 0;
  2108. this.scroller.currentPage = snap;
  2109. this.scroller.scrollTo(snap.x, snap.y, time, easing);
  2110. }
  2111. }
  2112. if (this.moved) {
  2113. this.scroller.trigger('scrollEnd', {
  2114. x: this.scroller.x,
  2115. y: this.scroller.y
  2116. });
  2117. }
  2118. };
  2119. Indicator.prototype._pos = function (x, y) {
  2120. if (x < 0) {
  2121. x = 0;
  2122. } else if (x > this.maxPosX) {
  2123. x = this.maxPosX;
  2124. }
  2125. if (y < 0) {
  2126. y = 0;
  2127. } else if (y > this.maxPosY) {
  2128. y = this.maxPosY;
  2129. }
  2130. x = Math.round(x / this.sizeRatioX);
  2131. y = Math.round(y / this.sizeRatioY);
  2132. this.scroller.scrollTo(x, y);
  2133. this.scroller.trigger('scroll', {
  2134. x: this.scroller.x,
  2135. y: this.scroller.y
  2136. });
  2137. };
  2138. Indicator.prototype._shouldShow = function () {
  2139. if (this.direction === 'vertical' && this.scroller.hasVerticalScroll || this.direction === 'horizontal' && this.scroller.hasHorizontalScroll) {
  2140. this.wrapper.style.display = '';
  2141. return true;
  2142. }
  2143. this.wrapper.style.display = 'none';
  2144. return false;
  2145. };
  2146. Indicator.prototype._calculate = function () {
  2147. if (this.direction === 'vertical') {
  2148. var wrapperHeight = this.wrapper.clientHeight;
  2149. this.indicatorHeight = Math.max(Math.round(wrapperHeight * wrapperHeight / (this.scroller.scrollerHeight || wrapperHeight || 1)), INDICATOR_MIN_LEN);
  2150. this.indicatorStyle.height = this.indicatorHeight + 'px';
  2151. this.maxPosY = wrapperHeight - this.indicatorHeight;
  2152. this.sizeRatioY = this.maxPosY / this.scroller.maxScrollY;
  2153. } else {
  2154. var wrapperWidth = this.wrapper.clientWidth;
  2155. this.indicatorWidth = Math.max(Math.round(wrapperWidth * wrapperWidth / (this.scroller.scrollerWidth || wrapperWidth || 1)), INDICATOR_MIN_LEN);
  2156. this.indicatorStyle.width = this.indicatorWidth + 'px';
  2157. this.maxPosX = wrapperWidth - this.indicatorWidth;
  2158. this.sizeRatioX = this.maxPosX / this.scroller.maxScrollX;
  2159. }
  2160. };
  2161. Indicator.prototype._addDOMEvents = function () {
  2162. var eventOperation = addEvent;
  2163. this._handleDOMEvents(eventOperation);
  2164. };
  2165. Indicator.prototype._removeDOMEvents = function () {
  2166. var eventOperation = removeEvent;
  2167. this._handleDOMEvents(eventOperation);
  2168. this._handleMoveEvents(eventOperation);
  2169. };
  2170. Indicator.prototype._handleMoveEvents = function (eventOperation) {
  2171. if (!this.scroller.options.disableTouch) {
  2172. eventOperation(window, 'touchmove', this);
  2173. }
  2174. if (!this.scroller.options.disableMouse) {
  2175. eventOperation(window, 'mousemove', this);
  2176. }
  2177. };
  2178. Indicator.prototype._handleDOMEvents = function (eventOperation) {
  2179. if (!this.scroller.options.disableTouch) {
  2180. eventOperation(this.indicator, 'touchstart', this);
  2181. eventOperation(window, 'touchend', this);
  2182. }
  2183. if (!this.scroller.options.disableMouse) {
  2184. eventOperation(this.indicator, 'mousedown', this);
  2185. eventOperation(window, 'mouseup', this);
  2186. }
  2187. };
  2188. function pullDownMixin(BScroll) {
  2189. BScroll.prototype._initPullDown = function () {
  2190. // must watch scroll in real time
  2191. this.options.probeType = PROBE_REALTIME;
  2192. };
  2193. BScroll.prototype._checkPullDown = function () {
  2194. var _options$pullDownRefr = this.options.pullDownRefresh,
  2195. _options$pullDownRefr2 = _options$pullDownRefr.threshold,
  2196. threshold = _options$pullDownRefr2 === undefined ? 90 : _options$pullDownRefr2,
  2197. _options$pullDownRefr3 = _options$pullDownRefr.stop,
  2198. stop = _options$pullDownRefr3 === undefined ? 40 : _options$pullDownRefr3;
  2199. // check if a real pull down action
  2200. if (this.directionY !== DIRECTION_DOWN || this.y < threshold) {
  2201. return false;
  2202. }
  2203. if (!this.pulling) {
  2204. this.pulling = true;
  2205. this.trigger('pullingDown');
  2206. }
  2207. this.scrollTo(this.x, stop, this.options.bounceTime, ease.bounce);
  2208. return this.pulling;
  2209. };
  2210. BScroll.prototype.finishPullDown = function () {
  2211. this.pulling = false;
  2212. this.resetPosition(this.options.bounceTime, ease.bounce);
  2213. };
  2214. BScroll.prototype.openPullDown = function () {
  2215. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  2216. this.options.pullDownRefresh = config;
  2217. this._initPullDown();
  2218. };
  2219. BScroll.prototype.closePullDown = function () {
  2220. this.options.pullDownRefresh = false;
  2221. };
  2222. BScroll.prototype.autoPullDownRefresh = function () {
  2223. var _options$pullDownRefr4 = this.options.pullDownRefresh,
  2224. _options$pullDownRefr5 = _options$pullDownRefr4.threshold,
  2225. threshold = _options$pullDownRefr5 === undefined ? 90 : _options$pullDownRefr5,
  2226. _options$pullDownRefr6 = _options$pullDownRefr4.stop,
  2227. stop = _options$pullDownRefr6 === undefined ? 40 : _options$pullDownRefr6;
  2228. if (this.pulling) {
  2229. return;
  2230. }
  2231. this.pulling = true;
  2232. this.scrollTo(this.x, threshold);
  2233. this.trigger('pullingDown');
  2234. this.scrollTo(this.x, stop, this.options.bounceTime, ease.bounce);
  2235. };
  2236. }
  2237. function pullUpMixin(BScroll) {
  2238. BScroll.prototype._initPullUp = function () {
  2239. // must watch scroll in real time
  2240. this.options.probeType = PROBE_REALTIME;
  2241. this.pullupWatching = false;
  2242. this._watchPullUp();
  2243. };
  2244. BScroll.prototype._watchPullUp = function () {
  2245. if (this.pullupWatching) {
  2246. return;
  2247. }
  2248. this.pullupWatching = true;
  2249. this.on('scroll', this._checkToEnd);
  2250. };
  2251. BScroll.prototype._checkToEnd = function (pos) {
  2252. var _this = this;
  2253. var _options$pullUpLoad$t = this.options.pullUpLoad.threshold,
  2254. threshold = _options$pullUpLoad$t === undefined ? 0 : _options$pullUpLoad$t;
  2255. if (this.movingDirectionY === DIRECTION_UP && pos.y <= this.maxScrollY + threshold) {
  2256. // reset pullupWatching status after scroll end.
  2257. this.once('scrollEnd', function () {
  2258. _this.pullupWatching = false;
  2259. });
  2260. this.trigger('pullingUp');
  2261. this.off('scroll', this._checkToEnd);
  2262. }
  2263. };
  2264. BScroll.prototype.finishPullUp = function () {
  2265. var _this2 = this;
  2266. if (this.pullupWatching) {
  2267. this.once('scrollEnd', function () {
  2268. _this2._watchPullUp();
  2269. });
  2270. } else {
  2271. this._watchPullUp();
  2272. }
  2273. };
  2274. BScroll.prototype.openPullUp = function () {
  2275. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  2276. this.options.pullUpLoad = config;
  2277. this._initPullUp();
  2278. };
  2279. BScroll.prototype.closePullUp = function () {
  2280. this.options.pullUpLoad = false;
  2281. if (!this.pullupWatching) {
  2282. return;
  2283. }
  2284. this.pullupWatching = false;
  2285. this.off('scroll', this._checkToEnd);
  2286. };
  2287. }
  2288. function mouseWheelMixin(BScroll) {
  2289. BScroll.prototype._initMouseWheel = function () {
  2290. var _this = this;
  2291. this._handleMouseWheelEvent(addEvent);
  2292. this.on('destroy', function () {
  2293. clearTimeout(_this.mouseWheelTimer);
  2294. clearTimeout(_this.mouseWheelEndTimer);
  2295. _this._handleMouseWheelEvent(removeEvent);
  2296. });
  2297. this.firstWheelOpreation = true;
  2298. };
  2299. BScroll.prototype._handleMouseWheelEvent = function (eventOperation) {
  2300. eventOperation(this.wrapper, 'wheel', this);
  2301. eventOperation(this.wrapper, 'mousewheel', this);
  2302. eventOperation(this.wrapper, 'DOMMouseScroll', this);
  2303. };
  2304. BScroll.prototype._onMouseWheel = function (e) {
  2305. var _this2 = this;
  2306. if (!this.enabled) {
  2307. return;
  2308. }
  2309. e.preventDefault();
  2310. if (this.options.stopPropagation) {
  2311. e.stopPropagation();
  2312. }
  2313. if (this.firstWheelOpreation) {
  2314. this.trigger('scrollStart');
  2315. }
  2316. this.firstWheelOpreation = false;
  2317. var _options$mouseWheel = this.options.mouseWheel,
  2318. _options$mouseWheel$s = _options$mouseWheel.speed,
  2319. speed = _options$mouseWheel$s === undefined ? 20 : _options$mouseWheel$s,
  2320. _options$mouseWheel$i = _options$mouseWheel.invert,
  2321. invert = _options$mouseWheel$i === undefined ? false : _options$mouseWheel$i,
  2322. _options$mouseWheel$e = _options$mouseWheel.easeTime,
  2323. easeTime = _options$mouseWheel$e === undefined ? 300 : _options$mouseWheel$e;
  2324. clearTimeout(this.mouseWheelTimer);
  2325. this.mouseWheelTimer = setTimeout(function () {
  2326. if (!_this2.options.snap && !easeTime) {
  2327. _this2.trigger('scrollEnd', {
  2328. x: _this2.x,
  2329. y: _this2.y
  2330. });
  2331. }
  2332. _this2.firstWheelOpreation = true;
  2333. }, 400);
  2334. var wheelDeltaX = void 0;
  2335. var wheelDeltaY = void 0;
  2336. switch (true) {
  2337. case 'deltaX' in e:
  2338. if (e.deltaMode === 1) {
  2339. wheelDeltaX = -e.deltaX * speed;
  2340. wheelDeltaY = -e.deltaY * speed;
  2341. } else {
  2342. wheelDeltaX = -e.deltaX;
  2343. wheelDeltaY = -e.deltaY;
  2344. }
  2345. break;
  2346. case 'wheelDeltaX' in e:
  2347. wheelDeltaX = e.wheelDeltaX / 120 * speed;
  2348. wheelDeltaY = e.wheelDeltaY / 120 * speed;
  2349. break;
  2350. case 'wheelDelta' in e:
  2351. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * speed;
  2352. break;
  2353. case 'detail' in e:
  2354. wheelDeltaX = wheelDeltaY = -e.detail / 3 * speed;
  2355. break;
  2356. default:
  2357. return;
  2358. }
  2359. var direction = invert ? -1 : 1;
  2360. wheelDeltaX *= direction;
  2361. wheelDeltaY *= direction;
  2362. if (!this.hasVerticalScroll) {
  2363. wheelDeltaX = wheelDeltaY;
  2364. wheelDeltaY = 0;
  2365. }
  2366. var newX = void 0;
  2367. var newY = void 0;
  2368. if (this.options.snap) {
  2369. newX = this.currentPage.pageX;
  2370. newY = this.currentPage.pageY;
  2371. if (wheelDeltaX > 0) {
  2372. newX--;
  2373. } else if (wheelDeltaX < 0) {
  2374. newX++;
  2375. }
  2376. if (wheelDeltaY > 0) {
  2377. newY--;
  2378. } else if (wheelDeltaY < 0) {
  2379. newY++;
  2380. }
  2381. this._goToPage(newX, newY);
  2382. return;
  2383. }
  2384. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  2385. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  2386. this.movingDirectionX = this.directionX = wheelDeltaX > 0 ? -1 : wheelDeltaX < 0 ? 1 : 0;
  2387. this.movingDirectionY = this.directionY = wheelDeltaY > 0 ? -1 : wheelDeltaY < 0 ? 1 : 0;
  2388. if (newX > this.minScrollX) {
  2389. newX = this.minScrollX;
  2390. } else if (newX < this.maxScrollX) {
  2391. newX = this.maxScrollX;
  2392. }
  2393. if (newY > this.minScrollY) {
  2394. newY = this.minScrollY;
  2395. } else if (newY < this.maxScrollY) {
  2396. newY = this.maxScrollY;
  2397. }
  2398. var needTriggerEnd = this.y === newY;
  2399. this.scrollTo(newX, newY, easeTime, ease.swipe);
  2400. this.trigger('scroll', {
  2401. x: this.x,
  2402. y: this.y
  2403. });
  2404. clearTimeout(this.mouseWheelEndTimer);
  2405. if (needTriggerEnd) {
  2406. this.mouseWheelEndTimer = setTimeout(function () {
  2407. _this2.trigger('scrollEnd', {
  2408. x: _this2.x,
  2409. y: _this2.y
  2410. });
  2411. }, easeTime);
  2412. }
  2413. };
  2414. }
  2415. function zoomMixin(BScroll) {
  2416. BScroll.prototype._initZoom = function () {
  2417. var _options$zoom = this.options.zoom,
  2418. _options$zoom$start = _options$zoom.start,
  2419. start = _options$zoom$start === undefined ? 1 : _options$zoom$start,
  2420. _options$zoom$min = _options$zoom.min,
  2421. min = _options$zoom$min === undefined ? 1 : _options$zoom$min,
  2422. _options$zoom$max = _options$zoom.max,
  2423. max = _options$zoom$max === undefined ? 4 : _options$zoom$max;
  2424. this.scale = Math.min(Math.max(start, min), max);
  2425. this.setScale(this.scale);
  2426. this.scrollerStyle[style.transformOrigin] = '0 0';
  2427. };
  2428. BScroll.prototype._zoomTo = function (scale, originX, originY, startScale) {
  2429. this.scaled = true;
  2430. var lastScale = scale / (startScale || this.scale);
  2431. this.setScale(scale);
  2432. this.refresh();
  2433. var newX = Math.round(this.startX - (originX - this.relativeX) * (lastScale - 1));
  2434. var newY = Math.round(this.startY - (originY - this.relativeY) * (lastScale - 1));
  2435. if (newX > this.minScrollX) {
  2436. newX = this.minScrollX;
  2437. } else if (newX < this.maxScrollX) {
  2438. newX = this.maxScrollX;
  2439. }
  2440. if (newY > this.minScrollY) {
  2441. newY = this.minScrollY;
  2442. } else if (newY < this.maxScrollY) {
  2443. newY = this.maxScrollY;
  2444. }
  2445. if (this.x !== newX || this.y !== newY) {
  2446. this.scrollTo(newX, newY, this.options.bounceTime);
  2447. }
  2448. this.scaled = false;
  2449. };
  2450. BScroll.prototype.zoomTo = function (scale, x, y) {
  2451. var _offsetToBody = offsetToBody(this.wrapper),
  2452. left = _offsetToBody.left,
  2453. top = _offsetToBody.top;
  2454. var originX = x + left - this.x;
  2455. var originY = y + top - this.y;
  2456. this._zoomTo(scale, originX, originY);
  2457. };
  2458. BScroll.prototype._zoomStart = function (e) {
  2459. var firstFinger = e.touches[0];
  2460. var secondFinger = e.touches[1];
  2461. var deltaX = Math.abs(firstFinger.pageX - secondFinger.pageX);
  2462. var deltaY = Math.abs(firstFinger.pageY - secondFinger.pageY);
  2463. this.startDistance = getDistance(deltaX, deltaY);
  2464. this.startScale = this.scale;
  2465. var _offsetToBody2 = offsetToBody(this.wrapper),
  2466. left = _offsetToBody2.left,
  2467. top = _offsetToBody2.top;
  2468. this.originX = Math.abs(firstFinger.pageX + secondFinger.pageX) / 2 + left - this.x;
  2469. this.originY = Math.abs(firstFinger.pageY + secondFinger.pageY) / 2 + top - this.y;
  2470. this.trigger('zoomStart');
  2471. };
  2472. BScroll.prototype._zoom = function (e) {
  2473. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  2474. return;
  2475. }
  2476. if (this.options.preventDefault) {
  2477. e.preventDefault();
  2478. }
  2479. if (this.options.stopPropagation) {
  2480. e.stopPropagation();
  2481. }
  2482. var firstFinger = e.touches[0];
  2483. var secondFinger = e.touches[1];
  2484. var deltaX = Math.abs(firstFinger.pageX - secondFinger.pageX);
  2485. var deltaY = Math.abs(firstFinger.pageY - secondFinger.pageY);
  2486. var distance = getDistance(deltaX, deltaY);
  2487. var scale = distance / this.startDistance * this.startScale;
  2488. this.scaled = true;
  2489. var _options$zoom2 = this.options.zoom,
  2490. _options$zoom2$min = _options$zoom2.min,
  2491. min = _options$zoom2$min === undefined ? 1 : _options$zoom2$min,
  2492. _options$zoom2$max = _options$zoom2.max,
  2493. max = _options$zoom2$max === undefined ? 4 : _options$zoom2$max;
  2494. if (scale < min) {
  2495. scale = 0.5 * min * Math.pow(2.0, scale / min);
  2496. } else if (scale > max) {
  2497. scale = 2.0 * max * Math.pow(0.5, max / scale);
  2498. }
  2499. var lastScale = scale / this.startScale;
  2500. var x = this.startX - (this.originX - this.relativeX) * (lastScale - 1);
  2501. var y = this.startY - (this.originY - this.relativeY) * (lastScale - 1);
  2502. this.setScale(scale);
  2503. this.scrollTo(x, y, 0);
  2504. };
  2505. BScroll.prototype._zoomEnd = function (e) {
  2506. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  2507. return;
  2508. }
  2509. if (this.options.preventDefault) {
  2510. e.preventDefault();
  2511. }
  2512. if (this.options.stopPropagation) {
  2513. e.stopPropagation();
  2514. }
  2515. this.isInTransition = false;
  2516. this.isAnimating = false;
  2517. this.initiated = 0;
  2518. var _options$zoom3 = this.options.zoom,
  2519. _options$zoom3$min = _options$zoom3.min,
  2520. min = _options$zoom3$min === undefined ? 1 : _options$zoom3$min,
  2521. _options$zoom3$max = _options$zoom3.max,
  2522. max = _options$zoom3$max === undefined ? 4 : _options$zoom3$max;
  2523. var scale = this.scale > max ? max : this.scale < min ? min : this.scale;
  2524. this._zoomTo(scale, this.originX, this.originY, this.startScale);
  2525. this.trigger('zoomEnd');
  2526. };
  2527. }
  2528. // import { ease } from '../util/ease'
  2529. // Number of items to instantiate beyond current view in the scroll direction.
  2530. var RUNWAY_ITEMS = 30;
  2531. // Number of items to instantiate beyond current view in the opposite direction.
  2532. var RUNWAY_ITEMS_OPPOSITE = 10;
  2533. // The animation interval (in ms) for fading in content from tombstones.
  2534. var ANIMATION_DURATION_MS = 200;
  2535. // The number of pixels of default additional length to allow scrolling to.
  2536. var DEFAULT_SCROLL_RUNWAY = 2000;
  2537. function infiniteMixin(BScroll) {
  2538. BScroll.prototype._initInfinite = function () {
  2539. this.options.probeType = 3;
  2540. this.maxScrollY = -DEFAULT_SCROLL_RUNWAY;
  2541. this.infiniteScroller = new InfiniteScroller(this, this.options.infinity);
  2542. };
  2543. }
  2544. function isTombstoneNode(node) {
  2545. if (node && node.classList) {
  2546. return node.classList.contains('tombstone');
  2547. }
  2548. }
  2549. function InfiniteScroller(scroller, options) {
  2550. var _this = this;
  2551. this.options = options;
  2552. assert(typeof this.options.createTombstone === 'function', 'Infinite scroll need createTombstone Function to create tombstone');
  2553. assert(typeof this.options.fetch === 'function', 'Infinite scroll need fetch Function to fetch new data.');
  2554. assert(typeof this.options.render === 'function', 'Infinite scroll need render Function to render each item.');
  2555. this.firstAttachedItem = 0;
  2556. this.lastAttachedItem = 0;
  2557. this.anchorScrollTop = 0;
  2558. this.anchorItem = {
  2559. index: 0,
  2560. offset: 0
  2561. };
  2562. this.tombstoneHeight = 0;
  2563. this.tombstoneWidth = 0;
  2564. this.tombstones = [];
  2565. this.tombstonesAnimationHandlers = [];
  2566. this.items = [];
  2567. this.loadedItems = 0;
  2568. this.requestInProgress = false;
  2569. this.hasMore = true;
  2570. this.scroller = scroller;
  2571. this.wrapperEl = this.scroller.wrapper;
  2572. this.scrollerEl = this.scroller.scroller;
  2573. this.scroller.on('resize', function () {
  2574. _this.onResize();
  2575. });
  2576. this.scroller.on('destroy', function () {
  2577. _this.destroy();
  2578. });
  2579. // wait scroll core init
  2580. this._onResizeHandler = setTimeout(function () {
  2581. _this.onResize();
  2582. // must wait tombstoneHeight has size
  2583. _this.scroller.on('scroll', function () {
  2584. _this.onScroll();
  2585. });
  2586. });
  2587. }
  2588. InfiniteScroller.prototype.destroy = function () {
  2589. var _this2 = this;
  2590. // In extreme scene, destroy is triggered before _onResizeHandler
  2591. clearTimeout(this._onResizeHandler);
  2592. this.tombstonesAnimationHandlers.forEach(function (handler) {
  2593. clearTimeout(handler);
  2594. });
  2595. this.tombstonesAnimationHandlers = null;
  2596. this.items.forEach(function (item) {
  2597. if (item.node) {
  2598. _this2.scrollerEl.removeChild(item.node);
  2599. item.node = null;
  2600. }
  2601. });
  2602. this.scroller.infiniteScroller = null;
  2603. this.scroller = null;
  2604. this.wrapperEl = null;
  2605. this.scrollerEl = null;
  2606. this.items = null;
  2607. this.tombstones = null;
  2608. };
  2609. InfiniteScroller.prototype.onScroll = function () {
  2610. var scrollTop = -this.scroller.y;
  2611. var delta = scrollTop - this.anchorScrollTop;
  2612. if (scrollTop === 0) {
  2613. this.anchorItem = {
  2614. index: 0,
  2615. offset: 0
  2616. };
  2617. } else {
  2618. this.anchorItem = this._calculateAnchoredItem(this.anchorItem, delta);
  2619. }
  2620. this.anchorScrollTop = scrollTop;
  2621. var lastScreenItem = this._calculateAnchoredItem(this.anchorItem, this.scroller.wrapperHeight);
  2622. var start = this.anchorItem.index;
  2623. var end = lastScreenItem.index;
  2624. if (delta < 0) {
  2625. start -= RUNWAY_ITEMS;
  2626. end += RUNWAY_ITEMS_OPPOSITE;
  2627. } else {
  2628. start -= RUNWAY_ITEMS_OPPOSITE;
  2629. end += RUNWAY_ITEMS;
  2630. }
  2631. this.fill(start, end);
  2632. this.maybeRequestContent();
  2633. };
  2634. InfiniteScroller.prototype.onResize = function () {
  2635. var tombstone = this.options.createTombstone();
  2636. tombstone.style.position = 'absolute';
  2637. this.scrollerEl.appendChild(tombstone);
  2638. tombstone.style.display = '';
  2639. this.tombstoneHeight = tombstone.offsetHeight;
  2640. this.tombstoneWidth = tombstone.offsetWidth;
  2641. this.scrollerEl.removeChild(tombstone);
  2642. for (var i = 0; i < this.items.length; i++) {
  2643. this.items[i].height = this.items[i].width = 0;
  2644. }
  2645. this.onScroll();
  2646. };
  2647. InfiniteScroller.prototype.fill = function (start, end) {
  2648. this.firstAttachedItem = Math.max(0, start);
  2649. if (!this.hasMore) {
  2650. end = Math.min(end, this.items.length);
  2651. }
  2652. this.lastAttachedItem = end;
  2653. this.attachContent();
  2654. };
  2655. InfiniteScroller.prototype.maybeRequestContent = function () {
  2656. var _this3 = this;
  2657. if (this.requestInProgress || !this.hasMore) {
  2658. return;
  2659. }
  2660. var itemsNeeded = this.lastAttachedItem - this.loadedItems;
  2661. if (itemsNeeded <= 0) {
  2662. return;
  2663. }
  2664. this.requestInProgress = true;
  2665. this.options.fetch(itemsNeeded).then(function (items) {
  2666. _this3.requestInProgress = false;
  2667. if (items) {
  2668. _this3.addContent(items);
  2669. } else {
  2670. _this3.hasMore = false;
  2671. var tombstoneLen = _this3._removeTombstones();
  2672. var curPos = 0;
  2673. if (_this3.anchorItem.index <= _this3.items.length) {
  2674. curPos = _this3._fixScrollPosition();
  2675. _this3._setupAnimations({}, curPos);
  2676. _this3.scroller.resetPosition(_this3.scroller.options.bounceTime);
  2677. } else {
  2678. _this3.anchorItem.index -= tombstoneLen;
  2679. curPos = _this3._fixScrollPosition();
  2680. _this3._setupAnimations({}, curPos);
  2681. _this3.scroller.stop();
  2682. _this3.scroller.resetPosition();
  2683. _this3.onScroll();
  2684. }
  2685. }
  2686. });
  2687. };
  2688. InfiniteScroller.prototype.addContent = function (items) {
  2689. for (var i = 0; i < items.length; i++) {
  2690. if (this.items.length <= this.loadedItems) {
  2691. this._addItem();
  2692. }
  2693. this.items[this.loadedItems++].data = items[i];
  2694. }
  2695. this.attachContent();
  2696. this.maybeRequestContent();
  2697. };
  2698. InfiniteScroller.prototype.attachContent = function () {
  2699. var unusedNodes = this._collectUnusedNodes();
  2700. var tombstoneAnimations = this._createDOMNodes(unusedNodes);
  2701. this._cleanupUnusedNodes(unusedNodes);
  2702. this._cacheNodeSize();
  2703. var curPos = this._fixScrollPosition();
  2704. this._setupAnimations(tombstoneAnimations, curPos);
  2705. };
  2706. InfiniteScroller.prototype.resetMore = function () {
  2707. this.hasMore = true;
  2708. };
  2709. InfiniteScroller.prototype._removeTombstones = function () {
  2710. var markIndex = void 0;
  2711. var tombstoneLen = 0;
  2712. var itemLen = this.items.length;
  2713. for (var i = 0; i < itemLen; i++) {
  2714. var currentNode = this.items[i].node;
  2715. var currentData = this.items[i].data;
  2716. if ((!currentNode || isTombstoneNode(currentNode)) && !currentData) {
  2717. // 0 should be excluded
  2718. if (markIndex === void 0) {
  2719. markIndex = i;
  2720. }
  2721. if (currentNode) {
  2722. this.scrollerEl.removeChild(currentNode);
  2723. }
  2724. }
  2725. }
  2726. tombstoneLen = itemLen - markIndex;
  2727. this.items.splice(markIndex);
  2728. this.lastAttachedItem = Math.min(this.lastAttachedItem, this.items.length);
  2729. return tombstoneLen;
  2730. };
  2731. InfiniteScroller.prototype._collectUnusedNodes = function () {
  2732. var unusedNodes = [];
  2733. for (var i = 0; i < this.items.length; i++) {
  2734. // Skip the items which should be visible.
  2735. if (i === this.firstAttachedItem) {
  2736. i = this.lastAttachedItem - 1;
  2737. continue;
  2738. }
  2739. var currentNode = this.items[i].node;
  2740. if (currentNode) {
  2741. if (isTombstoneNode(currentNode)) {
  2742. // Cache tombstones for reuse
  2743. this.tombstones.push(currentNode);
  2744. this.tombstones[this.tombstones.length - 1].style.display = 'none';
  2745. } else {
  2746. unusedNodes.push(currentNode);
  2747. }
  2748. }
  2749. this.items[i].node = null;
  2750. }
  2751. return unusedNodes;
  2752. };
  2753. InfiniteScroller.prototype._createDOMNodes = function (unusedNodes) {
  2754. var tombstoneAnimations = {};
  2755. for (var i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
  2756. while (this.items.length <= i) {
  2757. this._addItem();
  2758. }
  2759. var currentNode = this.items[i].node;
  2760. var currentData = this.items[i].data;
  2761. if (currentNode) {
  2762. if (isTombstoneNode(currentNode) && currentData) {
  2763. currentNode.style.zIndex = 1;
  2764. tombstoneAnimations[i] = [currentNode, this.items[i].top - this.anchorScrollTop];
  2765. this.items[i].node = null;
  2766. } else {
  2767. continue;
  2768. }
  2769. }
  2770. var node = currentData ? this.options.render(currentData, unusedNodes.pop()) : this._getTombStone();
  2771. node.style.position = 'absolute';
  2772. this.items[i].top = -1;
  2773. this.scrollerEl.appendChild(node);
  2774. this.items[i].node = node;
  2775. }
  2776. return tombstoneAnimations;
  2777. };
  2778. InfiniteScroller.prototype._cleanupUnusedNodes = function (unusedNodes) {
  2779. while (unusedNodes.length) {
  2780. this.scrollerEl.removeChild(unusedNodes.pop());
  2781. }
  2782. };
  2783. InfiniteScroller.prototype._cacheNodeSize = function () {
  2784. for (var i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
  2785. var item = this.items[i];
  2786. // Only cache the height if we have the real contents, not a placeholder.
  2787. if (item.data && !item.height) {
  2788. var isTombstone = isTombstoneNode(item.node);
  2789. item.height = isTombstone ? this.tombstoneHeight : item.node.offsetHeight;
  2790. item.width = isTombstone ? this.tombstoneWidth : item.node.offsetWidth;
  2791. }
  2792. }
  2793. };
  2794. InfiniteScroller.prototype._fixScrollPosition = function () {
  2795. this.anchorScrollTop = 0;
  2796. for (var _i = 0; _i < this.anchorItem.index; _i++) {
  2797. this.anchorScrollTop += this.items[_i].height || this.tombstoneHeight;
  2798. }
  2799. this.anchorScrollTop += this.anchorItem.offset;
  2800. // Position all nodes.
  2801. var curPos = this.anchorScrollTop - this.anchorItem.offset;
  2802. var i = this.anchorItem.index;
  2803. while (i > this.firstAttachedItem) {
  2804. curPos -= this.items[i - 1].height || this.tombstoneHeight;
  2805. i--;
  2806. }
  2807. return curPos;
  2808. };
  2809. InfiniteScroller.prototype._setupAnimations = function (tombstoneAnimations, curPos) {
  2810. var _this4 = this;
  2811. for (var i in tombstoneAnimations) {
  2812. var animation = tombstoneAnimations[i];
  2813. this.items[i].node.style[style.transform] = 'translateY(' + (this.anchorScrollTop + animation[1]) + 'px) scale(' + this.tombstoneWidth / this.items[i].width + ', ' + this.tombstoneHeight / this.items[i].height + ')';
  2814. // Call offsetTop on the nodes to be animated to force them to apply current transforms.
  2815. /* eslint-disable no-unused-expressions */
  2816. this.items[i].node.offsetTop;
  2817. animation[0].offsetTop;
  2818. this.items[i].node.style[style.transition] = cssVendor + 'transform ' + ANIMATION_DURATION_MS + 'ms';
  2819. }
  2820. for (var _i2 = this.firstAttachedItem; _i2 < this.lastAttachedItem; _i2++) {
  2821. var _animation = tombstoneAnimations[_i2];
  2822. if (_animation) {
  2823. var tombstoneNode = _animation[0];
  2824. tombstoneNode.style[style.transition] = cssVendor + 'transform ' + ANIMATION_DURATION_MS + 'ms, opacity ' + ANIMATION_DURATION_MS + 'ms';
  2825. tombstoneNode.style[style.transform] = 'translateY(' + curPos + 'px) scale(' + this.items[_i2].width / this.tombstoneWidth + ', ' + this.items[_i2].height / this.tombstoneHeight + ')';
  2826. tombstoneNode.style.opacity = 0;
  2827. }
  2828. if (curPos !== this.items[_i2].top) {
  2829. if (!_animation) {
  2830. this.items[_i2].node.style[style.transition] = '';
  2831. }
  2832. this.items[_i2].node.style[style.transform] = 'translateY(' + curPos + 'px)';
  2833. }
  2834. this.items[_i2].top = curPos;
  2835. curPos += this.items[_i2].height || this.tombstoneHeight;
  2836. }
  2837. this.scroller.maxScrollY = -(curPos - this.scroller.wrapperHeight + (this.hasMore ? DEFAULT_SCROLL_RUNWAY : 0));
  2838. var tombstoneAnimationsHandler = setTimeout(function () {
  2839. for (var _i3 in tombstoneAnimations) {
  2840. var _animation2 = tombstoneAnimations[_i3];
  2841. _animation2[0].style.display = 'none';
  2842. // Tombstone can be recycled now.
  2843. _this4.tombstones.push(_animation2[0]);
  2844. }
  2845. }, ANIMATION_DURATION_MS);
  2846. this.tombstonesAnimationHandlers.push(tombstoneAnimationsHandler);
  2847. };
  2848. InfiniteScroller.prototype._getTombStone = function () {
  2849. var tombstone = this.tombstones.pop();
  2850. if (tombstone) {
  2851. tombstone.style.display = '';
  2852. tombstone.style.opacity = 1;
  2853. tombstone.style[style.transform] = '';
  2854. tombstone.style[style.transition] = '';
  2855. return tombstone;
  2856. }
  2857. return this.options.createTombstone();
  2858. };
  2859. InfiniteScroller.prototype._addItem = function () {
  2860. this.items.push({
  2861. data: null,
  2862. node: null,
  2863. height: 0,
  2864. width: 0,
  2865. top: 0
  2866. });
  2867. };
  2868. InfiniteScroller.prototype._calculateAnchoredItem = function (initialAnchor, delta) {
  2869. if (delta === 0) {
  2870. return initialAnchor;
  2871. }
  2872. var i = initialAnchor.index;
  2873. var tombstones = 0;
  2874. delta += initialAnchor.offset;
  2875. if (delta < 0) {
  2876. while (delta < 0 && i > 0 && this.items[i - 1].height) {
  2877. delta += this.items[i - 1].height;
  2878. i--;
  2879. }
  2880. tombstones = Math.max(-i, Math.ceil(Math.min(delta, 0) / this.tombstoneHeight));
  2881. } else {
  2882. while (delta > 0 && i < this.items.length && this.items[i].height && this.items[i].height < delta) {
  2883. delta -= this.items[i].height;
  2884. i++;
  2885. }
  2886. if (i >= this.items.length || !this.items[i].height) {
  2887. tombstones = Math.floor(Math.max(delta, 0) / this.tombstoneHeight);
  2888. }
  2889. }
  2890. i += tombstones;
  2891. delta -= tombstones * this.tombstoneHeight;
  2892. return {
  2893. index: i,
  2894. offset: delta
  2895. };
  2896. };
  2897. function BScroll(el, options) {
  2898. this.wrapper = typeof el === 'string' ? document.querySelector(el) : el;
  2899. if (!this.wrapper) {
  2900. warn('Can not resolve the wrapper DOM.');
  2901. }
  2902. this.scroller = this.wrapper.children[0];
  2903. if (!this.scroller) {
  2904. warn('The wrapper need at least one child element to be scroller.');
  2905. }
  2906. // cache style for better performance
  2907. this.scrollerStyle = this.scroller.style;
  2908. this._init(options);
  2909. }
  2910. initMixin(BScroll);
  2911. coreMixin(BScroll);
  2912. eventMixin(BScroll);
  2913. snapMixin(BScroll);
  2914. wheelMixin(BScroll);
  2915. scrollbarMixin(BScroll);
  2916. pullDownMixin(BScroll);
  2917. pullUpMixin(BScroll);
  2918. mouseWheelMixin(BScroll);
  2919. zoomMixin(BScroll);
  2920. infiniteMixin(BScroll);
  2921. BScroll.Version = '1.15.2';
  2922. return BScroll;
  2923. })));