jquery.nicescroll.js 113 KB

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