jquery.nicescroll.js 119 KB

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