jquery.nicescroll.js 110 KB

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