jquery.nicescroll.js 113 KB

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