jquery.nicescroll.js 111 KB

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