jquery.nicescroll.js 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  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. if (self.locked&&key!=27) return self.cancelEvent(e);
  1068. var ctrl = e.ctrlKey||false;
  1069. var shift = e.shiftKey || false;
  1070. var ret = false;
  1071. switch (key) {
  1072. case 38:
  1073. case 63233: //safari
  1074. self.doScrollBy(24*3);
  1075. ret = true;
  1076. break;
  1077. case 40:
  1078. case 63235: //safari
  1079. self.doScrollBy(-24*3);
  1080. ret = true;
  1081. break;
  1082. case 37:
  1083. case 63232: //safari
  1084. if (self.railh) {
  1085. (ctrl) ? self.doScrollLeft(0) : self.doScrollLeftBy(24*3);
  1086. ret = true;
  1087. }
  1088. break;
  1089. case 39:
  1090. case 63234: //safari
  1091. if (self.railh) {
  1092. (ctrl) ? self.doScrollLeft(self.page.maxw) : self.doScrollLeftBy(-24*3);
  1093. ret = true;
  1094. }
  1095. break;
  1096. case 33:
  1097. case 63276: // safari
  1098. self.doScrollBy(self.view.h);
  1099. ret = true;
  1100. break;
  1101. case 34:
  1102. case 63277: // safari
  1103. self.doScrollBy(-self.view.h);
  1104. ret = true;
  1105. break;
  1106. case 36:
  1107. case 63273: // safari
  1108. (self.railh&&ctrl) ? self.doScrollPos(0,0) : self.doScrollTo(0);
  1109. ret = true;
  1110. break;
  1111. case 35:
  1112. case 63275: // safari
  1113. (self.railh&&ctrl) ? self.doScrollPos(self.page.maxw,self.page.maxh) : self.doScrollTo(self.page.maxh);
  1114. ret = true;
  1115. break;
  1116. case 32:
  1117. if (self.opt.spacebarenabled) {
  1118. (shift) ? self.doScrollBy(self.view.h) : self.doScrollBy(-self.view.h);
  1119. ret = true;
  1120. }
  1121. break;
  1122. case 27: // ESC
  1123. if (self.zoomactive) {
  1124. self.doZoom();
  1125. ret = true;
  1126. }
  1127. break;
  1128. }
  1129. if (ret) return self.cancelEvent(e);
  1130. }
  1131. };
  1132. if (self.opt.enablekeyboard) self.bind(document,(cap.isopera&&!cap.isopera12)?"keypress":"keydown",self.onkeypress);
  1133. self.bind(window,'resize',self.resize);
  1134. self.bind(window,'orientationchange',self.resize);
  1135. self.bind(window,"load",self.resize);
  1136. if (cap.ischrome&&!self.ispage&&!self.haswrapper) { //chrome void scrollbar bug
  1137. var tmp=self.win.attr("style");
  1138. var ww = parseFloat(self.win.css("width"))+1;
  1139. self.win.css('width',ww);
  1140. self.synched("chromefix",function(){self.win.attr("style",tmp)});
  1141. }
  1142. // Trying a cross-browser implementation - good luck!
  1143. self.onAttributeChange = function(e) {
  1144. self.lazyResize();
  1145. }
  1146. if (!self.ispage&&!self.haswrapper) {
  1147. // thanks to Filip http://stackoverflow.com/questions/1882224/
  1148. if ("WebKitMutationObserver" in window) {
  1149. self.observer = new WebKitMutationObserver(function(mutations) {
  1150. mutations.forEach(self.onAttributeChange);
  1151. });
  1152. self.observer.observe(self.win[0],{attributes:true,subtree:false});
  1153. } else {
  1154. self.bind(self.win,(cap.isie&&!cap.isie9)?"propertychange":"DOMAttrModified",self.onAttributeChange);
  1155. if (cap.isie9) self.win[0].attachEvent("onpropertychange",self.onAttributeChange); //IE9 DOMAttrModified bug
  1156. }
  1157. }
  1158. //
  1159. if (!self.ispage&&self.opt.boxzoom) self.bind(window,"resize",self.resizeZoom);
  1160. if (self.istextarea) self.bind(self.win,"mouseup",self.resize);
  1161. self.resize();
  1162. }
  1163. if (this.doc[0].nodeName == 'IFRAME') {
  1164. function oniframeload(e) {
  1165. self.iframexd = false;
  1166. try {
  1167. var doc = 'contentDocument' in this ? this.contentDocument : this.contentWindow.document;
  1168. var a = doc.domain;
  1169. } catch(e){self.iframexd = true;doc=false};
  1170. if (self.iframexd) {
  1171. if ("console" in window) console.log('NiceScroll error: policy restriced iframe');
  1172. return true; //cross-domain - I can't manage this
  1173. }
  1174. self.forcescreen = true;
  1175. if (self.isiframe) {
  1176. self.iframe = {
  1177. "doc":$(doc),
  1178. "html":self.doc.contents().find('html')[0],
  1179. "body":self.doc.contents().find('body')[0]
  1180. };
  1181. self.getContentSize = function(){
  1182. return {
  1183. w:Math.max(self.iframe.html.scrollWidth,self.iframe.body.scrollWidth),
  1184. h:Math.max(self.iframe.html.scrollHeight,self.iframe.body.scrollHeight)
  1185. }
  1186. }
  1187. self.docscroll = $(self.iframe.body);//$(this.contentWindow);
  1188. }
  1189. if (!cap.isios&&self.opt.iframeautoresize&&!self.isiframe) {
  1190. self.win.scrollTop(0); // reset position
  1191. self.doc.height(""); //reset height to fix browser bug
  1192. var hh=Math.max(doc.getElementsByTagName('html')[0].scrollHeight,doc.body.scrollHeight);
  1193. self.doc.height(hh);
  1194. }
  1195. self.resize();
  1196. if (cap.isie7) self.css($(self.iframe.html),{'overflow-y':'hidden'});
  1197. //self.css($(doc.body),{'overflow-y':'hidden'});
  1198. self.css($(self.iframe.body),{'overflow-y':'hidden'});
  1199. if ('contentWindow' in this) {
  1200. self.bind(this.contentWindow,"scroll",self.onscroll); //IE8 & minor
  1201. } else {
  1202. self.bind(doc,"scroll",self.onscroll);
  1203. }
  1204. if (self.opt.enablemousewheel) {
  1205. self.bind(doc,"mousewheel",self.onmousewheel);
  1206. }
  1207. if (self.opt.enablekeyboard) self.bind(doc,(cap.isopera)?"keypress":"keydown",self.onkeypress);
  1208. if (cap.cantouch||self.opt.touchbehavior) {
  1209. self.bind(doc,"mousedown",self.onmousedown);
  1210. self.bind(doc,"mousemove",function(e){self.onmousemove(e,true)});
  1211. if (cap.cursorgrabvalue) self.css($(doc.body),{'cursor':cap.cursorgrabvalue});
  1212. }
  1213. self.bind(doc,"mouseup",self.onmouseup);
  1214. if (self.zoom) {
  1215. if (self.opt.dblclickzoom) self.bind(doc,'dblclick',self.doZoom);
  1216. if (self.ongesturezoom) self.bind(doc,"gestureend",self.ongesturezoom);
  1217. }
  1218. };
  1219. if (this.doc[0].readyState&&this.doc[0].readyState=="complete"){
  1220. setTimeout(function(){oniframeload.call(self.doc[0],false)},500);
  1221. }
  1222. self.bind(this.doc,"load",oniframeload);
  1223. }
  1224. };
  1225. this.showCursor = function(py,px) {
  1226. if (self.cursortimeout) {
  1227. clearTimeout(self.cursortimeout);
  1228. self.cursortimeout = 0;
  1229. }
  1230. if (!self.rail) return;
  1231. if (self.autohidedom) {
  1232. self.autohidedom.stop().css({opacity:self.opt.cursoropacitymax});
  1233. self.cursoractive = true;
  1234. }
  1235. if ((typeof py != "undefined")&&(py!==false)) {
  1236. self.scroll.y = Math.round(py * 1/self.scrollratio.y);
  1237. }
  1238. if (typeof px != "undefined") {
  1239. self.scroll.x = Math.round(px * 1/self.scrollratio.x);
  1240. }
  1241. self.cursor.css({height:self.cursorheight,top:self.scroll.y});
  1242. if (self.cursorh) {
  1243. (!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});
  1244. self.cursoractive = true;
  1245. }
  1246. if (self.zoom) self.zoom.stop().css({opacity:self.opt.cursoropacitymax});
  1247. };
  1248. this.hideCursor = function(tm) {
  1249. if (self.cursortimeout) return;
  1250. if (!self.rail) return;
  1251. if (!self.autohidedom) return;
  1252. self.cursortimeout = setTimeout(function() {
  1253. if (!self.rail.active||!self.showonmouseevent) {
  1254. self.autohidedom.stop().animate({opacity:self.opt.cursoropacitymin});
  1255. if (self.zoom) self.zoom.stop().animate({opacity:self.opt.cursoropacitymin});
  1256. self.cursoractive = false;
  1257. }
  1258. self.cursortimeout = 0;
  1259. },tm||400);
  1260. };
  1261. this.noticeCursor = function(tm,py,px) {
  1262. self.showCursor(py,px);
  1263. if (!self.rail.active) self.hideCursor(tm);
  1264. };
  1265. this.getContentSize =
  1266. (self.ispage) ?
  1267. function(){
  1268. return {
  1269. w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),
  1270. h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)
  1271. }
  1272. }
  1273. : (self.haswrapper) ?
  1274. function(){
  1275. return {
  1276. w:self.doc.outerWidth()+parseInt(self.win.css('paddingLeft'))+parseInt(self.win.css('paddingRight')),
  1277. h:self.doc.outerHeight()+parseInt(self.win.css('paddingTop'))+parseInt(self.win.css('paddingBottom'))
  1278. }
  1279. }
  1280. : function() {
  1281. return {
  1282. w:self.docscroll[0].scrollWidth,
  1283. h:self.docscroll[0].scrollHeight
  1284. }
  1285. };
  1286. this.onResize = function(e,page) {
  1287. if (!self.win) return false;
  1288. if (!self.haswrapper&&!self.ispage) {
  1289. if (self.win.css('display')=='none') {
  1290. if (self.visibility) self.hideRail().hideRailHr();
  1291. return false;
  1292. } else {
  1293. if (!self.hidden&&!self.visibility) self.showRail().showRailHr();
  1294. }
  1295. }
  1296. var premaxh = self.page.maxh;
  1297. var premaxw = self.page.maxw;
  1298. var preview = {h:self.view.h,w:self.view.w};
  1299. self.view = {
  1300. w:(self.ispage) ? self.win.width() : parseInt(self.win[0].clientWidth),
  1301. h:(self.ispage) ? self.win.height() : parseInt(self.win[0].clientHeight)
  1302. };
  1303. self.page = (page) ? page : self.getContentSize();
  1304. self.page.maxh = Math.max(0,self.page.h - self.view.h);
  1305. self.page.maxw = Math.max(0,self.page.w - self.view.w);
  1306. if ((self.page.maxh==premaxh)&&(self.page.maxw==premaxw)&&(self.view.w==preview.w)) {
  1307. // test position
  1308. if (!self.ispage) {
  1309. var pos = self.win.offset();
  1310. if (self.lastposition) {
  1311. var lst = self.lastposition;
  1312. if ((lst.top==pos.top)&&(lst.left==pos.left)) return self; //nothing to do
  1313. }
  1314. self.lastposition = pos;
  1315. } else {
  1316. return self; //nothing to do
  1317. }
  1318. }
  1319. if (self.page.maxh==0) {
  1320. self.hideRail();
  1321. self.scrollvaluemax = 0;
  1322. self.scroll.y = 0;
  1323. self.scrollratio.y = 0;
  1324. self.cursorheight = 0;
  1325. self.setScrollTop(0);
  1326. self.rail.scrollable = false;
  1327. } else {
  1328. self.rail.scrollable = true;
  1329. }
  1330. if (self.page.maxw==0) {
  1331. self.hideRailHr();
  1332. self.scrollvaluemaxw = 0;
  1333. self.scroll.x = 0;
  1334. self.scrollratio.x = 0;
  1335. self.cursorwidth = 0;
  1336. self.setScrollLeft(0);
  1337. self.railh.scrollable = false;
  1338. } else {
  1339. self.railh.scrollable = true;
  1340. }
  1341. self.locked = (self.page.maxh==0)&&(self.page.maxw==0);
  1342. if (self.locked) {
  1343. if (!self.ispage) self.updateScrollBar(self.view);
  1344. return false;
  1345. }
  1346. if (!self.hidden&&!self.visibility) {
  1347. self.showRail().showRailHr();
  1348. }
  1349. else if (!self.hidden&&!self.railh.visibility) self.showRailHr();
  1350. if (self.istextarea&&self.win.css('resize')&&self.win.css('resize')!='none') self.view.h-=20;
  1351. if (!self.ispage) self.updateScrollBar(self.view);
  1352. self.cursorheight = Math.min(self.view.h,Math.round(self.view.h * (self.view.h / self.page.h)));
  1353. self.cursorheight = Math.max(self.opt.cursorminheight,self.cursorheight);
  1354. self.cursorwidth = Math.min(self.view.w,Math.round(self.view.w * (self.view.w / self.page.w)));
  1355. self.cursorwidth = Math.max(self.opt.cursorminheight,self.cursorwidth);
  1356. self.scrollvaluemax = self.view.h-self.cursorheight-self.cursor.hborder;
  1357. if (self.railh) {
  1358. self.railh.width = (self.page.maxh>0) ? (self.view.w-self.rail.width) : self.view.w;
  1359. self.scrollvaluemaxw = self.railh.width-self.cursorwidth-self.cursorh.wborder;
  1360. }
  1361. self.scrollratio = {
  1362. x:(self.page.maxw/self.scrollvaluemaxw),
  1363. y:(self.page.maxh/self.scrollvaluemax)
  1364. };
  1365. var sy = self.getScrollTop();
  1366. if (sy>self.page.maxh) {
  1367. self.doScrollTop(self.page.maxh);
  1368. } else {
  1369. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1370. self.scroll.x = Math.round(self.getScrollLeft() * (1/self.scrollratio.x));
  1371. if (self.cursoractive) self.noticeCursor();
  1372. }
  1373. if (self.scroll.y&&(self.getScrollTop()==0)) self.doScrollTo(Math.floor(self.scroll.y*self.scrollratio.y));
  1374. return self;
  1375. };
  1376. this.resize = function(){self.delayed('resize',self.onResize,30);return self;} // event debounce
  1377. this.lazyResize = function() {
  1378. self.delayed('resize',self.resize,250);
  1379. }
  1380. this._bind = function(el,name,fn,bubble) { // primitive bind
  1381. self.events.push({e:el,n:name,f:fn,b:bubble,q:false});
  1382. if (el.addEventListener) {
  1383. el.addEventListener(name,fn,bubble||false);
  1384. }
  1385. else if (el.attachEvent) {
  1386. el.attachEvent("on"+name,fn);
  1387. }
  1388. else {
  1389. el["on"+name] = fn;
  1390. }
  1391. };
  1392. this.jqbind = function(dom,name,fn) { // use jquery bind for non-native events (mouseenter/mouseleave)
  1393. self.events.push({e:dom,n:name,f:fn,q:true});
  1394. $(dom).bind(name,fn);
  1395. }
  1396. this.bind = function(dom,name,fn,bubble) { // touch-oriented & fixing jquery bind
  1397. var el = ("jquery" in dom) ? dom[0] : dom;
  1398. if (el.addEventListener) {
  1399. if (cap.cantouch && /mouseup|mousedown|mousemove/.test(name)) { // touch device support
  1400. var tt=(name=='mousedown')?'touchstart':(name=='mouseup')?'touchend':'touchmove';
  1401. self._bind(el,tt,function(e){
  1402. if (e.touches) {
  1403. if (e.touches.length<2) {var ev=(e.touches.length)?e.touches[0]:e;ev.original=e;fn.call(this,ev);}
  1404. }
  1405. else if (e.changedTouches) {var ev=e.changedTouches[0];ev.original=e;fn.call(this,ev);} //blackberry
  1406. },bubble||false);
  1407. }
  1408. self._bind(el,name,fn,bubble||false);
  1409. if (name=='mousewheel') self._bind(el,"DOMMouseScroll",fn,bubble||false);
  1410. if (cap.cantouch && name=="mouseup") self._bind(el,"touchcancel",fn,bubble||false);
  1411. }
  1412. else {
  1413. self._bind(el,name,function(e) {
  1414. e = e||window.event||false;
  1415. if (e) {
  1416. if (e.srcElement) e.target=e.srcElement;
  1417. }
  1418. return ((fn.call(el,e)===false)||bubble===false) ? self.cancelEvent(e) : true;
  1419. });
  1420. }
  1421. };
  1422. this._unbind = function(el,name,fn,bub) { // primitive unbind
  1423. if (el.removeEventListener) {
  1424. el.removeEventListener(name,fn,bub);
  1425. }
  1426. else if (el.detachEvent) {
  1427. el.detachEvent('on'+name,fn);
  1428. } else {
  1429. el['on'+name] = false;
  1430. }
  1431. };
  1432. this.unbindAll = function() {
  1433. for(var a=0;a<self.events.length;a++) {
  1434. var r = self.events[a];
  1435. (r.q) ? r.e.unbind(r.n,r.f) : self._unbind(r.e,r.n,r.f,r.b);
  1436. }
  1437. };
  1438. // Thanks to http://www.switchonthecode.com !!
  1439. this.cancelEvent = function(e) {
  1440. var e = (e.original) ? e.original : (e) ? e : window.event||false;
  1441. if (!e) return false;
  1442. if(e.preventDefault) e.preventDefault();
  1443. if(e.stopPropagation) e.stopPropagation();
  1444. if(e.preventManipulation) e.preventManipulation(); //IE10
  1445. e.cancelBubble = true;
  1446. e.cancel = true;
  1447. e.returnValue = false;
  1448. return false;
  1449. };
  1450. this.stopPropagation = function(e) {
  1451. var e = (e.original) ? e.original : (e) ? e : window.event||false;
  1452. if (!e) return false;
  1453. if (e.stopPropagation) return e.stopPropagation();
  1454. if (e.cancelBubble) e.cancelBubble=true;
  1455. return false;
  1456. }
  1457. this.showRail = function() {
  1458. if ((self.page.maxh!=0)&&(self.ispage||self.win.css('display')!='none')) {
  1459. self.visibility = true;
  1460. self.rail.visibility = true;
  1461. self.rail.css('display','block');
  1462. }
  1463. return self;
  1464. };
  1465. this.showRailHr = function() {
  1466. if (!self.railh) return self;
  1467. if ((self.page.maxw!=0)&&(self.ispage||self.win.css('display')!='none')) {
  1468. self.railh.visibility = true;
  1469. self.railh.css('display','block');
  1470. }
  1471. return self;
  1472. };
  1473. this.hideRail = function() {
  1474. self.visibility = false;
  1475. self.rail.visibility = false;
  1476. self.rail.css('display','none');
  1477. return self;
  1478. };
  1479. this.hideRailHr = function() {
  1480. if (!self.railh) return self;
  1481. self.railh.visibility = false;
  1482. self.railh.css('display','none');
  1483. return self;
  1484. };
  1485. this.show = function() {
  1486. self.hidden = false;
  1487. self.locked = false;
  1488. return self.showRail().showRailHr();
  1489. };
  1490. this.hide = function() {
  1491. self.hidden = true;
  1492. self.locked = true;
  1493. return self.hideRail().hideRailHr();
  1494. };
  1495. this.toggle = function() {
  1496. return (self.hidden) ? self.show() : self.hide();
  1497. };
  1498. this.remove = function() {
  1499. self.stop();
  1500. if (self.cursortimeout) clearTimeout(self.cursortimeout);
  1501. self.doZoomOut();
  1502. self.unbindAll();
  1503. if (self.observer !== false) self.observer.disconnect();
  1504. self.events = [];
  1505. if (self.cursor) {
  1506. self.cursor.remove();
  1507. self.cursor = null;
  1508. }
  1509. if (self.cursorh) {
  1510. self.cursorh.remove();
  1511. self.cursorh = null;
  1512. }
  1513. if (self.rail) {
  1514. self.rail.remove();
  1515. self.rail = null;
  1516. }
  1517. if (self.railh) {
  1518. self.railh.remove();
  1519. self.railh = null;
  1520. }
  1521. if (self.zoom) {
  1522. self.zoom.remove();
  1523. self.zoom = null;
  1524. }
  1525. for(var a=0;a<self.saved.css.length;a++) {
  1526. var d=self.saved.css[a];
  1527. d[0].css(d[1],(typeof d[2]=="undefined") ? '' : d[2]);
  1528. }
  1529. self.saved = false;
  1530. self.me.data('__nicescroll',''); //erase all traces
  1531. self.me = null;
  1532. self.doc = null;
  1533. self.docscroll = null;
  1534. self.win = null;
  1535. return self;
  1536. };
  1537. this.scrollstart = function(fn) {
  1538. this.onscrollstart = fn;
  1539. return self;
  1540. }
  1541. this.scrollend = function(fn) {
  1542. this.onscrollend = fn;
  1543. return self;
  1544. }
  1545. this.scrollcancel = function(fn) {
  1546. this.onscrollcancel = fn;
  1547. return self;
  1548. }
  1549. this.zoomin = function(fn) {
  1550. this.onzoomin = fn;
  1551. return self;
  1552. }
  1553. this.zoomout = function(fn) {
  1554. this.onzoomout = fn;
  1555. return self;
  1556. }
  1557. this.isScrollable = function(e) {
  1558. var dom = (e.target) ? e.target : e;
  1559. while (dom&&(dom.nodeType==1)&&!(/BODY|HTML/.test(dom.nodeName))) {
  1560. var dd = $(dom);
  1561. var ov = dd.css('overflowY')||dd.css('overflowX')||dd.css('overflow')||'';
  1562. if (/scroll|auto/.test(ov)) return (dom.clientHeight!=dom.scrollHeight);
  1563. dom = (dom.parentNode) ? dom.parentNode : false;
  1564. }
  1565. return false;
  1566. };
  1567. this.getViewport = function(me) {
  1568. var dom = (me&&me.parentNode) ? me.parentNode : false;
  1569. while (dom&&(dom.nodeType==1)&&!(/BODY|HTML/.test(dom.nodeName))) {
  1570. var dd = $(dom);
  1571. var ov = dd.css('overflowY')||dd.css('overflowX')||dd.css('overflow')||'';
  1572. if ((/scroll|auto/.test(ov))&&(dom.clientHeight!=dom.scrollHeight)) return dd;
  1573. if (dd.getNiceScroll().length>0) return dd;
  1574. dom = (dom.parentNode) ? dom.parentNode : false;
  1575. }
  1576. return false;
  1577. };
  1578. function execScrollWheel(e,hr) {
  1579. var px = 0;
  1580. var py = 0;
  1581. var rt = 1;
  1582. if ("wheelDeltaY" in e) {
  1583. rt = self.opt.mousescrollstep/(16*3);
  1584. px = Math.floor(e.wheelDeltaX*rt);
  1585. py = Math.floor(e.wheelDeltaY*rt);
  1586. } else {
  1587. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  1588. if (delta) {
  1589. (hr) ? px = Math.floor(delta*self.opt.mousescrollstep) : py = Math.floor(delta*self.opt.mousescrollstep);
  1590. }
  1591. }
  1592. if (px) {
  1593. if (self.scrollmom) {self.scrollmom.stop()}
  1594. self.lastdeltax+=px;
  1595. self.synched("mousewheelx",function(){var dt=self.lastdeltax;self.lastdeltax=0;if(!self.rail.drag){self.doScrollLeftBy(dt)}});
  1596. }
  1597. if (py) {
  1598. if (self.scrollmom) {self.scrollmom.stop()}
  1599. self.lastdeltay+=py;
  1600. self.synched("mousewheely",function(){var dt=self.lastdeltay;self.lastdeltay=0;if(!self.rail.drag){self.doScrollBy(dt)}});
  1601. }
  1602. };
  1603. this.onmousewheel = function(e) {
  1604. if (self.locked) return true;
  1605. if (!self.rail.scrollable) {
  1606. if (self.railh&&self.railh.scrollable) {
  1607. return self.onmousewheelhr(e);
  1608. } else {
  1609. return true;
  1610. }
  1611. }
  1612. if (self.opt.preservenativescrolling&&self.checkarea) {
  1613. self.checkarea = false;
  1614. self.nativescrollingarea = self.isScrollable(e);
  1615. }
  1616. if (self.nativescrollingarea) return true; // this isn't my business
  1617. if (self.locked) return self.cancelEvent(e);
  1618. if (self.rail.drag) return self.cancelEvent(e);
  1619. execScrollWheel(e,false);
  1620. return self.cancelEvent(e);
  1621. };
  1622. this.onmousewheelhr = function(e) {
  1623. if (self.locked||!self.railh.scrollable) return true;
  1624. if (self.opt.preservenativescrolling&&self.checkarea) {
  1625. self.checkarea = false;
  1626. self.nativescrollingarea = self.isScrollable(e);
  1627. }
  1628. if (self.nativescrollingarea) return true; // this isn't my business
  1629. if (self.locked) return self.cancelEvent(e);
  1630. if (self.rail.drag) return self.cancelEvent(e);
  1631. execScrollWheel(e,true);
  1632. return self.cancelEvent(e);
  1633. };
  1634. this.stop = function() {
  1635. self.cancelScroll();
  1636. if (self.scrollmon) self.scrollmon.stop();
  1637. self.cursorfreezed = false;
  1638. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1639. self.noticeCursor();
  1640. return self;
  1641. };
  1642. this.getTransitionSpeed = function(dif) {
  1643. var sp = Math.round(self.opt.scrollspeed*10);
  1644. var ex = Math.min(sp,Math.round((dif / 20) * self.opt.scrollspeed));
  1645. return (ex>20) ? ex : 0;
  1646. }
  1647. if (!self.opt.smoothscroll) {
  1648. this.doScrollLeft = function(x,spd) { //direct
  1649. var y = self.getScrollTop();
  1650. self.doScrollPos(x,y,spd);
  1651. }
  1652. this.doScrollTop = function(y,spd) { //direct
  1653. var x = self.getScrollLeft();
  1654. self.doScrollPos(x,y,spd);
  1655. }
  1656. this.doScrollPos = function(x,y,spd) { //direct
  1657. var nx = (x>self.page.maxw) ? self.page.maxw : x;
  1658. if (nx<0) nx=0;
  1659. var ny = (y>self.page.maxh) ? self.page.maxh : y;
  1660. if (ny<0) ny=0;
  1661. self.synched('scroll',function(){
  1662. self.setScrollTop(ny);
  1663. self.setScrollLeft(nx);
  1664. });
  1665. }
  1666. this.cancelScroll = function() {}; // direct
  1667. }
  1668. else if (self.ishwscroll&&cap.hastransition&&self.opt.usetransition) {
  1669. this.prepareTransition = function(dif,istime) {
  1670. var ex = (istime) ? ((dif>20)?dif:0) : self.getTransitionSpeed(dif);
  1671. var trans = (ex) ? cap.prefixstyle+'transform '+ex+'ms ease-out' : '';
  1672. if (!self.lasttransitionstyle||self.lasttransitionstyle!=trans) {
  1673. self.lasttransitionstyle = trans;
  1674. self.doc.css(cap.transitionstyle,trans);
  1675. }
  1676. return ex;
  1677. };
  1678. this.doScrollLeft = function(x,spd) { //trans
  1679. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  1680. self.doScrollPos(x,y,spd);
  1681. }
  1682. this.doScrollTop = function(y,spd) { //trans
  1683. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  1684. self.doScrollPos(x,y,spd);
  1685. }
  1686. this.doScrollPos = function(x,y,spd) { //trans
  1687. var py = self.getScrollTop();
  1688. var px = self.getScrollLeft();
  1689. if (((self.newscrolly-py)*(y-py)<0)||((self.newscrollx-px)*(x-px)<0)) self.cancelScroll(); //inverted movement detection
  1690. if (self.opt.bouncescroll==false) {
  1691. if (y<0) y=0;
  1692. else if (y>self.page.maxh) y=self.page.maxh;
  1693. if (x<0) x=0;
  1694. else if (x>self.page.maxw) x=self.page.maxw;
  1695. }
  1696. if (x==self.newscrollx&&y==self.newscrolly) return false;
  1697. self.newscrolly = y;
  1698. self.newscrollx = x;
  1699. self.newscrollspeed = spd||false;
  1700. if (self.timer) return false;
  1701. self.timer = setTimeout(function(){
  1702. var top = self.getScrollTop();
  1703. var lft = self.getScrollLeft();
  1704. var dst = {};
  1705. dst.x = x-lft;
  1706. dst.y = y-top;
  1707. dst.px = lft;
  1708. dst.py = top;
  1709. var dd = Math.round(Math.sqrt(Math.pow(dst.x,2)+Math.pow(dst.y,2)));
  1710. var df = (self.newscrollspeed) ? self.newscrollspeed : dd;
  1711. var ms = self.prepareTransition(df);
  1712. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  1713. if (ms>0) {
  1714. if (!self.scrollrunning&&self.onscrollstart) {
  1715. var info = {"type":"scrollstart","current":{"x":lft,"y":top},"request":{"x":x,"y":y},"end":{"x":self.newscrollx,"y":self.newscrolly},"speed":ms};
  1716. self.onscrollstart.call(self,info);
  1717. }
  1718. if (cap.transitionend) {
  1719. if (!self.scrollendtrapped) {
  1720. self.scrollendtrapped = true;
  1721. self.bind(self.doc,cap.transitionend,self.onScrollEnd,false); //I have got to do something usefull!!
  1722. }
  1723. } else {
  1724. if (self.scrollendtrapped) clearTimeout(self.scrollendtrapped);
  1725. self.scrollendtrapped = setTimeout(self.onScrollEnd,ms); // simulate transitionend event
  1726. }
  1727. var py = top;
  1728. var px = lft;
  1729. self.timerscroll = {
  1730. bz: new BezierClass(py,self.newscrolly,ms,0,0,0.58,1),
  1731. bh: new BezierClass(px,self.newscrollx,ms,0,0,0.58,1)
  1732. };
  1733. if (!self.cursorfreezed) self.timerscroll.tm=setInterval(function(){self.showCursor(self.getScrollTop(),self.getScrollLeft())},60);
  1734. }
  1735. self.synched("doScroll-set",function(){
  1736. self.timer = 0;
  1737. if (self.scrollendtrapped) self.scrollrunning = true;
  1738. self.setScrollTop(self.newscrolly);
  1739. self.setScrollLeft(self.newscrollx);
  1740. if (!self.scrollendtrapped) self.onScrollEnd();
  1741. });
  1742. },50);
  1743. };
  1744. this.cancelScroll = function() {
  1745. if (!self.scrollendtrapped) return true;
  1746. var py = self.getScrollTop();
  1747. var px = self.getScrollLeft();
  1748. self.scrollrunning = false;
  1749. if (!cap.transitionend) clearTimeout(cap.transitionend);
  1750. self.scrollendtrapped = false;
  1751. self._unbind(self.doc,cap.transitionend,self.onScrollEnd);
  1752. self.prepareTransition(0);
  1753. self.setScrollTop(py); // fire event onscroll
  1754. if (self.railh) self.setScrollLeft(px);
  1755. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  1756. self.timerscroll = false;
  1757. self.cursorfreezed = false;
  1758. //self.noticeCursor(false,py,px);
  1759. self.showCursor(py,px);
  1760. return self;
  1761. };
  1762. this.onScrollEnd = function() {
  1763. if (self.scrollendtrapped) self._unbind(self.doc,cap.transitionend,self.onScrollEnd);
  1764. self.scrollendtrapped = false;
  1765. self.prepareTransition(0);
  1766. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  1767. self.timerscroll = false;
  1768. var py = self.getScrollTop();
  1769. var px = self.getScrollLeft();
  1770. self.setScrollTop(py); // fire event onscroll
  1771. if (self.railh) self.setScrollLeft(px); // fire event onscroll left
  1772. self.noticeCursor(false,py,px);
  1773. self.cursorfreezed = false;
  1774. if (py<0) py=0
  1775. else if (py>self.page.maxh) py=self.page.maxh;
  1776. if (px<0) px=0
  1777. else if (px>self.page.maxw) px=self.page.maxw;
  1778. if((py!=self.newscrolly)||(px!=self.newscrollx)) return self.doScrollPos(px,py,self.opt.snapbackspeed);
  1779. if (self.onscrollend&&self.scrollrunning) {
  1780. var info = {"type":"scrollend","current":{"x":px,"y":py},"end":{"x":self.newscrollx,"y":self.newscrolly}};
  1781. self.onscrollend.call(self,info);
  1782. }
  1783. self.scrollrunning = false;
  1784. };
  1785. } else {
  1786. this.doScrollLeft = function(x) { //no-trans
  1787. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  1788. self.doScrollPos(x,y);
  1789. }
  1790. this.doScrollTop = function(y) { //no-trans
  1791. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  1792. self.doScrollPos(x,y);
  1793. }
  1794. this.doScrollPos = function(x,y) { //no-trans
  1795. var y = ((typeof y == "undefined")||(y===false)) ? self.getScrollTop(true) : y;
  1796. if ((self.timer)&&(self.newscrolly==y)&&(self.newscrollx==x)) return true;
  1797. if (self.timer) clearAnimationFrame(self.timer);
  1798. self.timer = 0;
  1799. var py = self.getScrollTop();
  1800. var px = self.getScrollLeft();
  1801. if (((self.newscrolly-py)*(y-py)<0)||((self.newscrollx-px)*(x-px)<0)) self.cancelScroll(); //inverted movement detection
  1802. self.newscrolly = y;
  1803. self.newscrollx = x;
  1804. if (!self.bouncescroll||!self.rail.visibility) {
  1805. if (self.newscrolly<0) {
  1806. self.newscrolly = 0;
  1807. }
  1808. else if (self.newscrolly>self.page.maxh) {
  1809. self.newscrolly = self.page.maxh;
  1810. }
  1811. }
  1812. if (!self.bouncescroll||!self.railh.visibility) {
  1813. if (self.newscrollx<0) {
  1814. self.newscrollx = 0;
  1815. }
  1816. else if (self.newscrollx>self.page.maxw) {
  1817. self.newscrollx = self.page.maxw;
  1818. }
  1819. }
  1820. self.dst = {};
  1821. self.dst.x = x-px;
  1822. self.dst.y = y-py;
  1823. self.dst.px = px;
  1824. self.dst.py = py;
  1825. var dst = Math.round(Math.sqrt(Math.pow(self.dst.x,2)+Math.pow(self.dst.y,2)));
  1826. self.dst.ax = self.dst.x / dst;
  1827. self.dst.ay = self.dst.y / dst;
  1828. var pa = 0;
  1829. var pe = dst;
  1830. if (self.dst.x==0) {
  1831. pa = py;
  1832. pe = y;
  1833. self.dst.ay = 1;
  1834. self.dst.py = 0;
  1835. } else if (self.dst.y==0) {
  1836. pa = px;
  1837. pe = x;
  1838. self.dst.ax = 1;
  1839. self.dst.px = 0;
  1840. }
  1841. var ms = self.getTransitionSpeed(dst);
  1842. if (ms>0) {
  1843. self.bzscroll = (self.bzscroll) ? self.bzscroll.update(pe,ms) : new BezierClass(pa,pe,ms,0,1,0,1);
  1844. } else {
  1845. self.bzscroll = false;
  1846. }
  1847. if (self.timer) return;
  1848. if ((py==self.page.maxh&&y>=self.page.maxh)||(px==self.page.maxw&&x>=self.page.maxw)) self.checkContentSize();
  1849. var sync = 1;
  1850. function scrolling() {
  1851. if (self.cancelAnimationFrame) return true;
  1852. self.scrollrunning = true;
  1853. sync = 1-sync;
  1854. if (sync) return (self.timer = setAnimationFrame(scrolling)||1);
  1855. var done = 0;
  1856. var sc = sy = self.getScrollTop();
  1857. if (self.dst.ay) {
  1858. sc = (self.bzscroll) ? self.dst.py + (self.bzscroll.getNow()*self.dst.ay) : self.newscrolly;
  1859. var dr=sc-sy;
  1860. if ((dr<0&&sc<self.newscrolly)||(dr>0&&sc>self.newscrolly)) sc = self.newscrolly;
  1861. self.setScrollTop(sc);
  1862. if (sc == self.newscrolly) done=1;
  1863. } else {
  1864. done=1;
  1865. }
  1866. var scx = sx = self.getScrollLeft();
  1867. if (self.dst.ax) {
  1868. scx = (self.bzscroll) ? self.dst.px + (self.bzscroll.getNow()*self.dst.ax) : self.newscrollx;
  1869. var dr=scx-sx;
  1870. if ((dr<0&&scx<self.newscrollx)||(dr>0&&scx>self.newscrollx)) scx = self.newscrollx;
  1871. self.setScrollLeft(scx);
  1872. if (scx == self.newscrollx) done+=1;
  1873. } else {
  1874. done+=1;
  1875. }
  1876. if (done==2) {
  1877. self.timer = 0;
  1878. self.cursorfreezed = false;
  1879. self.bzscroll = false;
  1880. self.scrollrunning = false;
  1881. if (sc<0) sc=0;
  1882. else if (sc>self.page.maxh) sc=self.page.maxh;
  1883. if (scx<0) scx=0;
  1884. else if (scx>self.page.maxw) scx=self.page.maxw;
  1885. if ((scx!=self.newscrollx)||(sc!=self.newscrolly)) self.doScrollPos(scx,sc);
  1886. else {
  1887. if (self.onscrollend) {
  1888. var info = {"type":"scrollend","current":{"x":sx,"y":sy},"end":{"x":self.newscrollx,"y":self.newscrolly}};
  1889. self.onscrollend.call(self,info);
  1890. }
  1891. }
  1892. } else {
  1893. self.timer = setAnimationFrame(scrolling)||1;
  1894. }
  1895. };
  1896. self.cancelAnimationFrame=false;
  1897. self.timer = 1;
  1898. if (self.onscrollstart&&!self.scrollrunning) {
  1899. var info = {"type":"scrollstart","current":{"x":px,"y":py},"request":{"x":x,"y":y},"end":{"x":self.newscrollx,"y":self.newscrolly},"speed":ms};
  1900. self.onscrollstart.call(self,info);
  1901. }
  1902. scrolling();
  1903. if ((py==self.page.maxh&&y>=py)||(px==self.page.maxw&&x>=px)) self.checkContentSize();
  1904. self.noticeCursor();
  1905. };
  1906. this.cancelScroll = function() {
  1907. if (self.timer) clearAnimationFrame(self.timer);
  1908. self.timer = 0;
  1909. self.bzscroll = false;
  1910. self.scrollrunning = false;
  1911. return self;
  1912. };
  1913. }
  1914. this.doScrollBy = function(stp,relative) {
  1915. var ny = 0;
  1916. if (relative) {
  1917. ny = Math.floor((self.scroll.y-stp)*self.scrollratio.y)
  1918. } else {
  1919. var sy = (self.timer) ? self.newscrolly : self.getScrollTop(true);
  1920. ny = sy-stp;
  1921. }
  1922. if (self.bouncescroll) {
  1923. var haf = Math.round(self.view.h/2);
  1924. if (ny<-haf) ny=-haf
  1925. else if (ny>(self.page.maxh+haf)) ny = (self.page.maxh+haf);
  1926. }
  1927. self.cursorfreezed = false;
  1928. py = self.getScrollTop(true);
  1929. if (ny<0&&py<=0) return self.noticeCursor();
  1930. else if (ny>self.page.maxh&&py>=self.page.maxh) {
  1931. self.checkContentSize();
  1932. return self.noticeCursor();
  1933. }
  1934. self.doScrollTop(ny);
  1935. };
  1936. this.doScrollLeftBy = function(stp,relative) {
  1937. var nx = 0;
  1938. if (relative) {
  1939. nx = Math.floor((self.scroll.x-stp)*self.scrollratio.x)
  1940. } else {
  1941. var sx = (self.timer) ? self.newscrollx : self.getScrollLeft(true);
  1942. nx = sx-stp;
  1943. }
  1944. if (self.bouncescroll) {
  1945. var haf = Math.round(self.view.w/2);
  1946. if (nx<-haf) nx=-haf
  1947. else if (nx>(self.page.maxw+haf)) nx = (self.page.maxw+haf);
  1948. }
  1949. self.cursorfreezed = false;
  1950. px = self.getScrollLeft(true);
  1951. if (nx<0&&px<=0) return self.noticeCursor();
  1952. else if (nx>self.page.maxw&&px>=self.page.maxw) return self.noticeCursor();
  1953. self.doScrollLeft(nx);
  1954. };
  1955. this.doScrollTo = function(pos,relative) {
  1956. var ny = (relative) ? Math.round(pos*self.scrollratio.y) : pos;
  1957. if (ny<0) ny=0
  1958. else if (ny>self.page.maxh) ny = self.page.maxh;
  1959. self.cursorfreezed = false;
  1960. self.doScrollTop(pos);
  1961. };
  1962. this.checkContentSize = function() {
  1963. var pg = self.getContentSize();
  1964. if ((pg.h!=self.page.h)||(pg.w!=self.page.w)) self.resize(false,pg);
  1965. };
  1966. self.onscroll = function(e) {
  1967. if (self.rail.drag) return;
  1968. if (!self.cursorfreezed) {
  1969. self.synched('scroll',function(){
  1970. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1971. if (self.railh) self.scroll.x = Math.round(self.getScrollLeft() * (1/self.scrollratio.x));
  1972. self.noticeCursor();
  1973. });
  1974. }
  1975. };
  1976. self.bind(self.docscroll,"scroll",self.onscroll);
  1977. this.doZoomIn = function(e) {
  1978. if (self.zoomactive) return;
  1979. self.zoomactive = true;
  1980. self.zoomrestore = {
  1981. style:{}
  1982. };
  1983. var lst = ['position','top','left','zIndex','backgroundColor','marginTop','marginBottom','marginLeft','marginRight'];
  1984. var win = self.win[0].style;
  1985. for(var a in lst) {
  1986. var pp = lst[a];
  1987. self.zoomrestore.style[pp] = (typeof win[pp]!='undefined') ? win[pp] : '';
  1988. }
  1989. self.zoomrestore.style.width = self.win.css('width');
  1990. self.zoomrestore.style.height = self.win.css('height');
  1991. self.zoomrestore.padding = {
  1992. w:self.win.outerWidth()-self.win.width(),
  1993. h:self.win.outerHeight()-self.win.height()
  1994. };
  1995. if (cap.isios4) {
  1996. self.zoomrestore.scrollTop = $(window).scrollTop();
  1997. $(window).scrollTop(0);
  1998. }
  1999. self.win.css({
  2000. "position":(cap.isios4)?"absolute":"fixed",
  2001. "top":0,
  2002. "left":0,
  2003. "z-index":self.opt.zindex+100,
  2004. "margin":"0px"
  2005. });
  2006. var bkg = self.win.css("backgroundColor");
  2007. if (bkg==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor","#fff");
  2008. self.rail.css({"z-index":self.opt.zindex+110});
  2009. self.zoom.css({"z-index":self.opt.zindex+112});
  2010. self.zoom.css('backgroundPosition','0px -18px');
  2011. self.resizeZoom();
  2012. if (self.onzoomin) self.onzoomin.call(self);
  2013. return self.cancelEvent(e);
  2014. };
  2015. this.doZoomOut = function(e) {
  2016. if (!self.zoomactive) return;
  2017. self.zoomactive = false;
  2018. self.win.css("margin","");
  2019. self.win.css(self.zoomrestore.style);
  2020. if (cap.isios4) {
  2021. $(window).scrollTop(self.zoomrestore.scrollTop);
  2022. }
  2023. self.rail.css({"z-index":(self.ispage)?self.opt.zindex:self.opt.zindex+2});
  2024. self.zoom.css({"z-index":self.opt.zindex});
  2025. self.zoomrestore = false;
  2026. self.zoom.css('backgroundPosition','0px 0px');
  2027. self.onResize();
  2028. if (self.onzoomout) self.onzoomout.call(self);
  2029. return self.cancelEvent(e);
  2030. };
  2031. this.doZoom = function(e) {
  2032. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  2033. };
  2034. this.resizeZoom = function() {
  2035. if (!self.zoomactive) return;
  2036. var py = self.getScrollTop(); //preserve scrolling position
  2037. self.win.css({
  2038. width:$(window).width()-self.zoomrestore.padding.w+"px",
  2039. height:$(window).height()-self.zoomrestore.padding.h+"px"
  2040. });
  2041. self.onResize();
  2042. console.log(py);
  2043. self.setScrollTop(Math.min(self.page.maxh,py));
  2044. };
  2045. this.init();
  2046. $.nicescroll.push(this);
  2047. };
  2048. // Inspired by the work of Kin Blas
  2049. // http://webpro.host.adobe.com/people/jblas/momentum/includes/jquery.momentum.0.7.js
  2050. var ScrollMomentumClass2D = function(nc) {
  2051. var self = this;
  2052. this.nc = nc;
  2053. this.lastx = 0;
  2054. this.lasty = 0;
  2055. this.speedx = 0;
  2056. this.speedy = 0;
  2057. this.lasttime = 0;
  2058. this.steptime = 0;
  2059. this.snapx = false;
  2060. this.snapy = false;
  2061. this.demulx = 0;
  2062. this.demuly = 0;
  2063. this.lastscrollx = -1;
  2064. this.lastscrolly = -1;
  2065. this.chkx = 0;
  2066. this.chky = 0;
  2067. this.timer = 0;
  2068. this.time = function() {
  2069. return +new Date();//beautifull hack
  2070. };
  2071. this.reset = function(px,py) {
  2072. self.stop();
  2073. var now = self.time();
  2074. self.steptime = 0;
  2075. self.lasttime = now;
  2076. self.speedx = 0;
  2077. self.speedy = 0;
  2078. self.lastx = px;
  2079. self.lasty = py;
  2080. self.lastscrollx = -1;
  2081. self.lastscrolly = -1;
  2082. };
  2083. this.update = function(px,py) {
  2084. var now = self.time();
  2085. self.steptime = now - self.lasttime;
  2086. self.lasttime = now;
  2087. var dy = py - self.lasty;
  2088. var dx = px - self.lastx;
  2089. var sy = self.nc.getScrollTop();
  2090. var sx = self.nc.getScrollLeft();
  2091. var newy = sy + dy;
  2092. var newx = sx + dx;
  2093. self.snapx = (newx<0)||(newx>self.nc.page.maxw);
  2094. self.snapy = (newy<0)||(newy>self.nc.page.maxh);
  2095. self.speedx = dx;
  2096. self.speedy = dy;
  2097. self.lastx = px;
  2098. self.lasty = py;
  2099. };
  2100. this.stop = function() {
  2101. self.nc.unsynched("domomentum2d");
  2102. if (self.timer) clearTimeout(self.timer);
  2103. self.timer = 0;
  2104. self.lastscrollx = -1;
  2105. self.lastscrolly = -1;
  2106. };
  2107. this.doSnapy = function(nx,ny) {
  2108. var snap = false;
  2109. if (ny<0) {
  2110. ny=0;
  2111. snap=true;
  2112. }
  2113. else if (ny>self.nc.page.maxh) {
  2114. ny=self.nc.page.maxh;
  2115. snap=true;
  2116. }
  2117. if (nx<0) {
  2118. nx=0;
  2119. snap=true;
  2120. }
  2121. else if (nx>self.nc.page.maxw) {
  2122. nx=self.nc.page.maxw;
  2123. snap=true;
  2124. }
  2125. if (snap) self.nc.doScrollPos(nx,ny,self.nc.opt.snapbackspeed);
  2126. };
  2127. this.doMomentum = function(gp) {
  2128. var t = self.time();
  2129. var l = (gp) ? t+gp : self.lasttime;
  2130. var sl = self.nc.getScrollLeft();
  2131. var st = self.nc.getScrollTop();
  2132. var pageh = self.nc.page.maxh;
  2133. var pagew = self.nc.page.maxw;
  2134. self.speedx = (pagew>0) ? Math.min(60,self.speedx) : 0;
  2135. self.speedy = (pageh>0) ? Math.min(60,self.speedy) : 0;
  2136. var chk = l && (t - l) <= 50;
  2137. if ((st<0)||(st>pageh)||(sl<0)||(sl>pagew)) chk = false;
  2138. var sy = (self.speedy && chk) ? self.speedy : false;
  2139. var sx = (self.speedx && chk) ? self.speedx : false;
  2140. if (sy||sx) {
  2141. var tm = Math.max(16,self.steptime); //timeout granularity
  2142. if (tm>50) { // do smooth
  2143. var xm = tm/50;
  2144. self.speedx*=xm;
  2145. self.speedy*=xm;
  2146. tm = 50;
  2147. }
  2148. self.demulxy = 0;
  2149. self.lastscrollx = self.nc.getScrollLeft();
  2150. self.chkx = self.lastscrollx;
  2151. self.lastscrolly = self.nc.getScrollTop();
  2152. self.chky = self.lastscrolly;
  2153. var nx = self.lastscrollx;
  2154. var ny = self.lastscrolly;
  2155. var onscroll = function(){
  2156. var df = ((self.time()-t)>600) ? 0.04 : 0.02;
  2157. if (self.speedx) {
  2158. nx = Math.floor(self.lastscrollx - (self.speedx*(1-self.demulxy)));
  2159. self.lastscrollx = nx;
  2160. if ((nx<0)||(nx>pagew)) df=0.10;
  2161. }
  2162. if (self.speedy) {
  2163. ny = Math.floor(self.lastscrolly - (self.speedy*(1-self.demulxy)));
  2164. self.lastscrolly = ny;
  2165. if ((ny<0)||(ny>pageh)) df=0.10;
  2166. }
  2167. self.demulxy = Math.min(1,self.demulxy+df);
  2168. self.nc.synched("domomentum2d",function(){
  2169. if (self.speedx) {
  2170. var scx = self.nc.getScrollLeft();
  2171. if (scx!=self.chkx) self.stop();
  2172. self.chkx=nx;
  2173. self.nc.setScrollLeft(nx);
  2174. }
  2175. if (self.speedy) {
  2176. var scy = self.nc.getScrollTop();
  2177. if (scy!=self.chky) self.stop();
  2178. self.chky=ny;
  2179. self.nc.setScrollTop(ny);
  2180. }
  2181. if(!self.timer) {
  2182. self.nc.hideCursor();
  2183. self.doSnapy(nx,ny);
  2184. }
  2185. });
  2186. if (self.demulxy<1) {
  2187. self.timer = setTimeout(onscroll,tm);
  2188. } else {
  2189. self.stop();
  2190. self.nc.hideCursor();
  2191. self.doSnapy(nx,ny);
  2192. }
  2193. };
  2194. onscroll();
  2195. } else {
  2196. self.doSnapy(self.nc.getScrollLeft(),self.nc.getScrollTop());
  2197. }
  2198. }
  2199. };
  2200. // override jQuery scrollTop
  2201. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  2202. $.cssHooks["pageYOffset"] = {
  2203. get: function(elem,computed,extra) {
  2204. var nice = $.data(elem,'__nicescroll')||false;
  2205. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  2206. },
  2207. set: function(elem,value) {
  2208. var nice = $.data(elem,'__nicescroll')||false;
  2209. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem,value);
  2210. return this;
  2211. }
  2212. };
  2213. /*
  2214. $.fx.step["scrollTop"] = function(fx){
  2215. $.cssHooks["scrollTop"].set( fx.elem, fx.now + fx.unit );
  2216. };
  2217. */
  2218. jQuery.fn.scrollTop = function(value) {
  2219. if (typeof value == "undefined") {
  2220. var nice = (this[0]) ? $.data(this[0],'__nicescroll')||false : false;
  2221. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  2222. } else {
  2223. return this.each(function() {
  2224. var nice = $.data(this,'__nicescroll')||false;
  2225. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this),value);
  2226. });
  2227. }
  2228. }
  2229. // override jQuery scrollLeft
  2230. var _scrollLeft = jQuery.fn.scrollLeft; // preserve original function
  2231. $.cssHooks.pageXOffset = {
  2232. get: function(elem,computed,extra) {
  2233. var nice = $.data(elem,'__nicescroll')||false;
  2234. return (nice&&nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(elem);
  2235. },
  2236. set: function(elem,value) {
  2237. var nice = $.data(elem,'__nicescroll')||false;
  2238. (nice&&nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call(elem,value);
  2239. return this;
  2240. }
  2241. };
  2242. /*
  2243. $.fx.step["scrollLeft"] = function(fx){
  2244. $.cssHooks["scrollLeft"].set( fx.elem, fx.now + fx.unit );
  2245. };
  2246. */
  2247. jQuery.fn.scrollLeft = function(value) {
  2248. if (typeof value == "undefined") {
  2249. var nice = (this[0]) ? $.data(this[0],'__nicescroll')||false : false;
  2250. return (nice&&nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(this);
  2251. } else {
  2252. return this.each(function() {
  2253. var nice = $.data(this,'__nicescroll')||false;
  2254. (nice&&nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call($(this),value);
  2255. });
  2256. }
  2257. }
  2258. var NiceScrollArray = function(doms) {
  2259. var self = this;
  2260. this.length = 0;
  2261. this.name = "nicescrollarray";
  2262. this.each = function(fn) {
  2263. for(var a=0;a<self.length;a++) fn.call(self[a]);
  2264. return self;
  2265. };
  2266. this.push = function(nice) {
  2267. self[self.length]=nice;
  2268. self.length++;
  2269. };
  2270. this.eq = function(idx) {
  2271. return self[idx];
  2272. };
  2273. if (doms) {
  2274. for(a=0;a<doms.length;a++) {
  2275. var nice = $.data(doms[a],'__nicescroll')||false;
  2276. if (nice) {
  2277. this[this.length]=nice;
  2278. this.length++;
  2279. }
  2280. };
  2281. }
  2282. return this;
  2283. };
  2284. function mplex(el,lst,fn) {
  2285. for(var a=0;a<lst.length;a++) fn(el,lst[a]);
  2286. };
  2287. mplex(
  2288. NiceScrollArray.prototype,
  2289. ['show','hide','toggle','onResize','resize','remove','stop','doScrollPos'],
  2290. function(e,n) {
  2291. e[n] = function(){
  2292. var args = arguments;
  2293. return this.each(function(){
  2294. this[n].apply(this,args);
  2295. });
  2296. };
  2297. }
  2298. );
  2299. jQuery.fn.getNiceScroll = function(index) {
  2300. if (typeof index == "undefined") {
  2301. return new NiceScrollArray(this);
  2302. } else {
  2303. var nice = $.data(this[index],'__nicescroll')||false;
  2304. return nice;
  2305. }
  2306. };
  2307. jQuery.extend(jQuery.expr[':'], {
  2308. nicescroll: function(a) {
  2309. return ($.data(a,'__nicescroll'))?true:false;
  2310. }
  2311. });
  2312. $.fn.niceScroll = function(wrapper,opt) {
  2313. if (typeof opt=="undefined") {
  2314. if ((typeof wrapper=="object")&&!("jquery" in wrapper)) {
  2315. opt = wrapper;
  2316. wrapper = false;
  2317. }
  2318. }
  2319. var ret = new NiceScrollArray();
  2320. if (typeof opt=="undefined") opt = {};
  2321. if (wrapper||false) {
  2322. opt.doc = $(wrapper);
  2323. opt.win = $(this);
  2324. }
  2325. var docundef = !("doc" in opt);
  2326. if (!docundef&&!("win" in opt)) opt.win = $(this);
  2327. this.each(function() {
  2328. var nice = $(this).data('__nicescroll')||false;
  2329. if (!nice) {
  2330. opt.doc = (docundef) ? $(this) : opt.doc;
  2331. nice = new NiceScrollClass(opt,$(this));
  2332. $(this).data('__nicescroll',nice);
  2333. }
  2334. ret.push(nice);
  2335. });
  2336. return (ret.length==1) ? ret[0] : ret;
  2337. };
  2338. window.NiceScroll = {
  2339. getjQuery:function(){return jQuery}
  2340. };
  2341. if (!$.nicescroll) {
  2342. $.nicescroll = new NiceScrollArray();
  2343. }
  2344. })( jQuery );