jquery.nicescroll.js 114 KB

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