jquery.nicescroll.js 117 KB

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