jquery.nicescroll.js 93 KB

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