jquery.nicescroll.js 99 KB

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