jquery.nicescroll.js 96 KB

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