jquery.nicescroll.js 125 KB

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