jquery.nicescroll.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720
  1. /* jquery.nicescroll
  2. -- version 3.7.5
  3. -- copyright 2017-07-13 InuYaksa*2017
  4. -- licensed under the MIT
  5. --
  6. -- https://nicescroll.areaaperta.com/
  7. -- https://github.com/inuyaksa/jquery.nicescroll
  8. --
  9. */
  10. /* jshint expr: true */
  11. (function (factory) {
  12. if (typeof define === 'function' && define.amd) {
  13. // AMD. Register as anonymous module.
  14. define(['jquery'], factory);
  15. } else if (typeof exports === 'object') {
  16. // Node/CommonJS.
  17. module.exports = factory(require('jquery'));
  18. } else {
  19. // Browser globals.
  20. factory(jQuery);
  21. }
  22. }(function (jQuery) {
  23. "use strict";
  24. // globals
  25. var domfocus = false,
  26. mousefocus = false,
  27. tabindexcounter = 0,
  28. ascrailcounter = 2000,
  29. globalmaxzindex = 0;
  30. var $ = jQuery, // sandbox
  31. _doc = document,
  32. _win = window,
  33. $window = $(_win);
  34. var delegatevents = [];
  35. // http://stackoverflow.com/questions/2161159/get-script-path
  36. function getScriptPath() {
  37. var scripts = _doc.currentScript || (function () { var s = _doc.getElementsByTagName('script'); return (s.length) ? s[s.length - 1] : false; })();
  38. var path = scripts ? scripts.src.split('?')[0] : '';
  39. return (path.split('/').length > 0) ? path.split('/').slice(0, -1).join('/') + '/' : '';
  40. }
  41. // based on code by Paul Irish https://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
  42. var setAnimationFrame = _win.requestAnimationFrame || _win.webkitRequestAnimationFrame || _win.mozRequestAnimationFrame || false;
  43. var clearAnimationFrame = _win.cancelAnimationFrame || _win.webkitCancelAnimationFrame || _win.mozCancelAnimationFrame || false;
  44. if (!setAnimationFrame) {
  45. var anilasttime = 0;
  46. setAnimationFrame = function (callback, element) {
  47. var currTime = new Date().getTime();
  48. var timeToCall = Math.max(0, 16 - (currTime - anilasttime));
  49. var id = _win.setTimeout(function () { callback(currTime + timeToCall); },
  50. timeToCall);
  51. anilasttime = currTime + timeToCall;
  52. return id;
  53. };
  54. clearAnimationFrame = function (id) {
  55. _win.clearTimeout(id);
  56. };
  57. } else {
  58. if (!_win.cancelAnimationFrame) clearAnimationFrame = function (id) { };
  59. }
  60. var ClsMutationObserver = _win.MutationObserver || _win.WebKitMutationObserver || false;
  61. var now = Date.now || function () { return new Date().getTime(); };
  62. var _globaloptions = {
  63. zindex: "auto",
  64. cursoropacitymin: 0,
  65. cursoropacitymax: 1,
  66. cursorcolor: "#424242",
  67. cursorwidth: "6px",
  68. cursorborder: "1px solid #fff",
  69. cursorborderradius: "5px",
  70. scrollspeed: 40,
  71. mousescrollstep: 9 * 3,
  72. touchbehavior: false, // deprecated
  73. emulatetouch: false, // replacing touchbehavior
  74. hwacceleration: true,
  75. usetransition: true,
  76. boxzoom: false,
  77. dblclickzoom: true,
  78. gesturezoom: true,
  79. grabcursorenabled: true,
  80. autohidemode: true,
  81. background: "",
  82. iframeautoresize: true,
  83. cursorminheight: 32,
  84. preservenativescrolling: true,
  85. railoffset: false,
  86. railhoffset: false,
  87. bouncescroll: true,
  88. spacebarenabled: true,
  89. railpadding: {
  90. top: 0,
  91. right: 0,
  92. left: 0,
  93. bottom: 0
  94. },
  95. disableoutline: true,
  96. horizrailenabled: true,
  97. railalign: "right",
  98. railvalign: "bottom",
  99. enabletranslate3d: true,
  100. enablemousewheel: true,
  101. enablekeyboard: true,
  102. smoothscroll: true,
  103. sensitiverail: true,
  104. enablemouselockapi: true,
  105. // cursormaxheight:false,
  106. cursorfixedheight: false,
  107. directionlockdeadzone: 6,
  108. hidecursordelay: 400,
  109. nativeparentscrolling: true,
  110. enablescrollonselection: true,
  111. overflowx: true,
  112. overflowy: true,
  113. cursordragspeed: 0.3,
  114. rtlmode: "auto",
  115. cursordragontouch: false,
  116. oneaxismousemode: "auto",
  117. scriptpath: getScriptPath(),
  118. preventmultitouchscrolling: true,
  119. disablemutationobserver: false,
  120. enableobserver: true,
  121. scrollbarid: false
  122. };
  123. var browserdetected = false;
  124. var getBrowserDetection = function () {
  125. if (browserdetected) return browserdetected;
  126. var _el = _doc.createElement('DIV'),
  127. _style = _el.style,
  128. _agent = navigator.userAgent,
  129. _platform = navigator.platform,
  130. d = {};
  131. d.haspointerlock = "pointerLockElement" in _doc || "webkitPointerLockElement" in _doc || "mozPointerLockElement" in _doc;
  132. d.isopera = ("opera" in _win); // 12-
  133. d.isopera12 = (d.isopera && ("getUserMedia" in navigator));
  134. d.isoperamini = (Object.prototype.toString.call(_win.operamini) === "[object OperaMini]");
  135. d.isie = (("all" in _doc) && ("attachEvent" in _el) && !d.isopera); //IE10-
  136. d.isieold = (d.isie && !("msInterpolationMode" in _style)); // IE6 and older
  137. d.isie7 = d.isie && !d.isieold && (!("documentMode" in _doc) || (_doc.documentMode === 7));
  138. d.isie8 = d.isie && ("documentMode" in _doc) && (_doc.documentMode === 8);
  139. d.isie9 = d.isie && ("performance" in _win) && (_doc.documentMode === 9);
  140. d.isie10 = d.isie && ("performance" in _win) && (_doc.documentMode === 10);
  141. d.isie11 = ("msRequestFullscreen" in _el) && (_doc.documentMode >= 11); // IE11+
  142. d.ismsedge = ("msCredentials" in _win); // MS Edge 14+
  143. d.ismozilla = ("MozAppearance" in _style);
  144. d.iswebkit = !d.ismsedge && ("WebkitAppearance" in _style);
  145. d.ischrome = d.iswebkit && ("chrome" in _win);
  146. d.ischrome38 = (d.ischrome && ("touchAction" in _style)); // behavior changed in touch emulation
  147. d.ischrome22 = (!d.ischrome38) && (d.ischrome && d.haspointerlock);
  148. d.ischrome26 = (!d.ischrome38) && (d.ischrome && ("transition" in _style)); // issue with transform detection (maintain prefix)
  149. d.cantouch = ("ontouchstart" in _doc.documentElement) || ("ontouchstart" in _win); // with detection for Chrome Touch Emulation
  150. d.hasw3ctouch = (_win.PointerEvent || false) && ((navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)); //IE11 pointer events, following W3C Pointer Events spec
  151. d.hasmstouch = (!d.hasw3ctouch) && (_win.MSPointerEvent || false); // IE10 pointer events
  152. d.ismac = /^mac$/i.test(_platform);
  153. d.isios = d.cantouch && /iphone|ipad|ipod/i.test(_platform);
  154. d.isios4 = d.isios && !("seal" in Object);
  155. d.isios7 = d.isios && ("webkitHidden" in _doc); //iOS 7+
  156. d.isios8 = d.isios && ("hidden" in _doc); //iOS 8+
  157. d.isios10 = d.isios && _win.Proxy; //iOS 10+
  158. d.isandroid = (/android/i.test(_agent));
  159. d.haseventlistener = ("addEventListener" in _el);
  160. d.trstyle = false;
  161. d.hastransform = false;
  162. d.hastranslate3d = false;
  163. d.transitionstyle = false;
  164. d.hastransition = false;
  165. d.transitionend = false;
  166. d.trstyle = "transform";
  167. d.hastransform = ("transform" in _style) || (function () {
  168. var check = ['msTransform', 'webkitTransform', 'MozTransform', 'OTransform'];
  169. for (var a = 0, c = check.length; a < c; a++) {
  170. if (_style[check[a]] !== undefined) {
  171. d.trstyle = check[a];
  172. break;
  173. }
  174. }
  175. d.hastransform = (!!d.trstyle);
  176. })();
  177. if (d.hastransform) {
  178. _style[d.trstyle] = "translate3d(1px,2px,3px)";
  179. d.hastranslate3d = /translate3d/.test(_style[d.trstyle]);
  180. }
  181. d.transitionstyle = "transition";
  182. d.prefixstyle = '';
  183. d.transitionend = "transitionend";
  184. d.hastransition = ("transition" in _style) || (function () {
  185. d.transitionend = false;
  186. var check = ['webkitTransition', 'msTransition', 'MozTransition', 'OTransition', 'OTransition', 'KhtmlTransition'];
  187. var prefix = ['-webkit-', '-ms-', '-moz-', '-o-', '-o', '-khtml-'];
  188. var evs = ['webkitTransitionEnd', 'msTransitionEnd', 'transitionend', 'otransitionend', 'oTransitionEnd', 'KhtmlTransitionEnd'];
  189. for (var a = 0, c = check.length; a < c; a++) {
  190. if (check[a] in _style) {
  191. d.transitionstyle = check[a];
  192. d.prefixstyle = prefix[a];
  193. d.transitionend = evs[a];
  194. break;
  195. }
  196. }
  197. if (d.ischrome26) d.prefixstyle = prefix[1]; // always use prefix
  198. d.hastransition = (d.transitionstyle);
  199. })();
  200. function detectCursorGrab() {
  201. var lst = ['grab', '-webkit-grab', '-moz-grab'];
  202. if ((d.ischrome && !d.ischrome38) || d.isie) lst = []; // force setting for IE returns false positive and chrome cursor bug
  203. for (var a = 0, l = lst.length; a < l; a++) {
  204. var p = lst[a];
  205. _style.cursor = p;
  206. if (_style.cursor == p) return p;
  207. }
  208. return 'url(https://cdnjs.cloudflare.com/ajax/libs/slider-pro/1.3.0/css/images/openhand.cur),n-resize'; // thanks to https://cdnjs.com/ for the openhand cursor!
  209. }
  210. d.cursorgrabvalue = detectCursorGrab();
  211. d.hasmousecapture = ("setCapture" in _el);
  212. d.hasMutationObserver = (ClsMutationObserver !== false);
  213. _el = null; //memory released
  214. browserdetected = d;
  215. return d;
  216. };
  217. var NiceScrollClass = function (myopt, me) {
  218. var self = this;
  219. this.version = '3.7.5';
  220. this.name = 'nicescroll';
  221. this.me = me;
  222. var $body = $("body");
  223. var opt = this.opt = {
  224. doc: $body,
  225. win: false
  226. };
  227. $.extend(opt, _globaloptions); // clone opts
  228. // Options for internal use
  229. opt.snapbackspeed = 80;
  230. if (myopt || false) {
  231. for (var a in opt) {
  232. if (myopt[a] !== undefined) opt[a] = myopt[a];
  233. }
  234. }
  235. if (opt.disablemutationobserver) ClsMutationObserver = false;
  236. this.doc = opt.doc;
  237. this.iddoc = (this.doc && this.doc[0]) ? this.doc[0].id || '' : '';
  238. this.ispage = /^BODY|HTML/.test((opt.win) ? opt.win[0].nodeName : this.doc[0].nodeName);
  239. this.haswrapper = (opt.win !== false);
  240. this.win = opt.win || (this.ispage ? $window : this.doc);
  241. this.docscroll = (this.ispage && !this.haswrapper) ? $window : this.win;
  242. this.body = $body;
  243. this.viewport = false;
  244. this.isfixed = false;
  245. this.iframe = false;
  246. this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName == 'IFRAME'));
  247. this.istextarea = (this.win[0].nodeName == 'TEXTAREA');
  248. this.forcescreen = false; //force to use screen position on events
  249. this.canshowonmouseevent = (opt.autohidemode != "scroll");
  250. // Events jump table
  251. this.onmousedown = false;
  252. this.onmouseup = false;
  253. this.onmousemove = false;
  254. this.onmousewheel = false;
  255. this.onkeypress = false;
  256. this.ongesturezoom = false;
  257. this.onclick = false;
  258. // Nicescroll custom events
  259. this.onscrollstart = false;
  260. this.onscrollend = false;
  261. this.onscrollcancel = false;
  262. this.onzoomin = false;
  263. this.onzoomout = false;
  264. // Let's start!
  265. this.view = false;
  266. this.page = false;
  267. this.scroll = {
  268. x: 0,
  269. y: 0
  270. };
  271. this.scrollratio = {
  272. x: 0,
  273. y: 0
  274. };
  275. this.cursorheight = 20;
  276. this.scrollvaluemax = 0;
  277. // http://dev.w3.org/csswg/css-writing-modes-3/#logical-to-physical
  278. // http://dev.w3.org/csswg/css-writing-modes-3/#svg-writing-mode
  279. if (opt.rtlmode == "auto") {
  280. var target = this.win[0] == _win ? this.body : this.win;
  281. var writingMode = target.css("writing-mode") || target.css("-webkit-writing-mode") || target.css("-ms-writing-mode") || target.css("-moz-writing-mode");
  282. if (writingMode == "horizontal-tb" || writingMode == "lr-tb" || writingMode === "") {
  283. this.isrtlmode = (target.css("direction") == "rtl");
  284. this.isvertical = false;
  285. } else {
  286. this.isrtlmode = (writingMode == "vertical-rl" || writingMode == "tb" || writingMode == "tb-rl" || writingMode == "rl-tb");
  287. this.isvertical = (writingMode == "vertical-rl" || writingMode == "tb" || writingMode == "tb-rl");
  288. }
  289. } else {
  290. this.isrtlmode = (opt.rtlmode === true);
  291. this.isvertical = false;
  292. }
  293. // this.checkrtlmode = false;
  294. this.scrollrunning = false;
  295. this.scrollmom = false;
  296. this.observer = false; // observer div changes
  297. this.observerremover = false; // observer on parent for remove detection
  298. this.observerbody = false; // observer on body for position change
  299. if (opt.scrollbarid !== false) {
  300. this.id = opt.scrollbarid;
  301. } else {
  302. do {
  303. this.id = "ascrail" + (ascrailcounter++);
  304. } while (_doc.getElementById(this.id));
  305. }
  306. this.rail = false;
  307. this.cursor = false;
  308. this.cursorfreezed = false;
  309. this.selectiondrag = false;
  310. this.zoom = false;
  311. this.zoomactive = false;
  312. this.hasfocus = false;
  313. this.hasmousefocus = false;
  314. //this.visibility = true;
  315. this.railslocked = false; // locked by resize
  316. this.locked = false; // prevent lost of locked status sets by user
  317. this.hidden = false; // rails always hidden
  318. this.cursoractive = true; // user can interact with cursors
  319. this.wheelprevented = false; //prevent mousewheel event
  320. this.overflowx = opt.overflowx;
  321. this.overflowy = opt.overflowy;
  322. this.nativescrollingarea = false;
  323. this.checkarea = 0;
  324. this.events = []; // event list for unbind
  325. this.saved = {}; // style saved
  326. this.delaylist = {};
  327. this.synclist = {};
  328. this.lastdeltax = 0;
  329. this.lastdeltay = 0;
  330. this.detected = getBrowserDetection();
  331. var cap = $.extend({}, this.detected);
  332. this.canhwscroll = (cap.hastransform && opt.hwacceleration);
  333. this.ishwscroll = (this.canhwscroll && self.haswrapper);
  334. if (!this.isrtlmode) {
  335. this.hasreversehr = false;
  336. } else if (this.isvertical) { // RTL mode with reverse horizontal axis
  337. this.hasreversehr = !(cap.iswebkit || cap.isie || cap.isie11);
  338. } else {
  339. this.hasreversehr = !(cap.iswebkit || (cap.isie && !cap.isie10 && !cap.isie11));
  340. }
  341. this.istouchcapable = false; // desktop devices with touch screen support
  342. //## Check WebKit-based desktop with touch support
  343. //## + Firefox 18 nightly build (desktop) false positive (or desktop with touch support)
  344. if (!cap.cantouch && (cap.hasw3ctouch || cap.hasmstouch)) { // desktop device with multiple input
  345. this.istouchcapable = true;
  346. } else if (cap.cantouch && !cap.isios && !cap.isandroid && (cap.iswebkit || cap.ismozilla)) {
  347. this.istouchcapable = true;
  348. }
  349. //## disable MouseLock API on user request
  350. if (!opt.enablemouselockapi) {
  351. cap.hasmousecapture = false;
  352. cap.haspointerlock = false;
  353. }
  354. this.debounced = function (name, fn, tm) {
  355. if (!self) return;
  356. var dd = self.delaylist[name] || false;
  357. if (!dd) {
  358. self.delaylist[name] = {
  359. h: setAnimationFrame(function () {
  360. self.delaylist[name].fn.call(self);
  361. self.delaylist[name] = false;
  362. }, tm)
  363. };
  364. fn.call(self);
  365. }
  366. self.delaylist[name].fn = fn;
  367. };
  368. this.synched = function (name, fn) {
  369. if (self.synclist[name]) self.synclist[name] = fn;
  370. else {
  371. self.synclist[name] = fn;
  372. setAnimationFrame(function () {
  373. if (!self) return;
  374. self.synclist[name] && self.synclist[name].call(self);
  375. self.synclist[name] = null;
  376. });
  377. }
  378. };
  379. this.unsynched = function (name) {
  380. if (self.synclist[name]) self.synclist[name] = false;
  381. };
  382. this.css = function (el, pars) { // save & set
  383. for (var n in pars) {
  384. self.saved.css.push([el, n, el.css(n)]);
  385. el.css(n, pars[n]);
  386. }
  387. };
  388. this.scrollTop = function (val) {
  389. return (val === undefined) ? self.getScrollTop() : self.setScrollTop(val);
  390. };
  391. this.scrollLeft = function (val) {
  392. return (val === undefined) ? self.getScrollLeft() : self.setScrollLeft(val);
  393. };
  394. // derived by by Dan Pupius www.pupius.net
  395. var BezierClass = function (st, ed, spd, p1, p2, p3, p4) {
  396. this.st = st;
  397. this.ed = ed;
  398. this.spd = spd;
  399. this.p1 = p1 || 0;
  400. this.p2 = p2 || 1;
  401. this.p3 = p3 || 0;
  402. this.p4 = p4 || 1;
  403. this.ts = now();
  404. this.df = ed - st;
  405. };
  406. BezierClass.prototype = {
  407. B2: function (t) {
  408. return 3 * (1 - t) * (1 - t) * t;
  409. },
  410. B3: function (t) {
  411. return 3 * (1 - t) * t * t;
  412. },
  413. B4: function (t) {
  414. return t * t * t;
  415. },
  416. getPos: function () {
  417. return (now() - this.ts) / this.spd;
  418. },
  419. getNow: function () {
  420. var pc = (now() - this.ts) / this.spd;
  421. var bz = this.B2(pc) + this.B3(pc) + this.B4(pc);
  422. return (pc >= 1) ? this.ed : this.st + (this.df * bz) | 0;
  423. },
  424. update: function (ed, spd) {
  425. this.st = this.getNow();
  426. this.ed = ed;
  427. this.spd = spd;
  428. this.ts = now();
  429. this.df = this.ed - this.st;
  430. return this;
  431. }
  432. };
  433. //derived from http://stackoverflow.com/questions/11236090/
  434. function getMatrixValues() {
  435. var tr = self.doc.css(cap.trstyle);
  436. if (tr && (tr.substr(0, 6) == "matrix")) {
  437. return tr.replace(/^.*\((.*)\)$/g, "$1").replace(/px/g, '').split(/, +/);
  438. }
  439. return false;
  440. }
  441. if (this.ishwscroll) { // hw accelerated scroll
  442. this.doc.translate = {
  443. x: 0,
  444. y: 0,
  445. tx: "0px",
  446. ty: "0px"
  447. };
  448. //this one can help to enable hw accel on ios6 http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/
  449. if (cap.hastranslate3d && cap.isios) this.doc.css("-webkit-backface-visibility", "hidden"); // prevent flickering http://stackoverflow.com/questions/3461441/
  450. this.getScrollTop = function (last) {
  451. if (!last) {
  452. var mtx = getMatrixValues();
  453. if (mtx) return (mtx.length == 16) ? -mtx[13] : -mtx[5]; //matrix3d 16 on IE10
  454. if (self.timerscroll && self.timerscroll.bz) return self.timerscroll.bz.getNow();
  455. }
  456. return self.doc.translate.y;
  457. };
  458. this.getScrollLeft = function (last) {
  459. if (!last) {
  460. var mtx = getMatrixValues();
  461. if (mtx) return (mtx.length == 16) ? -mtx[12] : -mtx[4]; //matrix3d 16 on IE10
  462. if (self.timerscroll && self.timerscroll.bh) return self.timerscroll.bh.getNow();
  463. }
  464. return self.doc.translate.x;
  465. };
  466. this.notifyScrollEvent = function (el) {
  467. var e = _doc.createEvent("UIEvents");
  468. e.initUIEvent("scroll", false, false, _win, 1);
  469. e.niceevent = true;
  470. el.dispatchEvent(e);
  471. };
  472. var cxscrollleft = (this.isrtlmode) ? 1 : -1;
  473. if (cap.hastranslate3d && opt.enabletranslate3d) {
  474. this.setScrollTop = function (val, silent) {
  475. self.doc.translate.y = val;
  476. self.doc.translate.ty = (val * -1) + "px";
  477. self.doc.css(cap.trstyle, "translate3d(" + self.doc.translate.tx + "," + self.doc.translate.ty + ",0)");
  478. if (!silent) self.notifyScrollEvent(self.win[0]);
  479. };
  480. this.setScrollLeft = function (val, silent) {
  481. self.doc.translate.x = val;
  482. self.doc.translate.tx = (val * cxscrollleft) + "px";
  483. self.doc.css(cap.trstyle, "translate3d(" + self.doc.translate.tx + "," + self.doc.translate.ty + ",0)");
  484. if (!silent) self.notifyScrollEvent(self.win[0]);
  485. };
  486. } else {
  487. this.setScrollTop = function (val, silent) {
  488. self.doc.translate.y = val;
  489. self.doc.translate.ty = (val * -1) + "px";
  490. self.doc.css(cap.trstyle, "translate(" + self.doc.translate.tx + "," + self.doc.translate.ty + ")");
  491. if (!silent) self.notifyScrollEvent(self.win[0]);
  492. };
  493. this.setScrollLeft = function (val, silent) {
  494. self.doc.translate.x = val;
  495. self.doc.translate.tx = (val * cxscrollleft) + "px";
  496. self.doc.css(cap.trstyle, "translate(" + self.doc.translate.tx + "," + self.doc.translate.ty + ")");
  497. if (!silent) self.notifyScrollEvent(self.win[0]);
  498. };
  499. }
  500. } else { // native scroll
  501. this.getScrollTop = function () {
  502. return self.docscroll.scrollTop();
  503. };
  504. this.setScrollTop = function (val) {
  505. self.docscroll.scrollTop(val);
  506. };
  507. this.getScrollLeft = function () {
  508. var val;
  509. if (!self.hasreversehr) {
  510. val = self.docscroll.scrollLeft();
  511. } else if (self.detected.ismozilla) {
  512. val = self.page.maxw - Math.abs(self.docscroll.scrollLeft());
  513. } else {
  514. val = self.page.maxw - self.docscroll.scrollLeft();
  515. }
  516. return val;
  517. };
  518. this.setScrollLeft = function (val) {
  519. return setTimeout(function () {
  520. if (!self) return;
  521. if (self.hasreversehr) {
  522. if (self.detected.ismozilla) {
  523. val = -(self.page.maxw - val);
  524. } else {
  525. val = self.page.maxw - val;
  526. }
  527. }
  528. return self.docscroll.scrollLeft(val);
  529. }, 1);
  530. };
  531. }
  532. this.getTarget = function (e) {
  533. if (!e) return false;
  534. if (e.target) return e.target;
  535. if (e.srcElement) return e.srcElement;
  536. return false;
  537. };
  538. this.hasParent = function (e, id) {
  539. if (!e) return false;
  540. var el = e.target || e.srcElement || e || false;
  541. while (el && el.id != id) {
  542. el = el.parentNode || false;
  543. }
  544. return (el !== false);
  545. };
  546. function getZIndex() {
  547. var dom = self.win;
  548. if ("zIndex" in dom) return dom.zIndex(); // use jQuery UI method when available
  549. while (dom.length > 0) {
  550. if (dom[0].nodeType == 9) return false;
  551. var zi = dom.css('zIndex');
  552. if (!isNaN(zi) && zi !== 0) return parseInt(zi);
  553. dom = dom.parent();
  554. }
  555. return false;
  556. }
  557. //inspired by http://forum.jquery.com/topic/width-includes-border-width-when-set-to-thin-medium-thick-in-ie
  558. var _convertBorderWidth = {
  559. "thin": 1,
  560. "medium": 3,
  561. "thick": 5
  562. };
  563. function getWidthToPixel(dom, prop, chkheight) {
  564. var wd = dom.css(prop);
  565. var px = parseFloat(wd);
  566. if (isNaN(px)) {
  567. px = _convertBorderWidth[wd] || 0;
  568. var brd = (px == 3) ? ((chkheight) ? (self.win.outerHeight() - self.win.innerHeight()) : (self.win.outerWidth() - self.win.innerWidth())) : 1; //DON'T TRUST CSS
  569. if (self.isie8 && px) px += 1;
  570. return (brd) ? px : 0;
  571. }
  572. return px;
  573. }
  574. this.getDocumentScrollOffset = function () {
  575. return {
  576. top: _win.pageYOffset || _doc.documentElement.scrollTop,
  577. left: _win.pageXOffset || _doc.documentElement.scrollLeft
  578. };
  579. };
  580. this.getOffset = function () {
  581. if (self.isfixed) {
  582. var ofs = self.win.offset(); // fix Chrome auto issue (when right/bottom props only)
  583. var scrl = self.getDocumentScrollOffset();
  584. ofs.top -= scrl.top;
  585. ofs.left -= scrl.left;
  586. return ofs;
  587. }
  588. var ww = self.win.offset();
  589. if (!self.viewport) return ww;
  590. var vp = self.viewport.offset();
  591. return {
  592. top: ww.top - vp.top,
  593. left: ww.left - vp.left
  594. };
  595. };
  596. this.updateScrollBar = function (len) {
  597. var pos, off;
  598. if (self.ishwscroll) {
  599. self.rail.css({
  600. height: self.win.innerHeight() - (opt.railpadding.top + opt.railpadding.bottom)
  601. });
  602. if (self.railh) self.railh.css({
  603. width: self.win.innerWidth() - (opt.railpadding.left + opt.railpadding.right)
  604. });
  605. } else {
  606. var wpos = self.getOffset();
  607. pos = {
  608. top: wpos.top,
  609. left: wpos.left - (opt.railpadding.left + opt.railpadding.right)
  610. };
  611. pos.top += getWidthToPixel(self.win, 'border-top-width', true);
  612. pos.left += (self.rail.align) ? self.win.outerWidth() - getWidthToPixel(self.win, 'border-right-width') - self.rail.width : getWidthToPixel(self.win, 'border-left-width');
  613. off = opt.railoffset;
  614. if (off) {
  615. if (off.top) pos.top += off.top;
  616. if (off.left) pos.left += off.left;
  617. }
  618. if (!self.railslocked) self.rail.css({
  619. top: pos.top,
  620. left: pos.left,
  621. height: ((len) ? len.h : self.win.innerHeight()) - (opt.railpadding.top + opt.railpadding.bottom)
  622. });
  623. if (self.zoom) {
  624. self.zoom.css({
  625. top: pos.top + 1,
  626. left: (self.rail.align == 1) ? pos.left - 20 : pos.left + self.rail.width + 4
  627. });
  628. }
  629. if (self.railh && !self.railslocked) {
  630. pos = {
  631. top: wpos.top,
  632. left: wpos.left
  633. };
  634. off = opt.railhoffset;
  635. if (off) {
  636. if (off.top) pos.top += off.top;
  637. if (off.left) pos.left += off.left;
  638. }
  639. var y = (self.railh.align) ? pos.top + getWidthToPixel(self.win, 'border-top-width', true) + self.win.innerHeight() - self.railh.height : pos.top + getWidthToPixel(self.win, 'border-top-width', true);
  640. var x = pos.left + getWidthToPixel(self.win, 'border-left-width');
  641. self.railh.css({
  642. top: y - (opt.railpadding.top + opt.railpadding.bottom),
  643. left: x,
  644. width: self.railh.width
  645. });
  646. }
  647. }
  648. };
  649. this.doRailClick = function (e, dbl, hr) {
  650. var fn, pg, cur, pos;
  651. if (self.railslocked) return;
  652. self.cancelEvent(e);
  653. if (!("pageY" in e)) {
  654. e.pageX = e.clientX + _doc.documentElement.scrollLeft;
  655. e.pageY = e.clientY + _doc.documentElement.scrollTop;
  656. }
  657. if (dbl) {
  658. fn = (hr) ? self.doScrollLeft : self.doScrollTop;
  659. cur = (hr) ? ((e.pageX - self.railh.offset().left - (self.cursorwidth / 2)) * self.scrollratio.x) : ((e.pageY - self.rail.offset().top - (self.cursorheight / 2)) * self.scrollratio.y);
  660. self.unsynched("relativexy");
  661. fn(cur|0);
  662. } else {
  663. fn = (hr) ? self.doScrollLeftBy : self.doScrollBy;
  664. cur = (hr) ? self.scroll.x : self.scroll.y;
  665. pos = (hr) ? e.pageX - self.railh.offset().left : e.pageY - self.rail.offset().top;
  666. pg = (hr) ? self.view.w : self.view.h;
  667. fn((cur >= pos) ? pg : -pg);
  668. }
  669. };
  670. self.newscrolly = self.newscrollx = 0;
  671. self.hasanimationframe = ("requestAnimationFrame" in _win);
  672. self.hascancelanimationframe = ("cancelAnimationFrame" in _win);
  673. self.hasborderbox = false;
  674. this.init = function () {
  675. self.saved.css = [];
  676. if (cap.isoperamini) return true; // SORRY, DO NOT WORK!
  677. if (cap.isandroid && !("hidden" in _doc)) return true; // Android 3- SORRY, DO NOT WORK!
  678. opt.emulatetouch = opt.emulatetouch || opt.touchbehavior; // mantain compatibility with "touchbehavior"
  679. self.hasborderbox = _win.getComputedStyle && (_win.getComputedStyle(_doc.body)['box-sizing'] === "border-box");
  680. var _scrollyhidden = { 'overflow-y': 'hidden' };
  681. if (cap.isie11 || cap.isie10) _scrollyhidden['-ms-overflow-style'] = 'none'; // IE 10 & 11 is always a world apart!
  682. if (self.ishwscroll) {
  683. this.doc.css(cap.transitionstyle, cap.prefixstyle + 'transform 0ms ease-out');
  684. if (cap.transitionend) self.bind(self.doc, cap.transitionend, self.onScrollTransitionEnd, false); //I have got to do something usefull!!
  685. }
  686. self.zindex = "auto";
  687. if (!self.ispage && opt.zindex == "auto") {
  688. self.zindex = getZIndex() || "auto";
  689. } else {
  690. self.zindex = opt.zindex;
  691. }
  692. if (!self.ispage && self.zindex != "auto" && self.zindex > globalmaxzindex) {
  693. globalmaxzindex = self.zindex;
  694. }
  695. if (self.isie && self.zindex === 0 && opt.zindex == "auto") { // fix IE auto == 0
  696. self.zindex = "auto";
  697. }
  698. if (!self.ispage || !cap.isieold) {
  699. var cont = self.docscroll;
  700. if (self.ispage) cont = (self.haswrapper) ? self.win : self.doc;
  701. self.css(cont, _scrollyhidden);
  702. if (self.ispage && (cap.isie11 || cap.isie)) { // IE 7-11
  703. self.css($("html"), _scrollyhidden);
  704. }
  705. if (cap.isios && !self.ispage && !self.haswrapper) self.css($body, {
  706. "-webkit-overflow-scrolling": "touch"
  707. }); //force hw acceleration
  708. var cursor = $(_doc.createElement('div'));
  709. cursor.css({
  710. position: "relative",
  711. top: 0,
  712. "float": "right",
  713. width: opt.cursorwidth,
  714. height: 0,
  715. 'background-color': opt.cursorcolor,
  716. border: opt.cursorborder,
  717. 'background-clip': 'padding-box',
  718. '-webkit-border-radius': opt.cursorborderradius,
  719. '-moz-border-radius': opt.cursorborderradius,
  720. 'border-radius': opt.cursorborderradius
  721. });
  722. cursor.addClass('nicescroll-cursors');
  723. self.cursor = cursor;
  724. var rail = $(_doc.createElement('div'));
  725. rail.attr('id', self.id);
  726. rail.addClass('nicescroll-rails nicescroll-rails-vr');
  727. var v, a, kp = ["left", "right", "top", "bottom"]; //**
  728. for (var n in kp) {
  729. a = kp[n];
  730. v = opt.railpadding[a] || 0;
  731. v && rail.css("padding-" + a, v + "px");
  732. }
  733. rail.append(cursor);
  734. rail.width = Math.max(parseFloat(opt.cursorwidth), cursor.outerWidth());
  735. rail.css({
  736. width: rail.width + "px",
  737. zIndex: self.zindex,
  738. background: opt.background,
  739. cursor: "default"
  740. });
  741. rail.visibility = true;
  742. rail.scrollable = true;
  743. rail.align = (opt.railalign == "left") ? 0 : 1;
  744. self.rail = rail;
  745. self.rail.drag = false;
  746. var zoom = false;
  747. if (opt.boxzoom && !self.ispage && !cap.isieold) {
  748. zoom = _doc.createElement('div');
  749. self.bind(zoom, "click", self.doZoom);
  750. self.bind(zoom, "mouseenter", function () {
  751. self.zoom.css('opacity', opt.cursoropacitymax);
  752. });
  753. self.bind(zoom, "mouseleave", function () {
  754. self.zoom.css('opacity', opt.cursoropacitymin);
  755. });
  756. self.zoom = $(zoom);
  757. self.zoom.css({
  758. cursor: "pointer",
  759. zIndex: self.zindex,
  760. backgroundImage: 'url(' + opt.scriptpath + 'zoomico.png)',
  761. height: 18,
  762. width: 18,
  763. backgroundPosition: '0 0'
  764. });
  765. if (opt.dblclickzoom) self.bind(self.win, "dblclick", self.doZoom);
  766. if (cap.cantouch && opt.gesturezoom) {
  767. self.ongesturezoom = function (e) {
  768. if (e.scale > 1.5) self.doZoomIn(e);
  769. if (e.scale < 0.8) self.doZoomOut(e);
  770. return self.cancelEvent(e);
  771. };
  772. self.bind(self.win, "gestureend", self.ongesturezoom);
  773. }
  774. }
  775. // init HORIZ
  776. self.railh = false;
  777. var railh;
  778. if (opt.horizrailenabled) {
  779. self.css(cont, {
  780. overflowX: 'hidden'
  781. });
  782. cursor = $(_doc.createElement('div'));
  783. cursor.css({
  784. position: "absolute",
  785. top: 0,
  786. height: opt.cursorwidth,
  787. width: 0,
  788. backgroundColor: opt.cursorcolor,
  789. border: opt.cursorborder,
  790. backgroundClip: 'padding-box',
  791. '-webkit-border-radius': opt.cursorborderradius,
  792. '-moz-border-radius': opt.cursorborderradius,
  793. 'border-radius': opt.cursorborderradius
  794. });
  795. if (cap.isieold) cursor.css('overflow', 'hidden'); //IE6 horiz scrollbar issue
  796. cursor.addClass('nicescroll-cursors');
  797. self.cursorh = cursor;
  798. railh = $(_doc.createElement('div'));
  799. railh.attr('id', self.id + '-hr');
  800. railh.addClass('nicescroll-rails nicescroll-rails-hr');
  801. railh.height = Math.max(parseFloat(opt.cursorwidth), cursor.outerHeight());
  802. railh.css({
  803. height: railh.height + "px",
  804. 'zIndex': self.zindex,
  805. "background": opt.background
  806. });
  807. railh.append(cursor);
  808. railh.visibility = true;
  809. railh.scrollable = true;
  810. railh.align = (opt.railvalign == "top") ? 0 : 1;
  811. self.railh = railh;
  812. self.railh.drag = false;
  813. }
  814. if (self.ispage) {
  815. rail.css({
  816. position: "fixed",
  817. top: 0,
  818. height: "100%"
  819. });
  820. rail.css((rail.align) ? { right: 0 } : { left: 0 });
  821. self.body.append(rail);
  822. if (self.railh) {
  823. railh.css({
  824. position: "fixed",
  825. left: 0,
  826. width: "100%"
  827. });
  828. railh.css((railh.align) ? { bottom: 0 } : { top: 0 });
  829. self.body.append(railh);
  830. }
  831. } else {
  832. if (self.ishwscroll) {
  833. if (self.win.css('position') == 'static') self.css(self.win, { 'position': 'relative' });
  834. var bd = (self.win[0].nodeName == 'HTML') ? self.body : self.win;
  835. $(bd).scrollTop(0).scrollLeft(0); // fix rail position if content already scrolled
  836. if (self.zoom) {
  837. self.zoom.css({
  838. position: "absolute",
  839. top: 1,
  840. right: 0,
  841. "margin-right": rail.width + 4
  842. });
  843. bd.append(self.zoom);
  844. }
  845. rail.css({
  846. position: "absolute",
  847. top: 0
  848. });
  849. rail.css((rail.align) ? { right: 0 } : { left: 0 });
  850. bd.append(rail);
  851. if (railh) {
  852. railh.css({
  853. position: "absolute",
  854. left: 0,
  855. bottom: 0
  856. });
  857. railh.css((railh.align) ? { bottom: 0 } : { top: 0 });
  858. bd.append(railh);
  859. }
  860. } else {
  861. self.isfixed = (self.win.css("position") == "fixed");
  862. var rlpos = (self.isfixed) ? "fixed" : "absolute";
  863. if (!self.isfixed) self.viewport = self.getViewport(self.win[0]);
  864. if (self.viewport) {
  865. self.body = self.viewport;
  866. if (!(/fixed|absolute/.test(self.viewport.css("position")))) self.css(self.viewport, {
  867. "position": "relative"
  868. });
  869. }
  870. rail.css({
  871. position: rlpos
  872. });
  873. if (self.zoom) self.zoom.css({
  874. position: rlpos
  875. });
  876. self.updateScrollBar();
  877. self.body.append(rail);
  878. if (self.zoom) self.body.append(self.zoom);
  879. if (self.railh) {
  880. railh.css({
  881. position: rlpos
  882. });
  883. self.body.append(railh);
  884. }
  885. }
  886. if (cap.isios) self.css(self.win, {
  887. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)',
  888. '-webkit-touch-callout': 'none'
  889. }); // prevent grey layer on click
  890. if (opt.disableoutline) {
  891. if (cap.isie) self.win.attr("hideFocus", "true"); // IE, prevent dotted rectangle on focused div
  892. if (cap.iswebkit) self.win.css('outline', 'none'); // Webkit outline
  893. }
  894. }
  895. if (opt.autohidemode === false) {
  896. self.autohidedom = false;
  897. self.rail.css({
  898. opacity: opt.cursoropacitymax
  899. });
  900. if (self.railh) self.railh.css({
  901. opacity: opt.cursoropacitymax
  902. });
  903. } else if ((opt.autohidemode === true) || (opt.autohidemode === "leave")) {
  904. self.autohidedom = $().add(self.rail);
  905. if (cap.isie8) self.autohidedom = self.autohidedom.add(self.cursor);
  906. if (self.railh) self.autohidedom = self.autohidedom.add(self.railh);
  907. if (self.railh && cap.isie8) self.autohidedom = self.autohidedom.add(self.cursorh);
  908. } else if (opt.autohidemode == "scroll") {
  909. self.autohidedom = $().add(self.rail);
  910. if (self.railh) self.autohidedom = self.autohidedom.add(self.railh);
  911. } else if (opt.autohidemode == "cursor") {
  912. self.autohidedom = $().add(self.cursor);
  913. if (self.railh) self.autohidedom = self.autohidedom.add(self.cursorh);
  914. } else if (opt.autohidemode == "hidden") {
  915. self.autohidedom = false;
  916. self.hide();
  917. self.railslocked = false;
  918. }
  919. if (cap.cantouch || self.istouchcapable || opt.emulatetouch || cap.hasmstouch) {
  920. self.scrollmom = new ScrollMomentumClass2D(self);
  921. var delayedclick = null;
  922. self.ontouchstart = function (e) {
  923. if (self.locked) return false;
  924. //if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false;
  925. if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType === e.MSPOINTER_TYPE_MOUSE)) return false; // need test on surface!!
  926. self.hasmoving = false;
  927. if (self.scrollmom.timer) {
  928. self.triggerScrollEnd();
  929. self.scrollmom.stop();
  930. }
  931. if (!self.railslocked) {
  932. var tg = self.getTarget(e);
  933. if (tg) {
  934. var skp = (/INPUT/i.test(tg.nodeName)) && (/range/i.test(tg.type));
  935. if (skp) return self.stopPropagation(e);
  936. }
  937. var ismouse = (e.type === "mousedown");
  938. if (!("clientX" in e) && ("changedTouches" in e)) {
  939. e.clientX = e.changedTouches[0].clientX;
  940. e.clientY = e.changedTouches[0].clientY;
  941. }
  942. if (self.forcescreen) {
  943. var le = e;
  944. e = {
  945. "original": (e.original) ? e.original : e
  946. };
  947. e.clientX = le.screenX;
  948. e.clientY = le.screenY;
  949. }
  950. self.rail.drag = {
  951. x: e.clientX,
  952. y: e.clientY,
  953. sx: self.scroll.x,
  954. sy: self.scroll.y,
  955. st: self.getScrollTop(),
  956. sl: self.getScrollLeft(),
  957. pt: 2,
  958. dl: false,
  959. tg: tg
  960. };
  961. if (self.ispage || !opt.directionlockdeadzone) {
  962. self.rail.drag.dl = "f";
  963. } else {
  964. var view = {
  965. w: $window.width(),
  966. h: $window.height()
  967. };
  968. var page = self.getContentSize();
  969. var maxh = page.h - view.h;
  970. var maxw = page.w - view.w;
  971. if (self.rail.scrollable && !self.railh.scrollable) self.rail.drag.ck = (maxh > 0) ? "v" : false;
  972. else if (!self.rail.scrollable && self.railh.scrollable) self.rail.drag.ck = (maxw > 0) ? "h" : false;
  973. else self.rail.drag.ck = false;
  974. }
  975. if (opt.emulatetouch && self.isiframe && cap.isie) {
  976. var wp = self.win.position();
  977. self.rail.drag.x += wp.left;
  978. self.rail.drag.y += wp.top;
  979. }
  980. self.hasmoving = false;
  981. self.lastmouseup = false;
  982. self.scrollmom.reset(e.clientX, e.clientY);
  983. if (tg&&ismouse) {
  984. var ip = /INPUT|SELECT|BUTTON|TEXTAREA/i.test(tg.nodeName);
  985. if (!ip) {
  986. if (cap.hasmousecapture) tg.setCapture();
  987. if (opt.emulatetouch) {
  988. if (tg.onclick && !(tg._onclick || false)) { // intercept DOM0 onclick event
  989. tg._onclick = tg.onclick;
  990. tg.onclick = function (e) {
  991. if (self.hasmoving) return false;
  992. tg._onclick.call(this, e);
  993. };
  994. }
  995. return self.cancelEvent(e);
  996. }
  997. return self.stopPropagation(e);
  998. }
  999. if (/SUBMIT|CANCEL|BUTTON/i.test($(tg).attr('type'))) {
  1000. self.preventclick = {
  1001. "tg": tg,
  1002. "click": false
  1003. };
  1004. }
  1005. }
  1006. }
  1007. };
  1008. self.ontouchend = function (e) {
  1009. if (!self.rail.drag) return true;
  1010. if (self.rail.drag.pt == 2) {
  1011. //if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false;
  1012. if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType === e.MSPOINTER_TYPE_MOUSE)) return false;
  1013. self.rail.drag = false;
  1014. var ismouse = (e.type === "mouseup");
  1015. if (self.hasmoving) {
  1016. self.scrollmom.doMomentum();
  1017. self.lastmouseup = true;
  1018. self.hideCursor();
  1019. if (cap.hasmousecapture) _doc.releaseCapture();
  1020. if (ismouse) return self.cancelEvent(e);
  1021. }
  1022. }
  1023. else if (self.rail.drag.pt == 1) {
  1024. return self.onmouseup(e);
  1025. }
  1026. };
  1027. var moveneedoffset = (opt.emulatetouch && self.isiframe && !cap.hasmousecapture);
  1028. var locktollerance = opt.directionlockdeadzone * 0.3 | 0;
  1029. self.ontouchmove = function (e, byiframe) {
  1030. if (!self.rail.drag) return true;
  1031. if (e.targetTouches && opt.preventmultitouchscrolling) {
  1032. if (e.targetTouches.length > 1) return true; // multitouch
  1033. }
  1034. //if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false;
  1035. if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType === e.MSPOINTER_TYPE_MOUSE)) return true;
  1036. if (self.rail.drag.pt == 2) {
  1037. if (("changedTouches" in e)) {
  1038. e.clientX = e.changedTouches[0].clientX;
  1039. e.clientY = e.changedTouches[0].clientY;
  1040. }
  1041. var ofy, ofx;
  1042. ofx = ofy = 0;
  1043. if (moveneedoffset && !byiframe) {
  1044. var wp = self.win.position();
  1045. ofx = -wp.left;
  1046. ofy = -wp.top;
  1047. }
  1048. var fy = e.clientY + ofy;
  1049. var my = (fy - self.rail.drag.y);
  1050. var fx = e.clientX + ofx;
  1051. var mx = (fx - self.rail.drag.x);
  1052. var ny = self.rail.drag.st - my;
  1053. if (self.ishwscroll && opt.bouncescroll) {
  1054. if (ny < 0) {
  1055. ny = Math.round(ny / 2);
  1056. } else if (ny > self.page.maxh) {
  1057. ny = self.page.maxh + Math.round((ny - self.page.maxh) / 2);
  1058. }
  1059. } else {
  1060. if (ny < 0) {
  1061. ny = 0;
  1062. fy = 0;
  1063. }
  1064. else if (ny > self.page.maxh) {
  1065. ny = self.page.maxh;
  1066. fy = 0;
  1067. }
  1068. if (fy === 0 && !self.hasmoving) {
  1069. if (!self.ispage) self.rail.drag = false;
  1070. return true;
  1071. }
  1072. }
  1073. var nx = self.getScrollLeft();
  1074. if (self.railh && self.railh.scrollable) {
  1075. nx = (self.isrtlmode) ? mx - self.rail.drag.sl : self.rail.drag.sl - mx;
  1076. if (self.ishwscroll && opt.bouncescroll) {
  1077. if (nx < 0) {
  1078. nx = Math.round(nx / 2);
  1079. } else if (nx > self.page.maxw) {
  1080. nx = self.page.maxw + Math.round((nx - self.page.maxw) / 2);
  1081. }
  1082. } else {
  1083. if (nx < 0) {
  1084. nx = 0;
  1085. fx = 0;
  1086. }
  1087. if (nx > self.page.maxw) {
  1088. nx = self.page.maxw;
  1089. fx = 0;
  1090. }
  1091. }
  1092. }
  1093. if (!self.hasmoving) {
  1094. if (self.rail.drag.y === e.clientY && self.rail.drag.x === e.clientX) return self.cancelEvent(e); // prevent first useless move event
  1095. var ay = Math.abs(my);
  1096. var ax = Math.abs(mx);
  1097. var dz = opt.directionlockdeadzone;
  1098. if (!self.rail.drag.ck) {
  1099. if (ay > dz && ax > dz) self.rail.drag.dl = "f";
  1100. else if (ay > dz) self.rail.drag.dl = (ax > locktollerance) ? "f" : "v";
  1101. else if (ax > dz) self.rail.drag.dl = (ay > locktollerance) ? "f" : "h";
  1102. }
  1103. else if (self.rail.drag.ck == "v") {
  1104. if (ax > dz && ay <= locktollerance) {
  1105. self.rail.drag = false;
  1106. }
  1107. else if (ay > dz) self.rail.drag.dl = "v";
  1108. }
  1109. else if (self.rail.drag.ck == "h") {
  1110. if (ay > dz && ax <= locktollerance) {
  1111. self.rail.drag = false;
  1112. }
  1113. else if (ax > dz) self.rail.drag.dl = "h";
  1114. }
  1115. if (!self.rail.drag.dl) return self.cancelEvent(e);
  1116. self.triggerScrollStart(e.clientX, e.clientY, 0, 0, 0);
  1117. self.hasmoving = true;
  1118. }
  1119. if (self.preventclick && !self.preventclick.click) {
  1120. self.preventclick.click = self.preventclick.tg.onclick || false;
  1121. self.preventclick.tg.onclick = self.onpreventclick;
  1122. }
  1123. if (self.rail.drag.dl) {
  1124. if (self.rail.drag.dl == "v") nx = self.rail.drag.sl;
  1125. else if (self.rail.drag.dl == "h") ny = self.rail.drag.st;
  1126. }
  1127. self.synched("touchmove", function () {
  1128. if (self.rail.drag && (self.rail.drag.pt == 2)) {
  1129. if (self.prepareTransition) self.resetTransition();
  1130. if (self.rail.scrollable) self.setScrollTop(ny);
  1131. self.scrollmom.update(fx, fy);
  1132. if (self.railh && self.railh.scrollable) {
  1133. self.setScrollLeft(nx);
  1134. self.showCursor(ny, nx);
  1135. } else {
  1136. self.showCursor(ny);
  1137. }
  1138. if (cap.isie10) _doc.selection.clear();
  1139. }
  1140. });
  1141. return self.cancelEvent(e);
  1142. }
  1143. else if (self.rail.drag.pt == 1) { // drag on cursor
  1144. return self.onmousemove(e);
  1145. }
  1146. };
  1147. self.ontouchstartCursor = function (e, hronly) {
  1148. if (self.rail.drag && self.rail.drag.pt != 3) return;
  1149. if (self.locked) return self.cancelEvent(e);
  1150. self.cancelScroll();
  1151. self.rail.drag = {
  1152. x: e.touches[0].clientX,
  1153. y: e.touches[0].clientY,
  1154. sx: self.scroll.x,
  1155. sy: self.scroll.y,
  1156. pt: 3,
  1157. hr: (!!hronly)
  1158. };
  1159. var tg = self.getTarget(e);
  1160. if (!self.ispage && cap.hasmousecapture) tg.setCapture();
  1161. if (self.isiframe && !cap.hasmousecapture) {
  1162. self.saved.csspointerevents = self.doc.css("pointer-events");
  1163. self.css(self.doc, { "pointer-events": "none" });
  1164. }
  1165. return self.cancelEvent(e);
  1166. };
  1167. self.ontouchendCursor = function (e) {
  1168. if (self.rail.drag) {
  1169. if (cap.hasmousecapture) _doc.releaseCapture();
  1170. if (self.isiframe && !cap.hasmousecapture) self.doc.css("pointer-events", self.saved.csspointerevents);
  1171. if (self.rail.drag.pt != 3) return;
  1172. self.rail.drag = false;
  1173. return self.cancelEvent(e);
  1174. }
  1175. };
  1176. self.ontouchmoveCursor = function (e) {
  1177. if (self.rail.drag) {
  1178. if (self.rail.drag.pt != 3) return;
  1179. self.cursorfreezed = true;
  1180. if (self.rail.drag.hr) {
  1181. self.scroll.x = self.rail.drag.sx + (e.touches[0].clientX - self.rail.drag.x);
  1182. if (self.scroll.x < 0) self.scroll.x = 0;
  1183. var mw = self.scrollvaluemaxw;
  1184. if (self.scroll.x > mw) self.scroll.x = mw;
  1185. } else {
  1186. self.scroll.y = self.rail.drag.sy + (e.touches[0].clientY - self.rail.drag.y);
  1187. if (self.scroll.y < 0) self.scroll.y = 0;
  1188. var my = self.scrollvaluemax;
  1189. if (self.scroll.y > my) self.scroll.y = my;
  1190. }
  1191. self.synched('touchmove', function () {
  1192. if (self.rail.drag && (self.rail.drag.pt == 3)) {
  1193. self.showCursor();
  1194. if (self.rail.drag.hr) self.doScrollLeft(Math.round(self.scroll.x * self.scrollratio.x), opt.cursordragspeed);
  1195. else self.doScrollTop(Math.round(self.scroll.y * self.scrollratio.y), opt.cursordragspeed);
  1196. }
  1197. });
  1198. return self.cancelEvent(e);
  1199. }
  1200. };
  1201. }
  1202. self.onmousedown = function (e, hronly) {
  1203. if (self.rail.drag && self.rail.drag.pt != 1) return;
  1204. if (self.railslocked) return self.cancelEvent(e);
  1205. self.cancelScroll();
  1206. self.rail.drag = {
  1207. x: e.clientX,
  1208. y: e.clientY,
  1209. sx: self.scroll.x,
  1210. sy: self.scroll.y,
  1211. pt: 1,
  1212. hr: hronly || false
  1213. };
  1214. var tg = self.getTarget(e);
  1215. if (cap.hasmousecapture) tg.setCapture();
  1216. if (self.isiframe && !cap.hasmousecapture) {
  1217. self.saved.csspointerevents = self.doc.css("pointer-events");
  1218. self.css(self.doc, {
  1219. "pointer-events": "none"
  1220. });
  1221. }
  1222. self.hasmoving = false;
  1223. return self.cancelEvent(e);
  1224. };
  1225. self.onmouseup = function (e) {
  1226. if (self.rail.drag) {
  1227. if (self.rail.drag.pt != 1) return true;
  1228. if (cap.hasmousecapture) _doc.releaseCapture();
  1229. if (self.isiframe && !cap.hasmousecapture) self.doc.css("pointer-events", self.saved.csspointerevents);
  1230. self.rail.drag = false;
  1231. self.cursorfreezed = false;
  1232. if (self.hasmoving) self.triggerScrollEnd();
  1233. return self.cancelEvent(e);
  1234. }
  1235. };
  1236. self.onmousemove = function (e) {
  1237. if (self.rail.drag) {
  1238. if (self.rail.drag.pt !== 1) return;
  1239. if (cap.ischrome && e.which === 0) return self.onmouseup(e);
  1240. self.cursorfreezed = true;
  1241. if (!self.hasmoving) self.triggerScrollStart(e.clientX, e.clientY, 0, 0, 0);
  1242. self.hasmoving = true;
  1243. if (self.rail.drag.hr) {
  1244. self.scroll.x = self.rail.drag.sx + (e.clientX - self.rail.drag.x);
  1245. if (self.scroll.x < 0) self.scroll.x = 0;
  1246. var mw = self.scrollvaluemaxw;
  1247. if (self.scroll.x > mw) self.scroll.x = mw;
  1248. } else {
  1249. self.scroll.y = self.rail.drag.sy + (e.clientY - self.rail.drag.y);
  1250. if (self.scroll.y < 0) self.scroll.y = 0;
  1251. var my = self.scrollvaluemax;
  1252. if (self.scroll.y > my) self.scroll.y = my;
  1253. }
  1254. self.synched('mousemove', function () {
  1255. if (self.cursorfreezed) {
  1256. self.showCursor();
  1257. if (self.rail.drag.hr) {
  1258. self.scrollLeft(Math.round(self.scroll.x * self.scrollratio.x));
  1259. } else {
  1260. self.scrollTop(Math.round(self.scroll.y * self.scrollratio.y));
  1261. }
  1262. }
  1263. });
  1264. return self.cancelEvent(e);
  1265. }
  1266. else {
  1267. self.checkarea = 0;
  1268. }
  1269. };
  1270. if (cap.cantouch || opt.emulatetouch) {
  1271. self.onpreventclick = function (e) {
  1272. if (self.preventclick) {
  1273. self.preventclick.tg.onclick = self.preventclick.click;
  1274. self.preventclick = false;
  1275. return self.cancelEvent(e);
  1276. }
  1277. };
  1278. self.onclick = (cap.isios) ? false : function (e) { // it needs to check IE11 ???
  1279. if (self.lastmouseup) {
  1280. self.lastmouseup = false;
  1281. return self.cancelEvent(e);
  1282. } else {
  1283. return true;
  1284. }
  1285. };
  1286. if (opt.grabcursorenabled && cap.cursorgrabvalue) {
  1287. self.css((self.ispage) ? self.doc : self.win, {
  1288. 'cursor': cap.cursorgrabvalue
  1289. });
  1290. self.css(self.rail, {
  1291. 'cursor': cap.cursorgrabvalue
  1292. });
  1293. }
  1294. } else {
  1295. var checkSelectionScroll = function (e) {
  1296. if (!self.selectiondrag) return;
  1297. if (e) {
  1298. var ww = self.win.outerHeight();
  1299. var df = (e.pageY - self.selectiondrag.top);
  1300. if (df > 0 && df < ww) df = 0;
  1301. if (df >= ww) df -= ww;
  1302. self.selectiondrag.df = df;
  1303. }
  1304. if (self.selectiondrag.df === 0) return;
  1305. var rt = -(self.selectiondrag.df*2/6)|0;
  1306. self.doScrollBy(rt);
  1307. self.debounced("doselectionscroll", function () {
  1308. checkSelectionScroll();
  1309. }, 50);
  1310. };
  1311. if ("getSelection" in _doc) { // A grade - Major browsers
  1312. self.hasTextSelected = function () {
  1313. return (_doc.getSelection().rangeCount > 0);
  1314. };
  1315. } else if ("selection" in _doc) { //IE9-
  1316. self.hasTextSelected = function () {
  1317. return (_doc.selection.type != "None");
  1318. };
  1319. } else {
  1320. self.hasTextSelected = function () { // no support
  1321. return false;
  1322. };
  1323. }
  1324. self.onselectionstart = function (e) {
  1325. // More testing - severe chrome issues
  1326. /*
  1327. if (!self.haswrapper&&(e.which&&e.which==2)) { // fool browser to manage middle button scrolling
  1328. self.win.css({'overflow':'auto'});
  1329. setTimeout(function(){
  1330. self.win.css({'overflow':'hidden'});
  1331. },10);
  1332. return true;
  1333. }
  1334. */
  1335. if (self.ispage) return;
  1336. self.selectiondrag = self.win.offset();
  1337. };
  1338. self.onselectionend = function (e) {
  1339. self.selectiondrag = false;
  1340. };
  1341. self.onselectiondrag = function (e) {
  1342. if (!self.selectiondrag) return;
  1343. if (self.hasTextSelected()) self.debounced("selectionscroll", function () {
  1344. checkSelectionScroll(e);
  1345. }, 250);
  1346. };
  1347. }
  1348. if (cap.hasw3ctouch) { //IE11+
  1349. self.css((self.ispage) ? $("html") : self.win, { 'touch-action': 'none' });
  1350. self.css(self.rail, {
  1351. 'touch-action': 'none'
  1352. });
  1353. self.css(self.cursor, {
  1354. 'touch-action': 'none'
  1355. });
  1356. self.bind(self.win, "pointerdown", self.ontouchstart);
  1357. self.bind(_doc, "pointerup", self.ontouchend);
  1358. self.delegate(_doc, "pointermove", self.ontouchmove);
  1359. } else if (cap.hasmstouch) { //IE10
  1360. self.css((self.ispage) ? $("html") : self.win, { '-ms-touch-action': 'none' });
  1361. self.css(self.rail, {
  1362. '-ms-touch-action': 'none'
  1363. });
  1364. self.css(self.cursor, {
  1365. '-ms-touch-action': 'none'
  1366. });
  1367. self.bind(self.win, "MSPointerDown", self.ontouchstart);
  1368. self.bind(_doc, "MSPointerUp", self.ontouchend);
  1369. self.delegate(_doc, "MSPointerMove", self.ontouchmove);
  1370. self.bind(self.cursor, "MSGestureHold", function (e) {
  1371. e.preventDefault();
  1372. });
  1373. self.bind(self.cursor, "contextmenu", function (e) {
  1374. e.preventDefault();
  1375. });
  1376. } else if (cap.cantouch) { // smartphones/touch devices
  1377. self.bind(self.win, "touchstart", self.ontouchstart, false, true);
  1378. self.bind(_doc, "touchend", self.ontouchend, false, true);
  1379. self.bind(_doc, "touchcancel", self.ontouchend, false, true);
  1380. self.delegate(_doc, "touchmove", self.ontouchmove, false, true);
  1381. }
  1382. if (opt.emulatetouch) {
  1383. self.bind(self.win, "mousedown", self.ontouchstart, false, true);
  1384. self.bind(_doc, "mouseup", self.ontouchend, false, true);
  1385. self.bind(_doc, "mousemove", self.ontouchmove, false, true);
  1386. }
  1387. if (opt.cursordragontouch || (!cap.cantouch && !opt.emulatetouch)) {
  1388. self.rail.css({
  1389. cursor: "default"
  1390. });
  1391. self.railh && self.railh.css({
  1392. cursor: "default"
  1393. });
  1394. self.jqbind(self.rail, "mouseenter", function () {
  1395. if (!self.ispage && !self.win.is(":visible")) return false;
  1396. if (self.canshowonmouseevent) self.showCursor();
  1397. self.rail.active = true;
  1398. });
  1399. self.jqbind(self.rail, "mouseleave", function () {
  1400. self.rail.active = false;
  1401. if (!self.rail.drag) self.hideCursor();
  1402. });
  1403. if (opt.sensitiverail) {
  1404. self.bind(self.rail, "click", function (e) {
  1405. self.doRailClick(e, false, false);
  1406. });
  1407. self.bind(self.rail, "dblclick", function (e) {
  1408. self.doRailClick(e, true, false);
  1409. });
  1410. self.bind(self.cursor, "click", function (e) {
  1411. self.cancelEvent(e);
  1412. });
  1413. self.bind(self.cursor, "dblclick", function (e) {
  1414. self.cancelEvent(e);
  1415. });
  1416. }
  1417. if (self.railh) {
  1418. self.jqbind(self.railh, "mouseenter", function () {
  1419. if (!self.ispage && !self.win.is(":visible")) return false;
  1420. if (self.canshowonmouseevent) self.showCursor();
  1421. self.rail.active = true;
  1422. });
  1423. self.jqbind(self.railh, "mouseleave", function () {
  1424. self.rail.active = false;
  1425. if (!self.rail.drag) self.hideCursor();
  1426. });
  1427. if (opt.sensitiverail) {
  1428. self.bind(self.railh, "click", function (e) {
  1429. self.doRailClick(e, false, true);
  1430. });
  1431. self.bind(self.railh, "dblclick", function (e) {
  1432. self.doRailClick(e, true, true);
  1433. });
  1434. self.bind(self.cursorh, "click", function (e) {
  1435. self.cancelEvent(e);
  1436. });
  1437. self.bind(self.cursorh, "dblclick", function (e) {
  1438. self.cancelEvent(e);
  1439. });
  1440. }
  1441. }
  1442. }
  1443. if (opt.cursordragontouch && (this.istouchcapable || cap.cantouch)) {
  1444. self.bind(self.cursor, "touchstart", self.ontouchstartCursor);
  1445. self.bind(self.cursor, "touchmove", self.ontouchmoveCursor);
  1446. self.bind(self.cursor, "touchend", self.ontouchendCursor);
  1447. self.cursorh && self.bind(self.cursorh, "touchstart", function (e) {
  1448. self.ontouchstartCursor(e, true);
  1449. });
  1450. self.cursorh && self.bind(self.cursorh, "touchmove", self.ontouchmoveCursor);
  1451. self.cursorh && self.bind(self.cursorh, "touchend", self.ontouchendCursor);
  1452. }
  1453. // if (!cap.cantouch && !opt.emulatetouch) {
  1454. if (!opt.emulatetouch && !cap.isandroid && !cap.isios) {
  1455. self.bind((cap.hasmousecapture) ? self.win : _doc, "mouseup", self.onmouseup);
  1456. self.bind(_doc, "mousemove", self.onmousemove);
  1457. if (self.onclick) self.bind(_doc, "click", self.onclick);
  1458. self.bind(self.cursor, "mousedown", self.onmousedown);
  1459. self.bind(self.cursor, "mouseup", self.onmouseup);
  1460. if (self.railh) {
  1461. self.bind(self.cursorh, "mousedown", function (e) {
  1462. self.onmousedown(e, true);
  1463. });
  1464. self.bind(self.cursorh, "mouseup", self.onmouseup);
  1465. }
  1466. if (!self.ispage && opt.enablescrollonselection) {
  1467. self.bind(self.win[0], "mousedown", self.onselectionstart);
  1468. self.bind(_doc, "mouseup", self.onselectionend);
  1469. self.bind(self.cursor, "mouseup", self.onselectionend);
  1470. if (self.cursorh) self.bind(self.cursorh, "mouseup", self.onselectionend);
  1471. self.bind(_doc, "mousemove", self.onselectiondrag);
  1472. }
  1473. if (self.zoom) {
  1474. self.jqbind(self.zoom, "mouseenter", function () {
  1475. if (self.canshowonmouseevent) self.showCursor();
  1476. self.rail.active = true;
  1477. });
  1478. self.jqbind(self.zoom, "mouseleave", function () {
  1479. self.rail.active = false;
  1480. if (!self.rail.drag) self.hideCursor();
  1481. });
  1482. }
  1483. } else {
  1484. self.bind((cap.hasmousecapture) ? self.win : _doc, "mouseup", self.ontouchend);
  1485. if (self.onclick) self.bind(_doc, "click", self.onclick);
  1486. if (opt.cursordragontouch) {
  1487. self.bind(self.cursor, "mousedown", self.onmousedown);
  1488. self.bind(self.cursor, "mouseup", self.onmouseup);
  1489. self.cursorh && self.bind(self.cursorh, "mousedown", function (e) {
  1490. self.onmousedown(e, true);
  1491. });
  1492. self.cursorh && self.bind(self.cursorh, "mouseup", self.onmouseup);
  1493. } else {
  1494. self.bind(self.rail, "mousedown", function (e) { e.preventDefault(); }); // prevent text selection
  1495. self.railh && self.bind(self.railh, "mousedown", function (e) { e.preventDefault(); });
  1496. }
  1497. }
  1498. if (opt.enablemousewheel) {
  1499. if (!self.isiframe) self.mousewheel((cap.isie && self.ispage) ? _doc : self.win, self.onmousewheel);
  1500. self.mousewheel(self.rail, self.onmousewheel);
  1501. if (self.railh) self.mousewheel(self.railh, self.onmousewheelhr);
  1502. }
  1503. if (!self.ispage && !cap.cantouch && !(/HTML|^BODY/.test(self.win[0].nodeName))) {
  1504. if (!self.win.attr("tabindex")) self.win.attr({
  1505. "tabindex": ++tabindexcounter
  1506. });
  1507. self.bind(self.win, "focus", function (e) { // better using native events
  1508. domfocus = (self.getTarget(e)).id || self.getTarget(e) || false;
  1509. self.hasfocus = true;
  1510. if (self.canshowonmouseevent) self.noticeCursor();
  1511. });
  1512. self.bind(self.win, "blur", function (e) { // *
  1513. domfocus = false;
  1514. self.hasfocus = false;
  1515. });
  1516. self.bind(self.win, "mouseenter", function (e) { // *
  1517. mousefocus = (self.getTarget(e)).id || self.getTarget(e) || false;
  1518. self.hasmousefocus = true;
  1519. if (self.canshowonmouseevent) self.noticeCursor();
  1520. });
  1521. self.bind(self.win, "mouseleave", function (e) { // *
  1522. mousefocus = false;
  1523. self.hasmousefocus = false;
  1524. if (!self.rail.drag) self.hideCursor();
  1525. });
  1526. }
  1527. //Thanks to http://www.quirksmode.org !!
  1528. self.onkeypress = function (e) {
  1529. if (self.railslocked && self.page.maxh === 0) return true;
  1530. e = e || _win.event;
  1531. var tg = self.getTarget(e);
  1532. if (tg && /INPUT|TEXTAREA|SELECT|OPTION/.test(tg.nodeName)) {
  1533. var tp = tg.getAttribute('type') || tg.type || false;
  1534. if ((!tp) || !(/submit|button|cancel/i.tp)) return true;
  1535. }
  1536. if ($(tg).attr('contenteditable')) return true;
  1537. if (self.hasfocus || (self.hasmousefocus && !domfocus) || (self.ispage && !domfocus && !mousefocus)) {
  1538. var key = e.keyCode;
  1539. if (self.railslocked && key != 27) return self.cancelEvent(e);
  1540. var ctrl = e.ctrlKey || false;
  1541. var shift = e.shiftKey || false;
  1542. var ret = false;
  1543. switch (key) {
  1544. case 38:
  1545. case 63233: //safari
  1546. self.doScrollBy(24 * 3);
  1547. ret = true;
  1548. break;
  1549. case 40:
  1550. case 63235: //safari
  1551. self.doScrollBy(-24 * 3);
  1552. ret = true;
  1553. break;
  1554. case 37:
  1555. case 63232: //safari
  1556. if (self.railh) {
  1557. (ctrl) ? self.doScrollLeft(0) : self.doScrollLeftBy(24 * 3);
  1558. ret = true;
  1559. }
  1560. break;
  1561. case 39:
  1562. case 63234: //safari
  1563. if (self.railh) {
  1564. (ctrl) ? self.doScrollLeft(self.page.maxw) : self.doScrollLeftBy(-24 * 3);
  1565. ret = true;
  1566. }
  1567. break;
  1568. case 33:
  1569. case 63276: // safari
  1570. self.doScrollBy(self.view.h);
  1571. ret = true;
  1572. break;
  1573. case 34:
  1574. case 63277: // safari
  1575. self.doScrollBy(-self.view.h);
  1576. ret = true;
  1577. break;
  1578. case 36:
  1579. case 63273: // safari
  1580. (self.railh && ctrl) ? self.doScrollPos(0, 0) : self.doScrollTo(0);
  1581. ret = true;
  1582. break;
  1583. case 35:
  1584. case 63275: // safari
  1585. (self.railh && ctrl) ? self.doScrollPos(self.page.maxw, self.page.maxh) : self.doScrollTo(self.page.maxh);
  1586. ret = true;
  1587. break;
  1588. case 32:
  1589. if (opt.spacebarenabled) {
  1590. (shift) ? self.doScrollBy(self.view.h) : self.doScrollBy(-self.view.h);
  1591. ret = true;
  1592. }
  1593. break;
  1594. case 27: // ESC
  1595. if (self.zoomactive) {
  1596. self.doZoom();
  1597. ret = true;
  1598. }
  1599. break;
  1600. }
  1601. if (ret) return self.cancelEvent(e);
  1602. }
  1603. };
  1604. if (opt.enablekeyboard) self.bind(_doc, (cap.isopera && !cap.isopera12) ? "keypress" : "keydown", self.onkeypress);
  1605. self.bind(_doc, "keydown", function (e) {
  1606. var ctrl = e.ctrlKey || false;
  1607. if (ctrl) self.wheelprevented = true;
  1608. });
  1609. self.bind(_doc, "keyup", function (e) {
  1610. var ctrl = e.ctrlKey || false;
  1611. if (!ctrl) self.wheelprevented = false;
  1612. });
  1613. self.bind(_win, "blur", function (e) {
  1614. self.wheelprevented = false;
  1615. });
  1616. self.bind(_win, 'resize', self.onscreenresize);
  1617. self.bind(_win, 'orientationchange', self.onscreenresize);
  1618. self.bind(_win, "load", self.lazyResize);
  1619. if (cap.ischrome && !self.ispage && !self.haswrapper) { //chrome void scrollbar bug - it persists in version 26
  1620. var tmp = self.win.attr("style");
  1621. var ww = parseFloat(self.win.css("width")) + 1;
  1622. self.win.css('width', ww);
  1623. self.synched("chromefix", function () {
  1624. self.win.attr("style", tmp);
  1625. });
  1626. }
  1627. // Trying a cross-browser implementation - good luck!
  1628. self.onAttributeChange = function (e) {
  1629. self.lazyResize(self.isieold ? 250 : 30);
  1630. };
  1631. if (opt.enableobserver) {
  1632. if ((!self.isie11) && (ClsMutationObserver !== false)) { // IE11 crashes #568
  1633. self.observerbody = new ClsMutationObserver(function (mutations) {
  1634. mutations.forEach(function (mut) {
  1635. if (mut.type == "attributes") {
  1636. return ($body.hasClass("modal-open") && $body.hasClass("modal-dialog") && !$.contains($('.modal-dialog')[0], self.doc[0])) ? self.hide() : self.show(); // Support for Bootstrap modal; Added check if the nice scroll element is inside a modal
  1637. }
  1638. });
  1639. if (self.me.clientWidth != self.page.width || self.me.clientHeight != self.page.height) return self.lazyResize(30);
  1640. });
  1641. self.observerbody.observe(_doc.body, {
  1642. childList: true,
  1643. subtree: true,
  1644. characterData: false,
  1645. attributes: true,
  1646. attributeFilter: ['class']
  1647. });
  1648. }
  1649. if (!self.ispage && !self.haswrapper) {
  1650. var _dom = self.win[0];
  1651. // redesigned MutationObserver for Chrome18+/Firefox14+/iOS6+ with support for: remove div, add/remove content
  1652. if (ClsMutationObserver !== false) {
  1653. self.observer = new ClsMutationObserver(function (mutations) {
  1654. mutations.forEach(self.onAttributeChange);
  1655. });
  1656. self.observer.observe(_dom, {
  1657. childList: true,
  1658. characterData: false,
  1659. attributes: true,
  1660. subtree: false
  1661. });
  1662. self.observerremover = new ClsMutationObserver(function (mutations) {
  1663. mutations.forEach(function (mo) {
  1664. if (mo.removedNodes.length > 0) {
  1665. for (var dd in mo.removedNodes) {
  1666. if (!!self && (mo.removedNodes[dd] === _dom)) return self.remove();
  1667. }
  1668. }
  1669. });
  1670. });
  1671. self.observerremover.observe(_dom.parentNode, {
  1672. childList: true,
  1673. characterData: false,
  1674. attributes: false,
  1675. subtree: false
  1676. });
  1677. } else {
  1678. self.bind(_dom, (cap.isie && !cap.isie9) ? "propertychange" : "DOMAttrModified", self.onAttributeChange);
  1679. if (cap.isie9) _dom.attachEvent("onpropertychange", self.onAttributeChange); //IE9 DOMAttrModified bug
  1680. self.bind(_dom, "DOMNodeRemoved", function (e) {
  1681. if (e.target === _dom) self.remove();
  1682. });
  1683. }
  1684. }
  1685. }
  1686. //
  1687. if (!self.ispage && opt.boxzoom) self.bind(_win, "resize", self.resizeZoom);
  1688. if (self.istextarea) {
  1689. self.bind(self.win, "keydown", self.lazyResize);
  1690. self.bind(self.win, "mouseup", self.lazyResize);
  1691. }
  1692. self.lazyResize(30);
  1693. }
  1694. if (this.doc[0].nodeName == 'IFRAME') {
  1695. var oniframeload = function () {
  1696. self.iframexd = false;
  1697. var doc;
  1698. try {
  1699. doc = 'contentDocument' in this ? this.contentDocument : this.contentWindow._doc;
  1700. var a = doc.domain;
  1701. } catch (e) {
  1702. self.iframexd = true;
  1703. doc = false;
  1704. }
  1705. if (self.iframexd) {
  1706. if ("console" in _win) console.log('NiceScroll error: policy restriced iframe');
  1707. return true; //cross-domain - I can't manage this
  1708. }
  1709. self.forcescreen = true;
  1710. if (self.isiframe) {
  1711. self.iframe = {
  1712. "doc": $(doc),
  1713. "html": self.doc.contents().find('html')[0],
  1714. "body": self.doc.contents().find('body')[0]
  1715. };
  1716. self.getContentSize = function () {
  1717. return {
  1718. w: Math.max(self.iframe.html.scrollWidth, self.iframe.body.scrollWidth),
  1719. h: Math.max(self.iframe.html.scrollHeight, self.iframe.body.scrollHeight)
  1720. };
  1721. };
  1722. self.docscroll = $(self.iframe.body);
  1723. }
  1724. if (!cap.isios && opt.iframeautoresize && !self.isiframe) {
  1725. self.win.scrollTop(0); // reset position
  1726. self.doc.height(""); //reset height to fix browser bug
  1727. var hh = Math.max(doc.getElementsByTagName('html')[0].scrollHeight, doc.body.scrollHeight);
  1728. self.doc.height(hh);
  1729. }
  1730. self.lazyResize(30);
  1731. self.css($(self.iframe.body), _scrollyhidden);
  1732. if (cap.isios && self.haswrapper) {
  1733. self.css($(doc.body), {
  1734. '-webkit-transform': 'translate3d(0,0,0)'
  1735. }); // avoid iFrame content clipping - thanks to http://blog.derraab.com/2012/04/02/avoid-iframe-content-clipping-with-css-transform-on-ios/
  1736. }
  1737. if ('contentWindow' in this) {
  1738. self.bind(this.contentWindow, "scroll", self.onscroll); //IE8 & minor
  1739. } else {
  1740. self.bind(doc, "scroll", self.onscroll);
  1741. }
  1742. if (opt.enablemousewheel) {
  1743. self.mousewheel(doc, self.onmousewheel);
  1744. }
  1745. if (opt.enablekeyboard) self.bind(doc, (cap.isopera) ? "keypress" : "keydown", self.onkeypress);
  1746. if (cap.cantouch) {
  1747. self.bind(doc, "touchstart", self.ontouchstart);
  1748. self.bind(doc, "touchmove", self.ontouchmove);
  1749. }
  1750. else if (opt.emulatetouch) {
  1751. self.bind(doc, "mousedown", self.ontouchstart);
  1752. self.bind(doc, "mousemove", function (e) {
  1753. return self.ontouchmove(e, true);
  1754. });
  1755. if (opt.grabcursorenabled && cap.cursorgrabvalue) self.css($(doc.body), {
  1756. 'cursor': cap.cursorgrabvalue
  1757. });
  1758. }
  1759. self.bind(doc, "mouseup", self.ontouchend);
  1760. if (self.zoom) {
  1761. if (opt.dblclickzoom) self.bind(doc, 'dblclick', self.doZoom);
  1762. if (self.ongesturezoom) self.bind(doc, "gestureend", self.ongesturezoom);
  1763. }
  1764. };
  1765. if (this.doc[0].readyState && this.doc[0].readyState === "complete") {
  1766. setTimeout(function () {
  1767. oniframeload.call(self.doc[0], false);
  1768. }, 500);
  1769. }
  1770. self.bind(this.doc, "load", oniframeload);
  1771. }
  1772. };
  1773. this.showCursor = function (py, px) {
  1774. if (self.cursortimeout) {
  1775. clearTimeout(self.cursortimeout);
  1776. self.cursortimeout = 0;
  1777. }
  1778. if (!self.rail) return;
  1779. if (self.autohidedom) {
  1780. self.autohidedom.stop().css({
  1781. opacity: opt.cursoropacitymax
  1782. });
  1783. self.cursoractive = true;
  1784. }
  1785. if (!self.rail.drag || self.rail.drag.pt != 1) {
  1786. if (py !== undefined && py !== false) {
  1787. self.scroll.y = (py / self.scrollratio.y) | 0;
  1788. }
  1789. if (px !== undefined) {
  1790. self.scroll.x = (px / self.scrollratio.x) | 0;
  1791. }
  1792. }
  1793. self.cursor.css({
  1794. height: self.cursorheight,
  1795. top: self.scroll.y
  1796. });
  1797. if (self.cursorh) {
  1798. var lx = (self.hasreversehr) ? self.scrollvaluemaxw - self.scroll.x : self.scroll.x;
  1799. self.cursorh.css({
  1800. width: self.cursorwidth,
  1801. left: (!self.rail.align && self.rail.visibility) ? lx + self.rail.width : lx
  1802. });
  1803. self.cursoractive = true;
  1804. }
  1805. if (self.zoom) self.zoom.stop().css({
  1806. opacity: opt.cursoropacitymax
  1807. });
  1808. };
  1809. this.hideCursor = function (tm) {
  1810. if (self.cursortimeout) return;
  1811. if (!self.rail) return;
  1812. if (!self.autohidedom) return;
  1813. if (self.hasmousefocus && opt.autohidemode === "leave") return;
  1814. self.cursortimeout = setTimeout(function () {
  1815. if (!self.rail.active || !self.showonmouseevent) {
  1816. self.autohidedom.stop().animate({
  1817. opacity: opt.cursoropacitymin
  1818. });
  1819. if (self.zoom) self.zoom.stop().animate({
  1820. opacity: opt.cursoropacitymin
  1821. });
  1822. self.cursoractive = false;
  1823. }
  1824. self.cursortimeout = 0;
  1825. }, tm || opt.hidecursordelay);
  1826. };
  1827. this.noticeCursor = function (tm, py, px) {
  1828. self.showCursor(py, px);
  1829. if (!self.rail.active) self.hideCursor(tm);
  1830. };
  1831. this.getContentSize =
  1832. (self.ispage) ?
  1833. function () {
  1834. return {
  1835. w: Math.max(_doc.body.scrollWidth, _doc.documentElement.scrollWidth),
  1836. h: Math.max(_doc.body.scrollHeight, _doc.documentElement.scrollHeight)
  1837. };
  1838. } : (self.haswrapper) ?
  1839. function () {
  1840. return {
  1841. w: self.doc[0].offsetWidth,
  1842. h: self.doc[0].offsetHeight
  1843. };
  1844. } : function () {
  1845. return {
  1846. w: self.docscroll[0].scrollWidth,
  1847. h: self.docscroll[0].scrollHeight
  1848. };
  1849. };
  1850. this.onResize = function (e, page) {
  1851. if (!self || !self.win) return false;
  1852. var premaxh = self.page.maxh,
  1853. premaxw = self.page.maxw,
  1854. previewh = self.view.h,
  1855. previeww = self.view.w;
  1856. self.view = {
  1857. w: (self.ispage) ? self.win.width() : self.win[0].clientWidth,
  1858. h: (self.ispage) ? self.win.height() : self.win[0].clientHeight
  1859. };
  1860. self.page = (page) ? page : self.getContentSize();
  1861. self.page.maxh = Math.max(0, self.page.h - self.view.h);
  1862. self.page.maxw = Math.max(0, self.page.w - self.view.w);
  1863. if ((self.page.maxh == premaxh) && (self.page.maxw == premaxw) && (self.view.w == previeww) && (self.view.h == previewh)) {
  1864. // test position
  1865. if (!self.ispage) {
  1866. var pos = self.win.offset();
  1867. if (self.lastposition) {
  1868. var lst = self.lastposition;
  1869. if ((lst.top == pos.top) && (lst.left == pos.left)) return self; //nothing to do
  1870. }
  1871. self.lastposition = pos;
  1872. } else {
  1873. return self; //nothing to do
  1874. }
  1875. }
  1876. if (self.page.maxh === 0) {
  1877. self.hideRail();
  1878. self.scrollvaluemax = 0;
  1879. self.scroll.y = 0;
  1880. self.scrollratio.y = 0;
  1881. self.cursorheight = 0;
  1882. self.setScrollTop(0);
  1883. if (self.rail) self.rail.scrollable = false;
  1884. } else {
  1885. self.page.maxh -= (opt.railpadding.top + opt.railpadding.bottom);
  1886. self.rail.scrollable = true;
  1887. }
  1888. if (self.page.maxw === 0) {
  1889. self.hideRailHr();
  1890. self.scrollvaluemaxw = 0;
  1891. self.scroll.x = 0;
  1892. self.scrollratio.x = 0;
  1893. self.cursorwidth = 0;
  1894. self.setScrollLeft(0);
  1895. if (self.railh) {
  1896. self.railh.scrollable = false;
  1897. }
  1898. } else {
  1899. self.page.maxw -= (opt.railpadding.left + opt.railpadding.right);
  1900. if (self.railh) self.railh.scrollable = (opt.horizrailenabled);
  1901. }
  1902. self.railslocked = (self.locked) || ((self.page.maxh === 0) && (self.page.maxw === 0));
  1903. if (self.railslocked) {
  1904. if (!self.ispage) self.updateScrollBar(self.view);
  1905. return false;
  1906. }
  1907. if (!self.hidden) {
  1908. if (!self.rail.visibility) self.showRail();
  1909. if (self.railh && !self.railh.visibility) self.showRailHr();
  1910. }
  1911. if (self.istextarea && self.win.css('resize') && self.win.css('resize') != 'none') self.view.h -= 20;
  1912. self.cursorheight = Math.min(self.view.h, Math.round(self.view.h * (self.view.h / self.page.h)));
  1913. self.cursorheight = (opt.cursorfixedheight) ? opt.cursorfixedheight : Math.max(opt.cursorminheight, self.cursorheight);
  1914. self.cursorwidth = Math.min(self.view.w, Math.round(self.view.w * (self.view.w / self.page.w)));
  1915. self.cursorwidth = (opt.cursorfixedheight) ? opt.cursorfixedheight : Math.max(opt.cursorminheight, self.cursorwidth);
  1916. self.scrollvaluemax = self.view.h - self.cursorheight - (opt.railpadding.top + opt.railpadding.bottom);
  1917. if (!self.hasborderbox) self.scrollvaluemax -= self.cursor[0].offsetHeight - self.cursor[0].clientHeight;
  1918. if (self.railh) {
  1919. self.railh.width = (self.page.maxh > 0) ? (self.view.w - self.rail.width) : self.view.w;
  1920. self.scrollvaluemaxw = self.railh.width - self.cursorwidth - (opt.railpadding.left + opt.railpadding.right);
  1921. }
  1922. if (!self.ispage) self.updateScrollBar(self.view);
  1923. self.scrollratio = {
  1924. x: (self.page.maxw / self.scrollvaluemaxw),
  1925. y: (self.page.maxh / self.scrollvaluemax)
  1926. };
  1927. var sy = self.getScrollTop();
  1928. if (sy > self.page.maxh) {
  1929. self.doScrollTop(self.page.maxh);
  1930. } else {
  1931. self.scroll.y = (self.getScrollTop() / self.scrollratio.y) | 0;
  1932. self.scroll.x = (self.getScrollLeft() / self.scrollratio.x) | 0;
  1933. if (self.cursoractive) self.noticeCursor();
  1934. }
  1935. if (self.scroll.y && (self.getScrollTop() === 0)) self.doScrollTo((self.scroll.y * self.scrollratio.y)|0);
  1936. return self;
  1937. };
  1938. this.resize = self.onResize;
  1939. var hlazyresize = 0;
  1940. this.onscreenresize = function(e) {
  1941. clearTimeout(hlazyresize);
  1942. var hiderails = (!self.ispage && !self.haswrapper);
  1943. if (hiderails) self.hideRails();
  1944. hlazyresize = setTimeout(function () {
  1945. if (self) {
  1946. if (hiderails) self.showRails();
  1947. self.resize();
  1948. }
  1949. hlazyresize=0;
  1950. }, 120);
  1951. };
  1952. this.lazyResize = function (tm) { // event debounce
  1953. clearTimeout(hlazyresize);
  1954. tm = isNaN(tm) ? 240 : tm;
  1955. hlazyresize = setTimeout(function () {
  1956. self && self.resize();
  1957. hlazyresize=0;
  1958. }, tm);
  1959. return self;
  1960. };
  1961. // derived by MDN https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/wheel
  1962. function _modernWheelEvent(dom, name, fn, bubble) {
  1963. self._bind(dom, name, function (e) {
  1964. e = e || _win.event;
  1965. var event = {
  1966. original: e,
  1967. target: e.target || e.srcElement,
  1968. type: "wheel",
  1969. deltaMode: e.type == "MozMousePixelScroll" ? 0 : 1,
  1970. deltaX: 0,
  1971. deltaZ: 0,
  1972. preventDefault: function () {
  1973. e.preventDefault ? e.preventDefault() : e.returnValue = false;
  1974. return false;
  1975. },
  1976. stopImmediatePropagation: function () {
  1977. (e.stopImmediatePropagation) ? e.stopImmediatePropagation() : e.cancelBubble = true;
  1978. }
  1979. };
  1980. if (name == "mousewheel") {
  1981. e.wheelDeltaX && (event.deltaX = -1 / 40 * e.wheelDeltaX);
  1982. e.wheelDeltaY && (event.deltaY = -1 / 40 * e.wheelDeltaY);
  1983. !event.deltaY && !event.deltaX && (event.deltaY = -1 / 40 * e.wheelDelta);
  1984. } else {
  1985. event.deltaY = e.detail;
  1986. }
  1987. return fn.call(dom, event);
  1988. }, bubble);
  1989. }
  1990. this.jqbind = function (dom, name, fn) { // use jquery bind for non-native events (mouseenter/mouseleave)
  1991. self.events.push({
  1992. e: dom,
  1993. n: name,
  1994. f: fn,
  1995. q: true
  1996. });
  1997. $(dom).on(name, fn);
  1998. };
  1999. this.mousewheel = function (dom, fn, bubble) { // bind mousewheel
  2000. var el = ("jquery" in dom) ? dom[0] : dom;
  2001. if ("onwheel" in _doc.createElement("div")) { // Modern browsers support "wheel"
  2002. self._bind(el, "wheel", fn, bubble || false);
  2003. } else {
  2004. var wname = (_doc.onmousewheel !== undefined) ? "mousewheel" : "DOMMouseScroll"; // older Webkit+IE support or older Firefox
  2005. _modernWheelEvent(el, wname, fn, bubble || false);
  2006. if (wname == "DOMMouseScroll") _modernWheelEvent(el, "MozMousePixelScroll", fn, bubble || false); // Firefox legacy
  2007. }
  2008. };
  2009. var passiveSupported = false;
  2010. if (cap.haseventlistener) { // W3C standard event model
  2011. // thanks to https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
  2012. try { var options = Object.defineProperty({}, "passive", { get: function () { passiveSupported = !0; } }); _win.addEventListener("test", null, options); } catch (err) { }
  2013. this.stopPropagation = function (e) {
  2014. if (!e) return false;
  2015. e = (e.original) ? e.original : e;
  2016. e.stopPropagation();
  2017. return false;
  2018. };
  2019. this.cancelEvent = function(e) {
  2020. if (e.cancelable) e.preventDefault();
  2021. e.stopImmediatePropagation();
  2022. if (e.preventManipulation) e.preventManipulation(); // IE10+
  2023. return false;
  2024. };
  2025. } else {
  2026. // inspired from https://gist.github.com/jonathantneal/2415137
  2027. Event.prototype.preventDefault = function () {
  2028. this.returnValue = false;
  2029. };
  2030. Event.prototype.stopPropagation = function () {
  2031. this.cancelBubble = true;
  2032. };
  2033. _win.constructor.prototype.addEventListener = _doc.constructor.prototype.addEventListener = Element.prototype.addEventListener = function (type, listener, useCapture) {
  2034. this.attachEvent("on" + type, listener);
  2035. };
  2036. _win.constructor.prototype.removeEventListener = _doc.constructor.prototype.removeEventListener = Element.prototype.removeEventListener = function (type, listener, useCapture) {
  2037. this.detachEvent("on" + type, listener);
  2038. };
  2039. // Thanks to http://www.switchonthecode.com !!
  2040. this.cancelEvent = function (e) {
  2041. e = e || _win.event;
  2042. if (e) {
  2043. e.cancelBubble = true;
  2044. e.cancel = true;
  2045. e.returnValue = false;
  2046. }
  2047. return false;
  2048. };
  2049. this.stopPropagation = function (e) {
  2050. e = e || _win.event;
  2051. if (e) e.cancelBubble = true;
  2052. return false;
  2053. };
  2054. }
  2055. this.delegate = function (dom, name, fn, bubble, active) {
  2056. var de = delegatevents[name] || false;
  2057. if (!de) {
  2058. de = {
  2059. a: [],
  2060. l: [],
  2061. f: function (e) {
  2062. var lst = de.l, l = lst.length - 1;
  2063. var r = false;
  2064. for (var a = l; a >= 0; a--) {
  2065. r = lst[a].call(e.target, e);
  2066. if (r === false) return false;
  2067. }
  2068. return r;
  2069. }
  2070. };
  2071. self.bind(dom, name, de.f, bubble, active);
  2072. delegatevents[name] = de;
  2073. }
  2074. if (self.ispage) {
  2075. de.a = [self.id].concat(de.a);
  2076. de.l = [fn].concat(de.l);
  2077. } else {
  2078. de.a.push(self.id);
  2079. de.l.push(fn);
  2080. }
  2081. };
  2082. this.undelegate = function (dom, name, fn, bubble, active) {
  2083. var de = delegatevents[name]||false;
  2084. if (de) {
  2085. for (var a=0,l=de.l.length;a<l;a++) {
  2086. if (de.a[a] === self.id) {
  2087. de.a.splice(a);
  2088. de.l.splice(a);
  2089. if (de.a.length===0) {
  2090. self._unbind(dom,name,de.l.f);
  2091. delegatevents[name] = null;
  2092. }
  2093. }
  2094. }
  2095. }
  2096. };
  2097. this.bind = function (dom, name, fn, bubble, active) {
  2098. var el = ("jquery" in dom) ? dom[0] : dom;
  2099. self._bind(el, name, fn, bubble || false, active || false);
  2100. };
  2101. this._bind = function (el, name, fn, bubble, active) { // primitive bind
  2102. self.events.push({
  2103. e: el,
  2104. n: name,
  2105. f: fn,
  2106. b: bubble,
  2107. q: false
  2108. });
  2109. (passiveSupported && active) ? el.addEventListener(name, fn, { passive: false, capture: bubble }) : el.addEventListener(name, fn, bubble || false);
  2110. };
  2111. this._unbind = function (el, name, fn, bub) { // primitive unbind
  2112. if (delegatevents[name]) self.undelegate(el, name, fn, bub);
  2113. else el.removeEventListener(name, fn, bub);
  2114. };
  2115. this.unbindAll = function () {
  2116. for (var a = 0; a < self.events.length; a++) {
  2117. var r = self.events[a];
  2118. (r.q) ? r.e.unbind(r.n, r.f) : self._unbind(r.e, r.n, r.f, r.b);
  2119. }
  2120. };
  2121. this.showRails = function () {
  2122. return self.showRail().showRailHr();
  2123. };
  2124. this.showRail = function () {
  2125. if ((self.page.maxh !== 0) && (self.ispage || self.win.css('display') != 'none')) {
  2126. //self.visibility = true;
  2127. self.rail.visibility = true;
  2128. self.rail.css('display', 'block');
  2129. }
  2130. return self;
  2131. };
  2132. this.showRailHr = function () {
  2133. if (self.railh) {
  2134. if ((self.page.maxw !== 0) && (self.ispage || self.win.css('display') != 'none')) {
  2135. self.railh.visibility = true;
  2136. self.railh.css('display', 'block');
  2137. }
  2138. }
  2139. return self;
  2140. };
  2141. this.hideRails = function () {
  2142. return self.hideRail().hideRailHr();
  2143. };
  2144. this.hideRail = function () {
  2145. //self.visibility = false;
  2146. self.rail.visibility = false;
  2147. self.rail.css('display', 'none');
  2148. return self;
  2149. };
  2150. this.hideRailHr = function () {
  2151. if (self.railh) {
  2152. self.railh.visibility = false;
  2153. self.railh.css('display', 'none');
  2154. }
  2155. return self;
  2156. };
  2157. this.show = function () {
  2158. self.hidden = false;
  2159. self.railslocked = false;
  2160. return self.showRails();
  2161. };
  2162. this.hide = function () {
  2163. self.hidden = true;
  2164. self.railslocked = true;
  2165. return self.hideRails();
  2166. };
  2167. this.toggle = function () {
  2168. return (self.hidden) ? self.show() : self.hide();
  2169. };
  2170. this.remove = function () {
  2171. self.stop();
  2172. if (self.cursortimeout) clearTimeout(self.cursortimeout);
  2173. for (var n in self.delaylist) if (self.delaylist[n]) clearAnimationFrame(self.delaylist[n].h);
  2174. self.doZoomOut();
  2175. self.unbindAll();
  2176. if (cap.isie9) self.win[0].detachEvent("onpropertychange", self.onAttributeChange); //IE9 DOMAttrModified bug
  2177. if (self.observer !== false) self.observer.disconnect();
  2178. if (self.observerremover !== false) self.observerremover.disconnect();
  2179. if (self.observerbody !== false) self.observerbody.disconnect();
  2180. self.events = null;
  2181. if (self.cursor) {
  2182. self.cursor.remove();
  2183. }
  2184. if (self.cursorh) {
  2185. self.cursorh.remove();
  2186. }
  2187. if (self.rail) {
  2188. self.rail.remove();
  2189. }
  2190. if (self.railh) {
  2191. self.railh.remove();
  2192. }
  2193. if (self.zoom) {
  2194. self.zoom.remove();
  2195. }
  2196. for (var a = 0; a < self.saved.css.length; a++) {
  2197. var d = self.saved.css[a];
  2198. d[0].css(d[1], (d[2] === undefined) ? '' : d[2]);
  2199. }
  2200. self.saved = false;
  2201. self.me.data('__nicescroll', ''); //erase all traces
  2202. // memory leak fixed by GianlucaGuarini - thanks a lot!
  2203. // remove the current nicescroll from the $.nicescroll array & normalize array
  2204. var lst = $.nicescroll;
  2205. lst.each(function (i) {
  2206. if (!this) return;
  2207. if (this.id === self.id) {
  2208. delete lst[i];
  2209. for (var b = ++i; b < lst.length; b++ , i++) lst[i] = lst[b];
  2210. lst.length--;
  2211. if (lst.length) delete lst[lst.length];
  2212. }
  2213. });
  2214. for (var i in self) {
  2215. self[i] = null;
  2216. delete self[i];
  2217. }
  2218. self = null;
  2219. };
  2220. this.scrollstart = function (fn) {
  2221. this.onscrollstart = fn;
  2222. return self;
  2223. };
  2224. this.scrollend = function (fn) {
  2225. this.onscrollend = fn;
  2226. return self;
  2227. };
  2228. this.scrollcancel = function (fn) {
  2229. this.onscrollcancel = fn;
  2230. return self;
  2231. };
  2232. this.zoomin = function (fn) {
  2233. this.onzoomin = fn;
  2234. return self;
  2235. };
  2236. this.zoomout = function (fn) {
  2237. this.onzoomout = fn;
  2238. return self;
  2239. };
  2240. this.isScrollable = function (e) {
  2241. var dom = (e.target) ? e.target : e;
  2242. if (dom.nodeName == 'OPTION') return true;
  2243. while (dom && (dom.nodeType == 1) && (dom !== this.me[0]) && !(/^BODY|HTML/.test(dom.nodeName))) {
  2244. var dd = $(dom);
  2245. var ov = dd.css('overflowY') || dd.css('overflowX') || dd.css('overflow') || '';
  2246. if (/scroll|auto/.test(ov)) return (dom.clientHeight != dom.scrollHeight);
  2247. dom = (dom.parentNode) ? dom.parentNode : false;
  2248. }
  2249. return false;
  2250. };
  2251. this.getViewport = function (me) {
  2252. var dom = (me && me.parentNode) ? me.parentNode : false;
  2253. while (dom && (dom.nodeType == 1) && !(/^BODY|HTML/.test(dom.nodeName))) {
  2254. var dd = $(dom);
  2255. if (/fixed|absolute/.test(dd.css("position"))) return dd;
  2256. var ov = dd.css('overflowY') || dd.css('overflowX') || dd.css('overflow') || '';
  2257. if ((/scroll|auto/.test(ov)) && (dom.clientHeight != dom.scrollHeight)) return dd;
  2258. if (dd.getNiceScroll().length > 0) return dd;
  2259. dom = (dom.parentNode) ? dom.parentNode : false;
  2260. }
  2261. return false;
  2262. };
  2263. this.triggerScrollStart = function (cx, cy, rx, ry, ms) {
  2264. if (self.onscrollstart) {
  2265. var info = {
  2266. type: "scrollstart",
  2267. current: {
  2268. x: cx,
  2269. y: cy
  2270. },
  2271. request: {
  2272. x: rx,
  2273. y: ry
  2274. },
  2275. end: {
  2276. x: self.newscrollx,
  2277. y: self.newscrolly
  2278. },
  2279. speed: ms
  2280. };
  2281. self.onscrollstart.call(self, info);
  2282. }
  2283. };
  2284. this.triggerScrollEnd = function () {
  2285. if (self.onscrollend) {
  2286. var px = self.getScrollLeft();
  2287. var py = self.getScrollTop();
  2288. var info = {
  2289. type: "scrollend",
  2290. current: {
  2291. x: px,
  2292. y: py
  2293. },
  2294. end: {
  2295. x: px,
  2296. y: py
  2297. }
  2298. };
  2299. self.onscrollend.call(self, info);
  2300. }
  2301. };
  2302. var scrolldiry = 0, scrolldirx = 0, scrolltmr = 0, scrollspd = 1;
  2303. function doScrollRelative(px, py, chkscroll, iswheel) {
  2304. if (!self.scrollrunning) {
  2305. self.newscrolly = self.getScrollTop();
  2306. self.newscrollx = self.getScrollLeft();
  2307. scrolltmr = now();
  2308. }
  2309. var gap = (now() - scrolltmr);
  2310. scrolltmr = now();
  2311. if (gap > 350) {
  2312. scrollspd = 1;
  2313. } else {
  2314. scrollspd += (2 - scrollspd) / 10;
  2315. }
  2316. px = px * scrollspd | 0;
  2317. py = py * scrollspd | 0;
  2318. if (px) {
  2319. if (iswheel) { // mouse-only
  2320. if (px < 0) { // fix apple magic mouse swipe back/forward
  2321. if (self.getScrollLeft() >= self.page.maxw) return true;
  2322. } else {
  2323. if (self.getScrollLeft() <= 0) return true;
  2324. }
  2325. }
  2326. var dx = px > 0 ? 1 : -1;
  2327. if (scrolldirx !== dx) {
  2328. if (self.scrollmom) self.scrollmom.stop();
  2329. self.newscrollx = self.getScrollLeft();
  2330. scrolldirx = dx;
  2331. }
  2332. self.lastdeltax -= px;
  2333. }
  2334. if (py) {
  2335. var chk = (function () {
  2336. var top = self.getScrollTop();
  2337. if (py < 0) {
  2338. if (top >= self.page.maxh) return true;
  2339. } else {
  2340. if (top <= 0) return true;
  2341. }
  2342. })();
  2343. if (chk) {
  2344. if (opt.nativeparentscrolling && chkscroll && !self.ispage && !self.zoomactive) return true;
  2345. var ny = self.view.h >> 1;
  2346. if (self.newscrolly < -ny) { self.newscrolly = -ny; py = -1; }
  2347. else if (self.newscrolly > self.page.maxh + ny) { self.newscrolly = self.page.maxh + ny; py = 1; }
  2348. else py = 0;
  2349. }
  2350. var dy = py > 0 ? 1 : -1;
  2351. if (scrolldiry !== dy) {
  2352. if (self.scrollmom) self.scrollmom.stop();
  2353. self.newscrolly = self.getScrollTop();
  2354. scrolldiry = dy;
  2355. }
  2356. self.lastdeltay -= py;
  2357. }
  2358. if (py || px) {
  2359. self.synched("relativexy", function () {
  2360. var dty = self.lastdeltay + self.newscrolly;
  2361. self.lastdeltay = 0;
  2362. var dtx = self.lastdeltax + self.newscrollx;
  2363. self.lastdeltax = 0;
  2364. if (!self.rail.drag) self.doScrollPos(dtx, dty);
  2365. });
  2366. }
  2367. }
  2368. var hasparentscrollingphase = false;
  2369. function execScrollWheel(e, hr, chkscroll) {
  2370. var px, py;
  2371. if (!chkscroll && hasparentscrollingphase) return true;
  2372. if (e.deltaMode === 0) { // PIXEL
  2373. px = -(e.deltaX * (opt.mousescrollstep / (18 * 3))) | 0;
  2374. py = -(e.deltaY * (opt.mousescrollstep / (18 * 3))) | 0;
  2375. } else if (e.deltaMode === 1) { // LINE
  2376. px = -(e.deltaX * opt.mousescrollstep * 50 / 80) | 0;
  2377. py = -(e.deltaY * opt.mousescrollstep * 50 / 80) | 0;
  2378. }
  2379. if (hr && opt.oneaxismousemode && (px === 0) && py) { // classic vertical-only mousewheel + browser with x/y support
  2380. px = py;
  2381. py = 0;
  2382. if (chkscroll) {
  2383. var hrend = (px < 0) ? (self.getScrollLeft() >= self.page.maxw) : (self.getScrollLeft() <= 0);
  2384. if (hrend) { // preserve vertical scrolling
  2385. py = px;
  2386. px = 0;
  2387. }
  2388. }
  2389. }
  2390. // invert horizontal direction for rtl mode
  2391. if (self.isrtlmode) px = -px;
  2392. var chk = doScrollRelative(px, py, chkscroll, true);
  2393. if (chk) {
  2394. if (chkscroll) hasparentscrollingphase = true;
  2395. } else {
  2396. hasparentscrollingphase = false;
  2397. e.stopImmediatePropagation();
  2398. return e.preventDefault();
  2399. }
  2400. }
  2401. this.onmousewheel = function (e) {
  2402. if (self.wheelprevented||self.locked) return false;
  2403. if (self.railslocked) {
  2404. self.debounced("checkunlock", self.resize, 250);
  2405. return false;
  2406. }
  2407. if (self.rail.drag) return self.cancelEvent(e);
  2408. if (opt.oneaxismousemode === "auto" && e.deltaX !== 0) opt.oneaxismousemode = false; // check two-axis mouse support (not very elegant)
  2409. if (opt.oneaxismousemode && e.deltaX === 0) {
  2410. if (!self.rail.scrollable) {
  2411. if (self.railh && self.railh.scrollable) {
  2412. return self.onmousewheelhr(e);
  2413. } else {
  2414. return true;
  2415. }
  2416. }
  2417. }
  2418. var nw = now();
  2419. var chk = false;
  2420. if (opt.preservenativescrolling && ((self.checkarea + 600) < nw)) {
  2421. self.nativescrollingarea = self.isScrollable(e);
  2422. chk = true;
  2423. }
  2424. self.checkarea = nw;
  2425. if (self.nativescrollingarea) return true; // this isn't my business
  2426. var ret = execScrollWheel(e, false, chk);
  2427. if (ret) self.checkarea = 0;
  2428. return ret;
  2429. };
  2430. this.onmousewheelhr = function (e) {
  2431. if (self.wheelprevented) return;
  2432. if (self.railslocked || !self.railh.scrollable) return true;
  2433. if (self.rail.drag) return self.cancelEvent(e);
  2434. var nw = now();
  2435. var chk = false;
  2436. if (opt.preservenativescrolling && ((self.checkarea + 600) < nw)) {
  2437. self.nativescrollingarea = self.isScrollable(e);
  2438. chk = true;
  2439. }
  2440. self.checkarea = nw;
  2441. if (self.nativescrollingarea) return true; // this is not my business
  2442. if (self.railslocked) return self.cancelEvent(e);
  2443. return execScrollWheel(e, true, chk);
  2444. };
  2445. this.stop = function () {
  2446. self.cancelScroll();
  2447. if (self.scrollmon) self.scrollmon.stop();
  2448. self.cursorfreezed = false;
  2449. self.scroll.y = Math.round(self.getScrollTop() * (1 / self.scrollratio.y));
  2450. self.noticeCursor();
  2451. return self;
  2452. };
  2453. this.getTransitionSpeed = function (dif) {
  2454. return 80 + (dif / 72) * opt.scrollspeed |0;
  2455. };
  2456. if (!opt.smoothscroll) {
  2457. this.doScrollLeft = function (x, spd) { //direct
  2458. var y = self.getScrollTop();
  2459. self.doScrollPos(x, y, spd);
  2460. };
  2461. this.doScrollTop = function (y, spd) { //direct
  2462. var x = self.getScrollLeft();
  2463. self.doScrollPos(x, y, spd);
  2464. };
  2465. this.doScrollPos = function (x, y, spd) { //direct
  2466. var nx = (x > self.page.maxw) ? self.page.maxw : x;
  2467. if (nx < 0) nx = 0;
  2468. var ny = (y > self.page.maxh) ? self.page.maxh : y;
  2469. if (ny < 0) ny = 0;
  2470. self.synched('scroll', function () {
  2471. self.setScrollTop(ny);
  2472. self.setScrollLeft(nx);
  2473. });
  2474. };
  2475. this.cancelScroll = function () { }; // direct
  2476. } else if (self.ishwscroll && cap.hastransition && opt.usetransition && !!opt.smoothscroll) {
  2477. var lasttransitionstyle = '';
  2478. this.resetTransition = function () {
  2479. lasttransitionstyle = '';
  2480. self.doc.css(cap.prefixstyle + 'transition-duration', '0ms');
  2481. };
  2482. this.prepareTransition = function (dif, istime) {
  2483. var ex = (istime) ? dif : self.getTransitionSpeed(dif);
  2484. var trans = ex + 'ms';
  2485. if (lasttransitionstyle !== trans) {
  2486. lasttransitionstyle = trans;
  2487. self.doc.css(cap.prefixstyle + 'transition-duration', trans);
  2488. }
  2489. return ex;
  2490. };
  2491. this.doScrollLeft = function (x, spd) { //trans
  2492. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  2493. self.doScrollPos(x, y, spd);
  2494. };
  2495. this.doScrollTop = function (y, spd) { //trans
  2496. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  2497. self.doScrollPos(x, y, spd);
  2498. };
  2499. this.cursorupdate = {
  2500. running: false,
  2501. start: function () {
  2502. var m = this;
  2503. if (m.running) return;
  2504. m.running = true;
  2505. var loop = function () {
  2506. if (m.running) setAnimationFrame(loop);
  2507. self.showCursor(self.getScrollTop(), self.getScrollLeft());
  2508. self.notifyScrollEvent(self.win[0]);
  2509. };
  2510. setAnimationFrame(loop);
  2511. },
  2512. stop: function () {
  2513. this.running = false;
  2514. }
  2515. };
  2516. this.doScrollPos = function (x, y, spd) { //trans
  2517. var py = self.getScrollTop();
  2518. var px = self.getScrollLeft();
  2519. if (((self.newscrolly - py) * (y - py) < 0) || ((self.newscrollx - px) * (x - px) < 0)) self.cancelScroll(); //inverted movement detection
  2520. if (!opt.bouncescroll) {
  2521. if (y < 0) y = 0;
  2522. else if (y > self.page.maxh) y = self.page.maxh;
  2523. if (x < 0) x = 0;
  2524. else if (x > self.page.maxw) x = self.page.maxw;
  2525. } else {
  2526. if (y < 0) y = y / 2 | 0;
  2527. else if (y > self.page.maxh) y = self.page.maxh + (y - self.page.maxh) / 2 | 0;
  2528. if (x < 0) x = x / 2 | 0;
  2529. else if (x > self.page.maxw) x = self.page.maxw + (x - self.page.maxw) / 2 | 0;
  2530. }
  2531. if (self.scrollrunning && x == self.newscrollx && y == self.newscrolly) return false;
  2532. self.newscrolly = y;
  2533. self.newscrollx = x;
  2534. var top = self.getScrollTop();
  2535. var lft = self.getScrollLeft();
  2536. var dst = {};
  2537. dst.x = x - lft;
  2538. dst.y = y - top;
  2539. var dd = Math.sqrt((dst.x * dst.x) + (dst.y * dst.y)) | 0;
  2540. var ms = self.prepareTransition(dd);
  2541. if (!self.scrollrunning) {
  2542. self.scrollrunning = true;
  2543. self.triggerScrollStart(lft, top, x, y, ms);
  2544. self.cursorupdate.start();
  2545. }
  2546. self.scrollendtrapped = true;
  2547. if (!cap.transitionend) {
  2548. if (self.scrollendtrapped) clearTimeout(self.scrollendtrapped);
  2549. self.scrollendtrapped = setTimeout(self.onScrollTransitionEnd, ms); // simulate transitionend event
  2550. }
  2551. self.setScrollTop(self.newscrolly);
  2552. self.setScrollLeft(self.newscrollx);
  2553. };
  2554. this.cancelScroll = function () {
  2555. if (!self.scrollendtrapped) return true;
  2556. var py = self.getScrollTop();
  2557. var px = self.getScrollLeft();
  2558. self.scrollrunning = false;
  2559. if (!cap.transitionend) clearTimeout(cap.transitionend);
  2560. self.scrollendtrapped = false;
  2561. self.resetTransition();
  2562. self.setScrollTop(py); // fire event onscroll
  2563. if (self.railh) self.setScrollLeft(px);
  2564. if (self.timerscroll && self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  2565. self.timerscroll = false;
  2566. self.cursorfreezed = false;
  2567. self.cursorupdate.stop();
  2568. self.showCursor(py, px);
  2569. return self;
  2570. };
  2571. this.onScrollTransitionEnd = function () {
  2572. if (!self.scrollendtrapped) return;
  2573. var py = self.getScrollTop();
  2574. var px = self.getScrollLeft();
  2575. if (py < 0) py = 0;
  2576. else if (py > self.page.maxh) py = self.page.maxh;
  2577. if (px < 0) px = 0;
  2578. else if (px > self.page.maxw) px = self.page.maxw;
  2579. if ((py != self.newscrolly) || (px != self.newscrollx)) return self.doScrollPos(px, py, opt.snapbackspeed);
  2580. if (self.scrollrunning) self.triggerScrollEnd();
  2581. self.scrollrunning = false;
  2582. self.scrollendtrapped = false;
  2583. self.resetTransition();
  2584. self.timerscroll = false;
  2585. self.setScrollTop(py); // fire event onscroll
  2586. if (self.railh) self.setScrollLeft(px); // fire event onscroll left
  2587. self.cursorupdate.stop();
  2588. self.noticeCursor(false, py, px);
  2589. self.cursorfreezed = false;
  2590. };
  2591. } else {
  2592. this.doScrollLeft = function (x, spd) { //no-trans
  2593. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  2594. self.doScrollPos(x, y, spd);
  2595. };
  2596. this.doScrollTop = function (y, spd) { //no-trans
  2597. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  2598. self.doScrollPos(x, y, spd);
  2599. };
  2600. this.doScrollPos = function (x, y, spd) { //no-trans
  2601. var py = self.getScrollTop();
  2602. var px = self.getScrollLeft();
  2603. if (((self.newscrolly - py) * (y - py) < 0) || ((self.newscrollx - px) * (x - px) < 0)) self.cancelScroll(); //inverted movement detection
  2604. var clipped = false;
  2605. if (!self.bouncescroll || !self.rail.visibility) {
  2606. if (y < 0) {
  2607. y = 0;
  2608. clipped = true;
  2609. } else if (y > self.page.maxh) {
  2610. y = self.page.maxh;
  2611. clipped = true;
  2612. }
  2613. }
  2614. if (!self.bouncescroll || !self.railh.visibility) {
  2615. if (x < 0) {
  2616. x = 0;
  2617. clipped = true;
  2618. } else if (x > self.page.maxw) {
  2619. x = self.page.maxw;
  2620. clipped = true;
  2621. }
  2622. }
  2623. if (self.scrollrunning && (self.newscrolly === y) && (self.newscrollx === x)) return true;
  2624. self.newscrolly = y;
  2625. self.newscrollx = x;
  2626. self.dst = {};
  2627. self.dst.x = x - px;
  2628. self.dst.y = y - py;
  2629. self.dst.px = px;
  2630. self.dst.py = py;
  2631. var dd = Math.sqrt((self.dst.x * self.dst.x) + (self.dst.y * self.dst.y)) | 0;
  2632. var ms = self.getTransitionSpeed(dd);
  2633. self.bzscroll = {};
  2634. var p3 = (clipped) ? 1 : 0.58;
  2635. self.bzscroll.x = new BezierClass(px, self.newscrollx, ms, 0, 0, p3, 1);
  2636. self.bzscroll.y = new BezierClass(py, self.newscrolly, ms, 0, 0, p3, 1);
  2637. var loopid = now();
  2638. var loop = function () {
  2639. if (!self.scrollrunning) return;
  2640. var x = self.bzscroll.y.getPos();
  2641. self.setScrollLeft(self.bzscroll.x.getNow());
  2642. self.setScrollTop(self.bzscroll.y.getNow());
  2643. if (x <= 1) {
  2644. self.timer = setAnimationFrame(loop);
  2645. } else {
  2646. self.scrollrunning = false;
  2647. self.timer = 0;
  2648. self.triggerScrollEnd();
  2649. }
  2650. };
  2651. if (!self.scrollrunning) {
  2652. self.triggerScrollStart(px, py, x, y, ms);
  2653. self.scrollrunning = true;
  2654. self.timer = setAnimationFrame(loop);
  2655. }
  2656. };
  2657. this.cancelScroll = function () {
  2658. if (self.timer) clearAnimationFrame(self.timer);
  2659. self.timer = 0;
  2660. self.bzscroll = false;
  2661. self.scrollrunning = false;
  2662. return self;
  2663. };
  2664. }
  2665. this.doScrollBy = function (stp, relative) {
  2666. doScrollRelative(0, stp);
  2667. };
  2668. this.doScrollLeftBy = function (stp, relative) {
  2669. doScrollRelative(stp, 0);
  2670. };
  2671. this.doScrollTo = function (pos, relative) {
  2672. var ny = (relative) ? Math.round(pos * self.scrollratio.y) : pos;
  2673. if (ny < 0) ny = 0;
  2674. else if (ny > self.page.maxh) ny = self.page.maxh;
  2675. self.cursorfreezed = false;
  2676. self.doScrollTop(pos);
  2677. };
  2678. this.checkContentSize = function () {
  2679. var pg = self.getContentSize();
  2680. if ((pg.h != self.page.h) || (pg.w != self.page.w)) self.resize(false, pg);
  2681. };
  2682. self.onscroll = function (e) {
  2683. if (self.rail.drag) return;
  2684. if (!self.cursorfreezed) {
  2685. self.synched('scroll', function () {
  2686. self.scroll.y = Math.round(self.getScrollTop() / self.scrollratio.y);
  2687. if (self.railh) self.scroll.x = Math.round(self.getScrollLeft() / self.scrollratio.x);
  2688. self.noticeCursor();
  2689. });
  2690. }
  2691. };
  2692. self.bind(self.docscroll, "scroll", self.onscroll);
  2693. this.doZoomIn = function (e) {
  2694. if (self.zoomactive) return;
  2695. self.zoomactive = true;
  2696. self.zoomrestore = {
  2697. style: {}
  2698. };
  2699. var lst = ['position', 'top', 'left', 'zIndex', 'backgroundColor', 'marginTop', 'marginBottom', 'marginLeft', 'marginRight'];
  2700. var win = self.win[0].style;
  2701. for (var a in lst) {
  2702. var pp = lst[a];
  2703. self.zoomrestore.style[pp] = (win[pp] !== undefined) ? win[pp] : '';
  2704. }
  2705. self.zoomrestore.style.width = self.win.css('width');
  2706. self.zoomrestore.style.height = self.win.css('height');
  2707. self.zoomrestore.padding = {
  2708. w: self.win.outerWidth() - self.win.width(),
  2709. h: self.win.outerHeight() - self.win.height()
  2710. };
  2711. if (cap.isios4) {
  2712. self.zoomrestore.scrollTop = $window.scrollTop();
  2713. $window.scrollTop(0);
  2714. }
  2715. self.win.css({
  2716. position: (cap.isios4) ? "absolute" : "fixed",
  2717. top: 0,
  2718. left: 0,
  2719. zIndex: globalmaxzindex + 100,
  2720. margin: 0
  2721. });
  2722. var bkg = self.win.css("backgroundColor");
  2723. if ("" === bkg || /transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor", "#fff");
  2724. self.rail.css({
  2725. zIndex: globalmaxzindex + 101
  2726. });
  2727. self.zoom.css({
  2728. zIndex: globalmaxzindex + 102
  2729. });
  2730. self.zoom.css('backgroundPosition', '0 -18px');
  2731. self.resizeZoom();
  2732. if (self.onzoomin) self.onzoomin.call(self);
  2733. return self.cancelEvent(e);
  2734. };
  2735. this.doZoomOut = function (e) {
  2736. if (!self.zoomactive) return;
  2737. self.zoomactive = false;
  2738. self.win.css("margin", "");
  2739. self.win.css(self.zoomrestore.style);
  2740. if (cap.isios4) {
  2741. $window.scrollTop(self.zoomrestore.scrollTop);
  2742. }
  2743. self.rail.css({
  2744. "z-index": self.zindex
  2745. });
  2746. self.zoom.css({
  2747. "z-index": self.zindex
  2748. });
  2749. self.zoomrestore = false;
  2750. self.zoom.css('backgroundPosition', '0 0');
  2751. self.onResize();
  2752. if (self.onzoomout) self.onzoomout.call(self);
  2753. return self.cancelEvent(e);
  2754. };
  2755. this.doZoom = function (e) {
  2756. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  2757. };
  2758. this.resizeZoom = function () {
  2759. if (!self.zoomactive) return;
  2760. var py = self.getScrollTop(); //preserve scrolling position
  2761. self.win.css({
  2762. width: $window.width() - self.zoomrestore.padding.w + "px",
  2763. height: $window.height() - self.zoomrestore.padding.h + "px"
  2764. });
  2765. self.onResize();
  2766. self.setScrollTop(Math.min(self.page.maxh, py));
  2767. };
  2768. this.init();
  2769. $.nicescroll.push(this);
  2770. };
  2771. // Inspired by the work of Kin Blas
  2772. // http://webpro.host.adobe.com/people/jblas/momentum/includes/jquery.momentum.0.7.js
  2773. var ScrollMomentumClass2D = function (nc) {
  2774. var self = this;
  2775. this.nc = nc;
  2776. this.lastx = 0;
  2777. this.lasty = 0;
  2778. this.speedx = 0;
  2779. this.speedy = 0;
  2780. this.lasttime = 0;
  2781. this.steptime = 0;
  2782. this.snapx = false;
  2783. this.snapy = false;
  2784. this.demulx = 0;
  2785. this.demuly = 0;
  2786. this.lastscrollx = -1;
  2787. this.lastscrolly = -1;
  2788. this.chkx = 0;
  2789. this.chky = 0;
  2790. this.timer = 0;
  2791. this.reset = function (px, py) {
  2792. self.stop();
  2793. self.steptime = 0;
  2794. self.lasttime = now();
  2795. self.speedx = 0;
  2796. self.speedy = 0;
  2797. self.lastx = px;
  2798. self.lasty = py;
  2799. self.lastscrollx = -1;
  2800. self.lastscrolly = -1;
  2801. };
  2802. this.update = function (px, py) {
  2803. var tm = now();
  2804. self.steptime = tm - self.lasttime;
  2805. self.lasttime = tm;
  2806. var dy = py - self.lasty;
  2807. var dx = px - self.lastx;
  2808. var sy = self.nc.getScrollTop();
  2809. var sx = self.nc.getScrollLeft();
  2810. var newy = sy + dy;
  2811. var newx = sx + dx;
  2812. self.snapx = (newx < 0) || (newx > self.nc.page.maxw);
  2813. self.snapy = (newy < 0) || (newy > self.nc.page.maxh);
  2814. self.speedx = dx;
  2815. self.speedy = dy;
  2816. self.lastx = px;
  2817. self.lasty = py;
  2818. };
  2819. this.stop = function () {
  2820. self.nc.unsynched("domomentum2d");
  2821. if (self.timer) clearTimeout(self.timer);
  2822. self.timer = 0;
  2823. self.lastscrollx = -1;
  2824. self.lastscrolly = -1;
  2825. };
  2826. this.doSnapy = function (nx, ny) {
  2827. var snap = false;
  2828. if (ny < 0) {
  2829. ny = 0;
  2830. snap = true;
  2831. } else if (ny > self.nc.page.maxh) {
  2832. ny = self.nc.page.maxh;
  2833. snap = true;
  2834. }
  2835. if (nx < 0) {
  2836. nx = 0;
  2837. snap = true;
  2838. } else if (nx > self.nc.page.maxw) {
  2839. nx = self.nc.page.maxw;
  2840. snap = true;
  2841. }
  2842. (snap) ? self.nc.doScrollPos(nx, ny, self.nc.opt.snapbackspeed) : self.nc.triggerScrollEnd();
  2843. };
  2844. this.doMomentum = function (gp) {
  2845. var t = now();
  2846. var l = (gp) ? t + gp : self.lasttime;
  2847. var sl = self.nc.getScrollLeft();
  2848. var st = self.nc.getScrollTop();
  2849. var pageh = self.nc.page.maxh;
  2850. var pagew = self.nc.page.maxw;
  2851. self.speedx = (pagew > 0) ? Math.min(60, self.speedx) : 0;
  2852. self.speedy = (pageh > 0) ? Math.min(60, self.speedy) : 0;
  2853. var chk = l && (t - l) <= 60;
  2854. if ((st < 0) || (st > pageh) || (sl < 0) || (sl > pagew)) chk = false;
  2855. var sy = (self.speedy && chk) ? self.speedy : false;
  2856. var sx = (self.speedx && chk) ? self.speedx : false;
  2857. if (sy || sx) {
  2858. var tm = Math.max(16, self.steptime); //timeout granularity
  2859. if (tm > 50) { // do smooth
  2860. var xm = tm / 50;
  2861. self.speedx *= xm;
  2862. self.speedy *= xm;
  2863. tm = 50;
  2864. }
  2865. self.demulxy = 0;
  2866. self.lastscrollx = self.nc.getScrollLeft();
  2867. self.chkx = self.lastscrollx;
  2868. self.lastscrolly = self.nc.getScrollTop();
  2869. self.chky = self.lastscrolly;
  2870. var nx = self.lastscrollx;
  2871. var ny = self.lastscrolly;
  2872. var onscroll = function () {
  2873. var df = ((now() - t) > 600) ? 0.04 : 0.02;
  2874. if (self.speedx) {
  2875. nx = Math.floor(self.lastscrollx - (self.speedx * (1 - self.demulxy)));
  2876. self.lastscrollx = nx;
  2877. if ((nx < 0) || (nx > pagew)) df = 0.10;
  2878. }
  2879. if (self.speedy) {
  2880. ny = Math.floor(self.lastscrolly - (self.speedy * (1 - self.demulxy)));
  2881. self.lastscrolly = ny;
  2882. if ((ny < 0) || (ny > pageh)) df = 0.10;
  2883. }
  2884. self.demulxy = Math.min(1, self.demulxy + df);
  2885. self.nc.synched("domomentum2d", function () {
  2886. if (self.speedx) {
  2887. var scx = self.nc.getScrollLeft();
  2888. // if (scx != self.chkx) self.stop();
  2889. self.chkx = nx;
  2890. self.nc.setScrollLeft(nx);
  2891. }
  2892. if (self.speedy) {
  2893. var scy = self.nc.getScrollTop();
  2894. // if (scy != self.chky) self.stop();
  2895. self.chky = ny;
  2896. self.nc.setScrollTop(ny);
  2897. }
  2898. if (!self.timer) {
  2899. self.nc.hideCursor();
  2900. self.doSnapy(nx, ny);
  2901. }
  2902. });
  2903. if (self.demulxy < 1) {
  2904. self.timer = setTimeout(onscroll, tm);
  2905. } else {
  2906. self.stop();
  2907. self.nc.hideCursor();
  2908. self.doSnapy(nx, ny);
  2909. }
  2910. };
  2911. onscroll();
  2912. } else {
  2913. self.doSnapy(self.nc.getScrollLeft(), self.nc.getScrollTop());
  2914. }
  2915. };
  2916. };
  2917. // override jQuery scrollTop
  2918. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  2919. jQuery.cssHooks.pageYOffset = {
  2920. get: function (elem, computed, extra) {
  2921. var nice = $.data(elem, '__nicescroll') || false;
  2922. return (nice && nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  2923. },
  2924. set: function (elem, value) {
  2925. var nice = $.data(elem, '__nicescroll') || false;
  2926. (nice && nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem, value);
  2927. return this;
  2928. }
  2929. };
  2930. jQuery.fn.scrollTop = function (value) {
  2931. if (value === undefined) {
  2932. var nice = (this[0]) ? $.data(this[0], '__nicescroll') || false : false;
  2933. return (nice && nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  2934. } else {
  2935. return this.each(function () {
  2936. var nice = $.data(this, '__nicescroll') || false;
  2937. (nice && nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this), value);
  2938. });
  2939. }
  2940. };
  2941. // override jQuery scrollLeft
  2942. var _scrollLeft = jQuery.fn.scrollLeft; // preserve original function
  2943. $.cssHooks.pageXOffset = {
  2944. get: function (elem, computed, extra) {
  2945. var nice = $.data(elem, '__nicescroll') || false;
  2946. return (nice && nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(elem);
  2947. },
  2948. set: function (elem, value) {
  2949. var nice = $.data(elem, '__nicescroll') || false;
  2950. (nice && nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call(elem, value);
  2951. return this;
  2952. }
  2953. };
  2954. jQuery.fn.scrollLeft = function (value) {
  2955. if (value === undefined) {
  2956. var nice = (this[0]) ? $.data(this[0], '__nicescroll') || false : false;
  2957. return (nice && nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(this);
  2958. } else {
  2959. return this.each(function () {
  2960. var nice = $.data(this, '__nicescroll') || false;
  2961. (nice && nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call($(this), value);
  2962. });
  2963. }
  2964. };
  2965. var NiceScrollArray = function (doms) {
  2966. var self = this;
  2967. this.length = 0;
  2968. this.name = "nicescrollarray";
  2969. this.each = function (fn) {
  2970. $.each(self, fn);
  2971. return self;
  2972. };
  2973. this.push = function (nice) {
  2974. self[self.length] = nice;
  2975. self.length++;
  2976. };
  2977. this.eq = function (idx) {
  2978. return self[idx];
  2979. };
  2980. if (doms) {
  2981. for (var a = 0; a < doms.length; a++) {
  2982. var nice = $.data(doms[a], '__nicescroll') || false;
  2983. if (nice) {
  2984. this[this.length] = nice;
  2985. this.length++;
  2986. }
  2987. }
  2988. }
  2989. return this;
  2990. };
  2991. function mplex(el, lst, fn) {
  2992. for (var a = 0, l = lst.length; a < l; a++) fn(el, lst[a]);
  2993. }
  2994. mplex(
  2995. NiceScrollArray.prototype, ['show', 'hide', 'toggle', 'onResize', 'resize', 'remove', 'stop', 'doScrollPos'],
  2996. function (e, n) {
  2997. e[n] = function () {
  2998. var args = arguments;
  2999. return this.each(function () {
  3000. this[n].apply(this, args);
  3001. });
  3002. };
  3003. }
  3004. );
  3005. jQuery.fn.getNiceScroll = function (index) {
  3006. if (index === undefined) {
  3007. return new NiceScrollArray(this);
  3008. } else {
  3009. return this[index] && $.data(this[index], '__nicescroll') || false;
  3010. }
  3011. };
  3012. var pseudos = jQuery.expr.pseudos || jQuery.expr[':']; // jQuery 3 migration
  3013. pseudos.nicescroll = function (a) {
  3014. return $.data(a, '__nicescroll') !== undefined;
  3015. };
  3016. $.fn.niceScroll = function (wrapper, _opt) {
  3017. if (_opt === undefined && typeof wrapper == "object" && !("jquery" in wrapper)) {
  3018. _opt = wrapper;
  3019. wrapper = false;
  3020. }
  3021. var ret = new NiceScrollArray();
  3022. this.each(function () {
  3023. var $this = $(this);
  3024. var opt = $.extend({}, _opt); // cloning
  3025. if (wrapper || false) {
  3026. var wrp = $(wrapper);
  3027. opt.doc = (wrp.length > 1) ? $(wrapper, $this) : wrp;
  3028. opt.win = $this;
  3029. }
  3030. var docundef = !("doc" in opt);
  3031. if (!docundef && !("win" in opt)) opt.win = $this;
  3032. var nice = $this.data('__nicescroll') || false;
  3033. if (!nice) {
  3034. opt.doc = opt.doc || $this;
  3035. nice = new NiceScrollClass(opt, $this);
  3036. $this.data('__nicescroll', nice);
  3037. }
  3038. ret.push(nice);
  3039. });
  3040. return (ret.length === 1) ? ret[0] : ret;
  3041. };
  3042. _win.NiceScroll = {
  3043. getjQuery: function () {
  3044. return jQuery;
  3045. }
  3046. };
  3047. if (!$.nicescroll) {
  3048. $.nicescroll = new NiceScrollArray();
  3049. $.nicescroll.options = _globaloptions;
  3050. }
  3051. }));