jquery.nicescroll.js 97 KB

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