jquery.nicescroll.js 97 KB

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