jquery.nicescroll.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /* jquery.nicescroll
  2. -- version 2.8.2
  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($){
  11. // globals
  12. var domfocus = false;
  13. var mousefocus = false;
  14. var zoomactive = false;
  15. var tabindexcounter = 5000;
  16. var ascrailcounter = 2000;
  17. // http://stackoverflow.com/questions/2161159/get-script-path
  18. function getScriptPath() {
  19. var scripts=document.getElementsByTagName('script');
  20. var path=scripts[scripts.length-1].src.split('?')[0];
  21. return (path.split('/').length>0) ? path.split('/').slice(0,-1).join('/')+'/' : '';
  22. }
  23. var scriptpath = getScriptPath();
  24. // derived by http://blog.joelambert.co.uk/2011/06/01/a-better-settimeoutsetinterval/
  25. var setAnimationFrame = (function(){
  26. return window.requestAnimationFrame ||
  27. window.webkitRequestAnimationFrame ||
  28. window.mozRequestAnimationFrame ||
  29. window.oRequestAnimationFrame ||
  30. window.msRequestAnimationFrame ||
  31. false;
  32. })();
  33. var clearAnimationFrame = (function(){
  34. return window.cancelRequestAnimationFrame ||
  35. window.webkitCancelRequestAnimationFrame ||
  36. window.mozCancelRequestAnimationFrame ||
  37. window.oCancelRequestAnimationFrame ||
  38. window.msCancelRequestAnimationFrame ||
  39. false;
  40. })();
  41. var NiceScrollClass = function(myopt,me) {
  42. var self = this;
  43. this.version = '2.8.2';
  44. this.name = 'nicescroll';
  45. this.me = me;
  46. this.opt = {
  47. doc:$("body"),
  48. win:false,
  49. zindex:9000,
  50. cursoropacitymin:0,
  51. cursoropacitymax:1,
  52. cursorcolor:"#424242",
  53. cursorwidth:"5px",
  54. cursorborder:"1px solid #fff",
  55. cursorborderradius:"5px",
  56. scrollspeed:60,
  57. mousescrollstep:8*5,
  58. touchbehavior:false,
  59. hwacceleration:true,
  60. usetransition:true,
  61. boxzoom:false,
  62. dblclickzoom:true,
  63. gesturezoom:true,
  64. grabcursorenabled:true,
  65. autohidemode:true,
  66. background:"",
  67. iframeautoresize:true,
  68. cursorminheight:20,
  69. preservenativescrolling:true,
  70. railoffset:false,
  71. bouncescroll:false
  72. };
  73. if (myopt||false) {
  74. for(var a in self.opt) {
  75. if (typeof myopt[a] != "undefined") self.opt[a] = myopt[a];
  76. }
  77. }
  78. this.doc = self.opt.doc;
  79. this.id = (this.doc&&this.doc[0])?this.doc[0].id||'':'';
  80. this.ispage = /BODY|HTML/.test((self.opt.win)?self.opt.win[0].nodeName:this.doc[0].nodeName);
  81. this.haswrapper = (self.opt.win!==false);
  82. this.win = self.opt.win||(this.ispage?$(window):this.doc);
  83. this.docscroll = (this.ispage&&!this.haswrapper)?$(window):this.win;
  84. this.body = $("body");
  85. this.iframe = false;
  86. this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName == 'IFRAME'));
  87. this.istextarea = (this.win[0].nodeName == 'TEXTAREA');
  88. // Events jump table
  89. this.onmousedown = false;
  90. this.onmouseup = false;
  91. this.onmousemove = false;
  92. this.onmousewheel = false;
  93. this.onkeypress = false;
  94. this.ongesturezoom = false;
  95. this.onclick = false;
  96. // Let's start!
  97. this.view = false;
  98. this.page = false;
  99. this.scroll = {x:0,y:0};
  100. this.scrollratio = {x:0,y:0};
  101. this.cursorheight = 20;
  102. this.scrollvaluemax = 0;
  103. this.scrollmom = false;
  104. do {
  105. this.id = "ascrail"+(ascrailcounter++);
  106. } while (document.getElementById(this.id));
  107. this.rail = false;
  108. this.cursor = false;
  109. this.cursorfreezed = false;
  110. this.zoom = false;
  111. this.zoomactive = false;
  112. this.hasfocus = false;
  113. this.hasmousefocus = false;
  114. this.visibility = true;
  115. this.locked = false;
  116. self.nativescrollingarea = false;
  117. this.events = []; // event list for unbind
  118. this.saved = {};
  119. this.delaylist = {};
  120. var domtest = document.createElement('DIV');
  121. this.isopera = ("opera" in window);
  122. this.isie = (("all" in document) && ("attachEvent" in domtest) && !this.isopera);
  123. this.isieold = (this.isie && !("msInterpolationMode" in domtest.style)); // IE6 and older
  124. this.isie7 = this.isie&&!this.isieold&&(!("documentMode" in document)||(document.documentMode==7));
  125. this.isie8 = this.isie&&("documentMode" in document)&&(document.documentMode==8);
  126. this.isie9 = this.isie&&("performance" in window)&&(document.documentMode>=9);
  127. this.ismozilla = ("MozAppearance" in domtest.style);
  128. this.ischrome = ("chrome" in window);
  129. this.cantouch = ("ontouchstart" in document.documentElement);
  130. this.isios = (this.cantouch && /iphone|ipad|ipod/i.test(navigator.platform));
  131. this.isios4 = ((this.isios)&&!("seal" in Object));
  132. if (self.opt.hwacceleration) { // if you dont need dont bother to look for
  133. this.trstyle = (window.opera) ? 'OTransform' : (document.all) ? 'msTransform' : (domtest.style.webkitTransform!==undefined) ? 'webkitTransform' : (domtest.style.MozTransform!==undefined) ? 'MozTransform' : false;
  134. if (this.trstyle && typeof domtest.style[this.trstyle] == "undefined") this.trstyle = false;
  135. this.hastransform = (this.trstyle != false);
  136. if (this.hastransform) {
  137. domtest.style[this.trstyle] = "translate3d(1px,2px,3px)";
  138. this.hastranslate3d = /translate3d/.test(domtest.style[this.trstyle]);
  139. }
  140. this.transitionstyle = false;
  141. this.prefixstyle = '';
  142. this.transitionend = false;
  143. var check = ['transition','webkitTransition','MozTransition','OTransition','msTransition','KhtmlTransition'];
  144. var prefix = ['','-webkit-','-moz-','-o-','-ms-','-khtml-'];
  145. var evs = ['transitionEnd','webkitTransitionEnd','transitionend','oTransitionEnd','msTransitionEnd','KhtmlTransitionEnd'];
  146. for(var a=0;a<check.length;a++) {
  147. if (check[a] in domtest.style) {
  148. this.transitionstyle = check[a];
  149. this.prefixstyle = prefix[a];
  150. this.transitionend = evs[a];
  151. break;
  152. }
  153. }
  154. this.hastransition = (this.transitionstyle);
  155. } else {
  156. this.trstyle = false;
  157. this.hastransform = false;
  158. this.hastranslate3d = false;
  159. this.transitionstyle = false;
  160. this.hastransition = false;
  161. this.transitionend = false;
  162. }
  163. this.cursorgrabvalue = '';
  164. if (self.opt.grabcursorenabled&&self.opt.touchbehavior) { // check grab cursor support
  165. function detectCursorGrab() {
  166. // thank you google for custom cursor!
  167. if (self.ischrome||(self.isie&&!self.isie9)) { // some old IE return false positive + chrome cursor bug
  168. var lst = ['url(http://www.google.com/intl/en_ALL/mapfiles/openhand.cur)','n-resize'];
  169. } else {
  170. var lst = ['grab','-moz-grab','-webkit-grab','url(http://www.google.com/intl/en_ALL/mapfiles/openhand.cur)','n-resize'];
  171. }
  172. for(var a=0;a<lst.length;a++) {
  173. var p = lst[a];
  174. domtest.style['cursor']=p;
  175. if (domtest.style['cursor']==p) return p;
  176. }
  177. return ''; // nope!
  178. }
  179. this.cursorgrabvalue = detectCursorGrab();
  180. }
  181. domtest = null; //memory released
  182. this.ishwscroll = (self.hastransform)&&(self.opt.hwacceleration)&&(self.haswrapper);
  183. this.delayed = function(name,fn,tm) {
  184. var dd = self.delaylist[name];
  185. var nw = (new Date()).getTime();
  186. if (dd&&dd.tt) return false;
  187. if (dd&&dd.last+tm>nw&&!dd.tt) {
  188. self.delaylist[name] = {
  189. last:nw+tm,
  190. tt:setTimeout(function(){self.delaylist[name].tt=0;fn.call();},tm)
  191. }
  192. }
  193. else if (!dd||!dd.tt) {
  194. self.delaylist[name] = {
  195. last:nw,
  196. tt:0
  197. }
  198. setTimeout(function(){fn.call();},0);
  199. }
  200. };
  201. this.css = function(el,pars) { // save & set
  202. for(var n in pars) {
  203. self.saved.css.push([el,n,el.css(n)]);
  204. el.css(n,pars[n]);
  205. }
  206. };
  207. this.scrollTop = function(val) {
  208. return (typeof val == "undefined") ? self.getScrollTop() : self.setScrollTop(val);
  209. };
  210. // derived by by Dan Pupius www.pupius.net
  211. BezierClass = function(st,ed,spd,p1,p2,p3,p4) {
  212. this.st = st;
  213. this.ed = ed;
  214. this.spd = spd;
  215. this.p1 = p1||0;
  216. this.p2 = p2||1;
  217. this.p3 = p3||0;
  218. this.p4 = p4||1;
  219. this.ts = (new Date()).getTime();
  220. this.df = this.ed-this.st;
  221. };
  222. BezierClass.prototype = {
  223. B2:function(t){ return 3*t*t*(1-t) },
  224. B3:function(t){ return 3*t*(1-t)*(1-t) },
  225. B4:function(t){ return (1-t)*(1-t)*(1-t) },
  226. getNow:function(){
  227. var nw = (new Date()).getTime();
  228. var pc = 1-((nw-this.ts)/this.spd);
  229. var bz = this.B2(pc) + this.B3(pc) + this.B4(pc);
  230. return (pc<0) ? this.ed : this.st+Math.round(this.df*bz);
  231. },
  232. update:function(ed,spd){
  233. this.st = this.getNow();
  234. this.ed = ed;
  235. this.spd = spd;
  236. this.ts = (new Date()).getTime();
  237. this.df = this.ed-this.st;
  238. return this;
  239. }
  240. };
  241. if (this.ishwscroll) {
  242. // hw accelerated scroll
  243. this.doc.translate = {x:0,y:0};
  244. this.getScrollTop = function(last) {
  245. if (self.timerscroll&&!last) {
  246. return self.timerscroll.bz.getNow();
  247. } else {
  248. return self.doc.translate.y;
  249. }
  250. };
  251. if (document.createEvent) {
  252. this.notifyScrollEvent = function(el) {
  253. var e = document.createEvent("UIEvents");
  254. e.initUIEvent("scroll", false, true, window, 1);
  255. el.dispatchEvent(e);
  256. };
  257. }
  258. else if (document.fireEvent) {
  259. this.notifyScrollEvent = function(el) {
  260. var e = document.createEventObject();
  261. el.fireEvent("onscroll");
  262. e.cancelBubble = true;
  263. };
  264. }
  265. else {
  266. this.notifyScrollEvent = function(el) {}; //NOPE
  267. }
  268. if (this.hastranslate3d) {
  269. this.setScrollTop = function(val,silent) {
  270. self.doc.css(self.trstyle,"translate3d(0px,"+(val*-1)+"px,0px)");
  271. self.doc.translate.y = val;
  272. if (!silent) self.notifyScrollEvent(self.win[0]);
  273. };
  274. } else {
  275. this.setScrollTop = function(val,silent) {
  276. self.doc.css(self.trstyle,"translate(0px,"+(val*-1)+"px)");
  277. self.doc.translate.y = val;
  278. if (!silent) self.notifyScrollEvent(self.win[0]);
  279. };
  280. }
  281. } else {
  282. // native scroll
  283. this.getScrollTop = function() {
  284. return self.docscroll.scrollTop();
  285. };
  286. this.setScrollTop = function(val) {
  287. return self.docscroll.scrollTop(val);
  288. };
  289. }
  290. this.getTarget = function(e) {
  291. if (!e) return false;
  292. if (e.target) return e.target;
  293. if (e.srcElement) return e.srcElement;
  294. return false;
  295. };
  296. this.hasParent = function(e,id) {
  297. if (!e) return false;
  298. var el = e.target||e.srcElement||e||false;
  299. while (el && el.id != id) {
  300. el = el.parentNode||false;
  301. }
  302. return (el!==false);
  303. };
  304. this.updateScrollBar = function(len) {
  305. if (self.ishwscroll) {
  306. self.rail.css({height:self.win.innerHeight()});
  307. } else {
  308. var pos = self.win.offset();
  309. pos.top+=2;
  310. var brd = (self.win.outerWidth() - self.win.innerWidth())/2;
  311. pos.left+= self.win.innerWidth() + brd - self.rail.width -1;
  312. var off = self.opt.railoffset;
  313. if (off) {
  314. if (off.top) pos.top+=off.top;
  315. if (off.left) pos.left+=off.left;
  316. }
  317. self.rail.css({top:pos.top,left:pos.left,height:(len)?len.h:self.win.innerHeight()});
  318. if (self.zoom) self.zoom.css({top:pos.top+1,left:pos.left-20});
  319. }
  320. };
  321. self.hasanimationframe = (setAnimationFrame);
  322. self.hascancelanimationframe = (clearAnimationFrame);
  323. if (!self.hasanimationframe) {
  324. setAnimationFrame=function(fn){return setTimeout(fn,1000/60)};
  325. clearAnimationFrame=clearInterval;
  326. }
  327. else if (!self.hascancelanimationframe) clearAnimationFrame=function(){self.cancelAnimationFrame=true};
  328. this.init = function() {
  329. self.saved.css = [];
  330. if (!self.ispage || (!self.cantouch && !self.isieold)) {
  331. var cont = self.docscroll;
  332. if (self.ispage) cont = (self.haswrapper)?self.win:self.doc;
  333. self.css(cont,{'overflow-y':'hidden'});
  334. if (self.ispage&&self.isie7&&self.win[0].nodeName=='BODY') self.css($("html"),{'overflow-y':'hidden'}); //IE7 double scrollbar issue
  335. var cursor = $(document.createElement('div'));
  336. cursor.css({
  337. position:"relative",top:0,"float":"right",width:self.opt.cursorwidth,height:"0px",
  338. 'background-color':self.opt.cursorcolor,
  339. border:self.opt.cursorborder,
  340. 'background-clip':'padding-box',
  341. '-webkit-border-radius':self.opt.cursorborderradius,
  342. '-moz-border-radius':self.opt.cursorborderradius,
  343. 'border-radius':self.opt.cursorborderradius
  344. });
  345. cursor.hborder = parseFloat(cursor.outerHeight() - cursor.innerHeight());
  346. self.cursor = cursor;
  347. var rail = $(document.createElement('div'));
  348. rail.attr('id',self.id);
  349. rail.width = 1+Math.max(parseFloat(self.opt.cursorwidth),cursor.outerWidth());
  350. rail.css({"padding-left":"0px","padding-right":"1px",width:rail.width+"px",'zIndex':(self.ispage)?self.opt.zindex:self.opt.zindex+2,"background":self.opt.background});
  351. rail.append(cursor);
  352. self.rail = rail;
  353. self.rail.drag = false;
  354. var zoom = false;
  355. if (self.opt.boxzoom&&!self.ispage&&!self.isieold) {
  356. zoom = document.createElement('div');
  357. self.bind(zoom,"click",self.doZoom);
  358. self.zoom = $(zoom);
  359. self.zoom.css({"cursor":"pointer",'z-index':self.opt.zindex,'backgroundImage':'url('+scriptpath+'zoomico.png)','height':18,'width':18,'backgroundPosition':'0px 0px'});
  360. if (self.opt.dblclickzoom) self.bind(self.win,"dblclick",self.doZoom);
  361. if (self.cantouch&&self.opt.gesturezoom) {
  362. self.ongesturezoom = function(e) {
  363. if (e.scale>1.5) self.doZoomIn(e);
  364. if (e.scale<0.8) self.doZoomOut(e);
  365. return self.cancelEvent(e);
  366. };
  367. self.bind(self.win,"gestureend",self.ongesturezoom);
  368. }
  369. }
  370. if (self.ispage) {
  371. rail.css({position:"fixed",top:"0px",right:"0px",height:"100%"});
  372. self.body.append(rail);
  373. } else {
  374. if (self.ishwscroll) {
  375. if (self.win.css('position')=='static') self.css(self.win,{'position':'relative'});
  376. var bd = (self.win[0].nodeName == 'HTML') ? self.body : self.win;
  377. if (self.zoom) {
  378. self.zoom.css({position:"absolute",top:1,right:0,"margin-right":rail.width+4});
  379. bd.append(self.zoom);
  380. }
  381. rail.css({position:"absolute",top:0,right:0});
  382. bd.append(rail);
  383. } else {
  384. rail.css({position:"absolute"});
  385. if (self.zoom) self.zoom.css({position:"absolute"});
  386. self.updateScrollBar();
  387. self.body.append(rail);
  388. if (self.zoom) self.body.append(self.zoom);
  389. }
  390. }
  391. if (self.opt.autohidemode===false) {
  392. self.autohidedom = false;
  393. }
  394. else if (self.opt.autohidemode===true) {
  395. self.autohidedom = self.rail;
  396. }
  397. else if (self.opt.autohidemode=="cursor") {
  398. self.autohidedom = self.cursor;
  399. }
  400. self.bind(window,'resize',self.onResize);
  401. self.bind(window,'orientationchange',self.onResize);
  402. /* DISABLED - No cross-browser implementation
  403. if (!self.ispage&&!self.haswrapper) {
  404. self.bind(self.win,(self.isie&&!self.isie9)?"propertychange":"DOMAttrModified",self.onAttributeChange);
  405. }
  406. */
  407. if (!self.ispage&&self.opt.boxzoom) self.bind(window,"resize",self.resizeZoom);
  408. if (self.istextarea) self.bind(self.win,"mouseup",self.onResize);
  409. self.onResize();
  410. if (self.cantouch||self.opt.touchbehavior) {
  411. self.scrollmom = {
  412. y:new ScrollMomentumClass(self)
  413. };
  414. self.onmousedown = function(e) {
  415. if (!self.locked) {
  416. self.cancelScroll();
  417. self.rail.drag = {x:e.screenX,y:e.screenY,sx:self.scroll.x,sy:self.scroll.y,st:self.getScrollTop()};
  418. self.hasmoving = false;
  419. self.scrollmom.y.reset(e.screenY);
  420. if (!self.cantouch) return self.cancelEvent(e);
  421. }
  422. };
  423. self.onmouseup = function(e) {
  424. if (self.rail.drag) {
  425. self.scrollmom.y.doMomentum();
  426. self.rail.drag = false;
  427. if (self.hasmoving) {
  428. // self.hasmoving = false;
  429. self.hideCursor();
  430. return self.cancelEvent(e);
  431. }
  432. }
  433. };
  434. self.onclick = function(e) {
  435. if (self.hasmoving) return self.cancelEvent(e);
  436. };
  437. self.onmousemove = function(e) {
  438. if (self.rail.drag) {
  439. self.hasmoving = true;
  440. var my = (e.screenY-self.rail.drag.y);
  441. var fy = e.screenY;
  442. var ny = self.rail.drag.st-my;
  443. if (self.ishwscroll) {
  444. if (ny<0) {
  445. ny = Math.round(ny/2);
  446. fy = 0;
  447. }
  448. else if (ny>self.page.maxh) {
  449. ny = self.page.maxh+Math.round((ny-self.page.maxh)/2);
  450. fy = 0;
  451. }
  452. } else {
  453. if (ny<0) ny=0;
  454. if (ny>self.page.maxh) ny=self.page.maxh;
  455. }
  456. self.showCursor(ny);
  457. if (self.prepareTransition) self.prepareTransition(0);
  458. self.setScrollTop(ny);
  459. self.scrollmom.y.update(fy);
  460. return self.cancelEvent(e);
  461. }
  462. };
  463. if (self.cursorgrabvalue) {
  464. self.css((self.ispage)?self.doc:self.win,{'cursor':self.cursorgrabvalue});
  465. self.css(self.rail,{'cursor':self.cursorgrabvalue});
  466. }
  467. } else {
  468. self.onmousedown = function(e) {
  469. if (self.locked) return self.cancelEvent(e);
  470. self.cancelScroll();
  471. self.rail.drag = {x:e.screenX,y:e.screenY,sx:self.scroll.x,sy:self.scroll.y};
  472. return self.cancelEvent(e);
  473. };
  474. self.onmouseup = function(e) {
  475. if (self.rail.drag) {
  476. self.rail.drag = false;
  477. return self.cancelEvent(e);
  478. }
  479. };
  480. self.onmousemove = function(e) {
  481. if (self.rail.drag) {
  482. self.scroll.y = self.rail.drag.sy + (e.screenY-self.rail.drag.y);
  483. if (self.scroll.y<0) self.scroll.y=0;
  484. var my = self.scrollvaluemax;
  485. if (self.scroll.y>my) self.scroll.y=my;
  486. self.showCursor();
  487. self.cursorfreezed = true;
  488. self.doScroll(Math.round(self.scroll.y*self.scrollratio.y));
  489. return self.cancelEvent(e);
  490. } else {
  491. self.checkarea = true;
  492. }
  493. };
  494. }
  495. if (self.cantouch||self.opt.touchbehavior) self.bind(self.win,"mousedown",self.onmousedown);
  496. self.bind(self.win,"mouseup",self.onmouseup);
  497. self.bind(self.cursor,"mousedown",self.onmousedown);
  498. self.bind(self.cursor,"mouseup",function(e) {
  499. self.rail.drag = false;
  500. self.hasmoving = false;
  501. self.hideCursor();
  502. return self.cancelEvent(e);
  503. });
  504. self.bind(document,"mouseup",self.onmouseup);
  505. self.bind(document,"mousemove",self.onmousemove);
  506. if (self.onclick) self.bind(document,"click",self.onclick);
  507. if (!self.cantouch) {
  508. self.rail.mouseenter(function() {
  509. self.showCursor();
  510. self.rail.active = true;
  511. });
  512. self.rail.mouseleave(function() {
  513. self.rail.active = false;
  514. if (!self.rail.drag) self.hideCursor();
  515. });
  516. if (!self.isiframe) self.bind((self.isie&&self.ispage) ? document : self.docscroll,"mousewheel",self.onmousewheel);
  517. self.bind(self.rail,"mousewheel",self.onmousewheel);
  518. }
  519. if (self.zoom) {
  520. self.zoom.mouseenter(function() {
  521. self.showCursor();
  522. self.rail.active = true;
  523. });
  524. self.zoom.mouseleave(function() {
  525. self.rail.active = false;
  526. if (!self.rail.drag) self.hideCursor();
  527. });
  528. }
  529. if (!self.ispage&&!self.cantouch&&!(/HTML|BODY/.test(self.win[0].nodeName))) {
  530. if (!self.win.attr("tabindex")) self.win.attr({"tabindex":tabindexcounter++});
  531. self.win.focus(function(e) {
  532. domfocus = (self.getTarget(e)).id||true;
  533. self.hasfocus = true;
  534. self.noticeCursor();
  535. });
  536. self.win.blur(function(e) {
  537. domfocus = false;
  538. self.hasfocus = false;
  539. });
  540. self.win.mouseenter(function(e) {
  541. mousefocus = (self.getTarget(e)).id||true;
  542. self.hasmousefocus = true;
  543. self.noticeCursor();
  544. });
  545. self.win.mouseleave(function() {
  546. mousefocus = false;
  547. self.hasmousefocus = false;
  548. });
  549. };
  550. //Thanks to http://www.quirksmode.org !!
  551. self.onkeypress = function(e) {
  552. if (self.locked&&self.page.maxh==0) return true;
  553. e = (e) ? e : window.e;
  554. var tg = self.getTarget(e);
  555. if (tg&&/INPUT|TEXTAREA|SELECT|OPTION/.test(tg.nodeName)) {
  556. var tp = tg.getAttribute('type')||tg.type||false;
  557. if ((!tp)||!(/submit|button|cancel/i.tp)) return true;
  558. }
  559. if (self.hasfocus||(self.hasmousefocus&&!domfocus)||(self.ispage&&!domfocus&&!mousefocus)) {
  560. var key = e.keyCode;
  561. if (self.locked&&key!=27) return self.cancelEvent(e);
  562. var ret = false;
  563. switch (key) {
  564. case 38:
  565. case 63233: //safari
  566. self.doScrollBy(24*3);
  567. ret = true;
  568. break;
  569. case 40:
  570. case 63235: //safari
  571. self.doScrollBy(-24*3);
  572. ret = true;
  573. break;
  574. case 33:
  575. case 63276: // safari
  576. self.doScrollBy(self.view.h);
  577. ret = true;
  578. break;
  579. case 34:
  580. case 63277: // safari
  581. self.doScrollBy(-self.view.h);
  582. ret = true;
  583. break;
  584. case 36:
  585. case 63273: // safari
  586. self.doScrollTo(0);
  587. ret = true;
  588. break;
  589. case 35:
  590. case 63275: // safari
  591. self.doScrollTo(self.page.maxh);
  592. ret = true;
  593. break;
  594. case 27: // ESC
  595. if (self.zoomactive) {
  596. self.doZoom();
  597. ret = true;
  598. }
  599. break;
  600. }
  601. if (ret) return self.cancelEvent(e);
  602. }
  603. };
  604. self.bind(document,(self.isopera)?"keypress":"keydown",self.onkeypress);
  605. }
  606. if (this.doc[0].nodeName == 'IFRAME') {
  607. function oniframeload(e) {
  608. self.iframexd = false;
  609. try {
  610. var doc = 'contentDocument' in this ? this.contentDocument : this.contentWindow.document;
  611. var a = doc.domain;
  612. } catch(e){self.iframexd = true;doc=false};
  613. if (self.iframexd) return true; //cross-domain - I can't manage this
  614. if (self.isiframe) {
  615. self.iframe = {
  616. html:self.doc.contents().find('html')[0],
  617. body:self.doc.contents().find('body')[0]
  618. };
  619. self.docscroll = $(this.contentWindow);
  620. }
  621. if (self.opt.iframeautoresize&&!self.isiframe) {
  622. self.win.scrollTop(0); // reset position
  623. self.doc.height(""); //reset height to fix browser bug
  624. var hh=Math.max(doc.getElementsByTagName('html')[0].scrollHeight,doc.body.scrollHeight);
  625. self.doc.height(hh);
  626. }
  627. self.onResize();
  628. if (self.isie7) self.css($(doc).find('html'),{'overflow-y':'hidden'});
  629. self.css($(doc.body),{'overflow-y':'hidden'});
  630. if ('contentWindow' in this) {
  631. self.bind(this.contentWindow,"scroll",self.onscroll); //IE8 & minor
  632. } else {
  633. self.bind(doc,"scroll",self.onscroll);
  634. }
  635. self.bind(doc,"mouseup",self.onmouseup);
  636. self.bind(doc,"mousewheel",self.onmousewheel);
  637. self.bind(doc,(self.isopera)?"keypress":"keydown",self.onkeypress);
  638. if (self.cantouch||self.opt.touchbehavior) {
  639. self.bind(doc,"mousedown",self.onmousedown);
  640. if (self.cursorgrabvalue) self.css($(doc.body),{'cursor':self.cursorgrabvalue});
  641. }
  642. self.bind(doc,"mousemove",self.onmousemove);
  643. if (self.zoom) {
  644. if (self.opt.dblclickzoom) self.bind(doc,'dblclick',self.doZoom);
  645. if (self.ongesturezoom) self.bind(doc,"gestureend",self.ongesturezoom);
  646. }
  647. };
  648. if (this.doc[0].readyState&&this.doc[0].readyState=="complete"){
  649. setTimeout(function(){oniframeload.call(self.doc[0],false)},500);
  650. }
  651. self.bind(this.doc,"load",oniframeload);
  652. }
  653. };
  654. this.showCursor = function(py) {
  655. if (self.cursortimeout) {
  656. clearTimeout(self.cursortimeout);
  657. self.cursortimeout = 0;
  658. }
  659. if (!self.rail) return;
  660. if (self.autohidedom) self.autohidedom.stop().css({opacity:self.opt.cursoropacitymax});
  661. if (typeof py != "undefined") {
  662. self.scroll.y = Math.round(py * 1/self.scrollratio.y);
  663. }
  664. self.cursor.css({height:self.cursorheight,top:self.scroll.y});
  665. if (self.zoom) self.zoom.stop().css({opacity:self.opt.cursoropacitymax});
  666. };
  667. this.hideCursor = function(tm) {
  668. if (self.cursortimeout) return;
  669. if (!self.rail) return;
  670. if (!self.autohidedom) return;
  671. self.cursortimeout = setTimeout(function() {
  672. if (!self.rail.active) {
  673. self.autohidedom.stop().animate({opacity:self.opt.cursoropacitymin});
  674. if (self.zoom) self.zoom.stop().animate({opacity:self.opt.cursoropacitymin});
  675. }
  676. self.cursortimeout = 0;
  677. },tm||400);
  678. };
  679. this.noticeCursor = function(tm,py) {
  680. self.showCursor(py);
  681. self.hideCursor(tm);
  682. };
  683. this.getContentSize = function() {
  684. var pg =
  685. (self.ispage) ?
  686. {
  687. w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),
  688. h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)
  689. }
  690. : (self.haswrapper) ?
  691. {
  692. w:self.doc.outerWidth()+parseInt(self.win.css('paddingLeft'))+parseInt(self.win.css('paddingRight')),
  693. h:self.doc.outerHeight()+parseInt(self.win.css('paddingTop'))+parseInt(self.win.css('paddingBottom'))
  694. }
  695. : (self.iframe) ?
  696. {
  697. w:Math.max(self.iframe.html.scrollWidth,self.iframe.body.scrollWidth),
  698. h:Math.max(self.iframe.html.scrollHeight,self.iframe.body.scrollHeight)
  699. }
  700. :
  701. {
  702. w:self.docscroll[0].scrollWidth,
  703. h:self.docscroll[0].scrollHeight
  704. };
  705. return pg;
  706. };
  707. this.onResize = function(e,page) {
  708. if (!self.haswrapper&&!self.ispage) {
  709. var vis = (self.win.css('display')!='none');
  710. if (vis&&!self.visibility) self.show();
  711. if (!vis&&self.visibility) self.hide();
  712. if (!self.visibility) return false;
  713. }
  714. self.view = {
  715. w:(self.ispage) ? self.win.width() : self.win.innerWidth(),
  716. h:(self.ispage) ? self.win.height() : self.win.innerHeight()
  717. };
  718. self.page = (page) ? page : self.getContentSize();
  719. self.page.maxh = Math.max(0,self.page.h - self.view.h);
  720. if (self.page.maxh==0) {
  721. self.hide();
  722. self.scrollvaluemax = 0;
  723. self.scroll.y = 0;
  724. self.scrollratio = {x:0,y:0};
  725. self.cursorheight = 0;
  726. self.locked = true;
  727. self.setScrollTop(0);
  728. return false;
  729. }
  730. else if (!self.visibility) self.show();
  731. self.locked = false;
  732. if (self.istextarea&&self.win.css('resize')&&self.win.css('resize')!='none') self.view.h-=20;
  733. if (!self.ispage) self.updateScrollBar(self.view);
  734. self.cursorheight = Math.min(self.view.h,Math.round(self.view.h * (self.view.h / self.page.h)));
  735. self.cursorheight = Math.max(self.opt.cursorminheight,self.cursorheight);
  736. self.scrollvaluemax = self.view.h-self.cursorheight-self.cursor.hborder;
  737. self.scrollratio = {
  738. x:0,
  739. y:(self.page.maxh/self.scrollvaluemax)
  740. };
  741. var sy = self.getScrollTop();
  742. if (sy>self.page.maxh) {
  743. self.doScroll(self.page.maxh);
  744. } else {
  745. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  746. self.noticeCursor();
  747. }
  748. return self;
  749. };
  750. this.resize = this.onResize; // mask internal method -- in future name can change
  751. this._bind = function(el,name,fn,bubble) { // primitive bind
  752. self.events.push({e:el,n:name,f:fn});
  753. if (el.addEventListener) {
  754. el.addEventListener(name,fn,bubble||false);
  755. }
  756. else if (el.attachEvent) {
  757. el.attachEvent("on"+name,fn);
  758. }
  759. else {
  760. el["on"+name] = fn;
  761. }
  762. };
  763. this.bind = function(dom,name,fn,bubble) { // touch-oriented & fixing jquery bind
  764. var el = ("jquery" in dom) ? dom[0] : dom;
  765. if (el.addEventListener) {
  766. if (self.cantouch && /mouseup|mousedown|mousemove/.test(name)) { // touch device support
  767. var tt = (name=='mousedown')?'touchstart':(name=='mouseup')?'touchend':'touchmove';
  768. self._bind(el,tt,function(e){
  769. if(e.touches.length<2){var ev=(e.touches.length>0)?e.touches[0]:e;ev.original=e;fn.call(this,ev);}
  770. },bubble||false);
  771. }
  772. self._bind(el,name,fn,bubble||false);
  773. if (name=='mousewheel') self._bind(el,"DOMMouseScroll",fn,bubble||false);
  774. if (self.cantouch && name=="mouseup") self._bind(el,"touchcancel",fn,bubble||false);
  775. }
  776. else {
  777. self._bind(el,name,function(e) {
  778. e = e||window.event||false;
  779. if (e) {
  780. if (e.srcElement) e.target=e.srcElement;
  781. }
  782. return ((fn.call(el,e)===false)||bubble===false) ? self.cancelEvent(e) : true;
  783. });
  784. }
  785. };
  786. this._unbind = function(el,name,fn) { // primitive unbind
  787. if (el.removeEventListener) {
  788. el.removeEventListener(name,fn,false);
  789. }
  790. else if (el.detachEvent) {
  791. el.detachEvent('on'+name,fn);
  792. } else {
  793. el['on'+name] = false;
  794. }
  795. };
  796. this.unbindAll = function() {
  797. for(var a=0;a<self.events.length;a++) {
  798. var r = self.events[a];
  799. self._unbind(r.e,r.n,r.f);
  800. }
  801. };
  802. // Thanks to http://www.switchonthecode.com !!
  803. this.cancelEvent = function(e) {
  804. if (self.cantouch) {
  805. e = e.original ? e.original : e||false;
  806. } else {
  807. e = e ? e : window.event||false;
  808. }
  809. if (!e) return false;
  810. if(e.stopPropagation) e.stopPropagation();
  811. if(e.preventDefault) e.preventDefault();
  812. e.cancelBubble = true;
  813. e.cancel = true;
  814. e.returnValue = false;
  815. return false;
  816. };
  817. this.show = function() {
  818. self.visibility = true;
  819. self.rail.css('display','block');
  820. return self;
  821. };
  822. this.hide = function() {
  823. self.visibility = false;
  824. self.rail.css('display','none');
  825. return self;
  826. };
  827. this.remove = function() {
  828. self.doZoomOut();
  829. self.unbindAll();
  830. self.events = [];
  831. self.rail.remove();
  832. if (self.zoom) self.zoom.remove();
  833. self.cursor = false;
  834. self.rail = false;
  835. self.zoom = false;
  836. for(var a=0;a<self.saved.css.length;a++) {
  837. var d=self.saved.css[a];
  838. d[0].css(d[1],(typeof d[2]=="undefined") ? '' : d[2]);
  839. }
  840. self.saved = false;
  841. self.me.data('__nicescroll',''); //erase all traces
  842. return self;
  843. };
  844. this.isScrollable = function(e) {
  845. var dom = (e.target) ? e.target : e;
  846. while (dom&&dom.nodeName&&!(/BODY|HTML/.test(dom.nodeName))) {
  847. var dd = $(dom);
  848. var ov = dd.css('overflowY')||dd.css('overflow')||'';
  849. if (/scroll|auto/.test(ov)) return (dom.clientHeight!=dom.scrollHeight);
  850. dom = (dom.parentNode) ? dom.parentNode : false;
  851. }
  852. return false;
  853. };
  854. this.onmousewheel = function(e) {
  855. if (self.locked&&self.page.maxh==0) return true;
  856. if (self.opt.preservenativescrolling&&self.checkarea) {
  857. self.checkarea = false;
  858. self.nativescrollingarea = self.isScrollable(e);
  859. }
  860. if (self.nativescrollingarea) return true; // it's not my business
  861. if (self.locked) return self.cancelEvent(e);
  862. if (self.rail.drag) return self.cancelEvent(e);
  863. var delta = 0;
  864. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  865. if (delta) {
  866. if (self.scrollmom) self.scrollmom.y.stop();
  867. self.doScrollBy(delta*self.opt.mousescrollstep);
  868. }
  869. return self.cancelEvent(e);
  870. };
  871. this.stop = function() {
  872. self.cancelScroll();
  873. if (self.scrollmon) self.scrollmon.stop();
  874. self.cursorfreezed = false;
  875. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  876. self.noticeCursor();
  877. return self;
  878. };
  879. if (self.ishwscroll&&self.hastransition&&self.opt.usetransition) {
  880. this.prepareTransition = function(dif,trend) {
  881. var sp = Math.round(self.opt.scrollspeed*10);
  882. var ex = Math.min(sp,Math.round((dif / 20) * self.opt.scrollspeed));
  883. var trans = (ex>20) ? self.prefixstyle+'transform '+ex+'ms ease-out 0s' : '';
  884. if (!self.lasttransitionstyle||self.lasttransitionstyle!=trans) {
  885. self.lasttransitionstyle = trans;
  886. self.doc.css(self.transitionstyle,trans);
  887. }
  888. return ex;
  889. };
  890. this.doScroll = function(y,spd) { //trans
  891. var sy = self.getScrollTop();
  892. if (y<0&&sy<=0) return self.noticeCursor();
  893. else if (y>self.page.maxh&&sy>=self.page.maxh) {
  894. self.checkContentSize();
  895. return self.noticeCursor();
  896. }
  897. self.newscrolly = y;
  898. self.newscrollspeed = spd||false;
  899. if (self.timer) return false;
  900. if (!self.scrollendtrapped) {
  901. self.scrollendtrapped = true;
  902. self.bind(self.doc,self.transitionend,self.onScrollEnd,false); //I have got to do something usefull!!
  903. }
  904. self.timer = setTimeout(function(){
  905. var top = self.getScrollTop();
  906. var dif = (self.newscrollspeed)?self.newscrollspeed:Math.abs(top-self.newscrolly);
  907. var ms = self.prepareTransition(dif);
  908. self.timer = setTimeout(function(){
  909. if (self.newscrolly<0&&!self.opt.bouncescroll) self.newscrolly = 0
  910. else if (self.newscrolly>self.page.maxh&&!self.opt.bouncescroll) self.newscrolly = self.page.maxh;
  911. if (self.newscrolly==self.getScrollTop()) {
  912. self.timer = 0;
  913. self.onScrollEnd();
  914. } else {
  915. var py = self.getScrollTop();
  916. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  917. if (ms>0) {
  918. self.timerscroll = {
  919. ts:(new Date()).getTime(),
  920. s:self.getScrollTop(),
  921. e:self.newscrolly,
  922. sp:ms,
  923. bz: new BezierClass(py,self.newscrolly,ms,0,1,0,1)
  924. };
  925. if (!self.cursorfreezed) self.timerscroll.tm=setInterval(function(){self.showCursor(self.getScrollTop())},60);
  926. }
  927. self.setScrollTop(self.newscrolly);
  928. self.timer = 0;
  929. }
  930. // self.noticeCursor();
  931. },15);
  932. },self.opt.scrollspeed);
  933. };
  934. this.cancelScroll = function() {
  935. if (!self.scrollendtrapped) return true;
  936. var py = self.getScrollTop();
  937. self.scrollendtrapped = false;
  938. self._unbind(self.doc,self.transitionend,self.onScrollEnd);
  939. self.prepareTransition(0);
  940. self.setScrollTop(py); // fire event onscroll
  941. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  942. self.timerscroll = false;
  943. self.cursorfreezed = false;
  944. // self.scrollstart = false;
  945. self.noticeCursor(false,py);
  946. return self;
  947. };
  948. this.onScrollEnd = function() {
  949. self.scrollendtrapped = false;
  950. self._unbind(self.doc,self.transitionend,self.onScrollEnd);
  951. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  952. self.timerscroll = false;
  953. self.cursorfreezed = false;
  954. var py = self.getScrollTop();
  955. self.setScrollTop(py); // fire event onscroll
  956. self.noticeCursor(false,py);
  957. if (py<0) self.doScroll(0,60)
  958. else if (py>self.page.maxh) self.doScroll(self.page.maxh,60);
  959. // else self.checkContentSize();
  960. };
  961. } else {
  962. this.doScroll = function(y) { //no-trans
  963. var py = self.getScrollTop();
  964. self.newscrolly = y;
  965. if (!self.bouncescroll) {
  966. if (self.newscrolly<0) {
  967. if (self.newspeedy) self.newspeedy.x = 0;
  968. self.newscrolly = 0;
  969. }
  970. else if (self.newscrolly>self.page.maxh) {
  971. if (self.newspeedy) self.newspeedy.x = self.page.maxh;
  972. self.newscrolly = self.page.maxh;
  973. }
  974. }
  975. var mg = Math.floor(Math.abs(y-py)/40);
  976. if (mg>0) {
  977. var ms = Math.min(10,mg)*100;
  978. self.bzscroll = (self.bzscroll) ? self.bzscroll.update(y,ms) : new BezierClass(py,y,ms,0,1,0,1);
  979. } else {
  980. self.bzscroll = false;
  981. }
  982. if (self.timer) return;
  983. if (py==self.page.maxh&&y>=self.page.maxh) self.checkContentSize();
  984. var sync = 1;
  985. function scrolling() {
  986. if (self.cancelAnimationFrame) return true;
  987. sync = 1-sync;
  988. if (sync) return (self.timer = setAnimationFrame(scrolling)||1);
  989. var sy = self.getScrollTop();
  990. var sc = (self.bzscroll) ? self.bzscroll.getNow() : self.newscrolly;
  991. var dr=sc-sy;
  992. if ((dr<0&&sc<self.newscrolly)||(dr>0&&sc>self.newscrolly)) sc = self.newscrolly;
  993. self.setScrollTop(sc);
  994. if (sc == self.newscrolly) {
  995. // clearAnimationFrame(self.timer);
  996. self.timer = 0;
  997. self.cursorfreezed = false;
  998. self.bzscroll = false;
  999. if (sc<0) self.doScroll(0);
  1000. else if (sc>self.page.maxh) self.doScroll(self.page.maxh);
  1001. // else self.checkContentSize();
  1002. } else {
  1003. self.timer = setAnimationFrame(scrolling)||1;
  1004. }
  1005. };
  1006. self.cancelAnimationFrame=false;
  1007. self.timer = 1;
  1008. scrolling();
  1009. if (py==self.page.maxh&&y>=py) self.checkContentSize();
  1010. self.noticeCursor();
  1011. };
  1012. this.cancelScroll = function() {
  1013. if (self.timer) clearAnimationFrame(self.timer);
  1014. self.timer = 0;
  1015. self.bzscroll = false;
  1016. return self;
  1017. };
  1018. }
  1019. this.doScrollBy = function(stp,relative) {
  1020. var ny = 0;
  1021. if (relative) {
  1022. ny = Math.floor((self.scroll.y-stp)*self.scrollratio.y)
  1023. } else {
  1024. var sy = (self.timer) ? self.newscrolly : self.getScrollTop(true);
  1025. ny = sy-stp;
  1026. }
  1027. if (self.bouncescroll) {
  1028. var haf = Math.round(self.view.h/2);
  1029. if (ny<-haf) ny=-haf
  1030. else if (ny>(self.page.maxh+haf)) ny = (self.page.maxh+haf);
  1031. }
  1032. self.cursorfreezed = false;
  1033. self.doScroll(ny);
  1034. };
  1035. this.doScrollTo = function(pos,relative) {
  1036. var ny = (relative) ? Math.round(pos*self.scrollratio.y) : pos;
  1037. if (ny<0) ny=0
  1038. else if (ny>self.page.maxh) ny = self.page.maxh;
  1039. self.cursorfreezed = false;
  1040. self.doScroll(pos);
  1041. };
  1042. this.checkContentSize = function() {
  1043. var pg = self.getContentSize();
  1044. if (pg.h!=self.page.h) self.resize(false,pg);
  1045. };
  1046. self.onscroll = function(e) {
  1047. if (self.rail.drag) return;
  1048. if (!self.cursorfreezed) {
  1049. self.delayed('onscroll',function(){
  1050. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1051. self.noticeCursor();
  1052. },30);
  1053. }
  1054. };
  1055. self.bind(self.docscroll,"scroll",self.onscroll);
  1056. this.doZoomIn = function(e) {
  1057. if (self.zoomactive) return;
  1058. self.zoomactive = true;
  1059. self.zoomrestore = {
  1060. style:{}
  1061. };
  1062. var lst = ['position','top','left','zIndex','backgroundColor','marginTop','marginBottom','marginLeft','marginRight'];
  1063. var win = self.win[0].style;
  1064. for(var a in lst) {
  1065. var pp = lst[a];
  1066. self.zoomrestore.style[pp] = (typeof win[pp]!='undefined') ? win[pp] : '';
  1067. }
  1068. self.zoomrestore.style.width = self.win.css('width');
  1069. self.zoomrestore.style.height = self.win.css('height');
  1070. self.zoomrestore.padding = {
  1071. w:self.win.outerWidth()-self.win.width(),
  1072. h:self.win.outerHeight()-self.win.height()
  1073. };
  1074. if (self.isios4) {
  1075. self.zoomrestore.scrollTop = $(window).scrollTop();
  1076. $(window).scrollTop(0);
  1077. }
  1078. self.win.css({
  1079. "position":(self.isios4)?"absolute":"fixed",
  1080. "top":0,
  1081. "left":0,
  1082. "z-index":self.opt.zindex+100,
  1083. "margin":"0px"
  1084. });
  1085. var bkg = self.win.css("backgroundColor");
  1086. if (bkg==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor","#fff");
  1087. self.rail.css({"z-index":self.opt.zindex+110});
  1088. self.zoom.css({"z-index":self.opt.zindex+112});
  1089. self.zoom.css('backgroundPosition','0px -18px');
  1090. self.resizeZoom();
  1091. return self.cancelEvent(e);
  1092. };
  1093. this.doZoomOut = function(e) {
  1094. if (!self.zoomactive) return;
  1095. self.zoomactive = false;
  1096. self.win.css("margin","");
  1097. self.win.css(self.zoomrestore.style);
  1098. if (self.isios4) {
  1099. $(window).scrollTop(self.zoomrestore.scrollTop);
  1100. }
  1101. self.rail.css({"z-index":(self.ispage)?self.opt.zindex:self.opt.zindex+2});
  1102. self.zoom.css({"z-index":self.opt.zindex});
  1103. self.zoomrestore = false;
  1104. self.zoom.css('backgroundPosition','0px 0px');
  1105. self.onResize();
  1106. return self.cancelEvent(e);
  1107. };
  1108. this.doZoom = function(e) {
  1109. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  1110. };
  1111. this.resizeZoom = function() {
  1112. if (!self.zoomactive) return;
  1113. var py = self.getScrollTop(); //preserve scrolling position
  1114. self.win.css({
  1115. width:$(window).width()-self.zoomrestore.padding.w+"px",
  1116. height:$(window).height()-self.zoomrestore.padding.h+"px"
  1117. });
  1118. self.onResize();
  1119. self.setScrollTop(Math.min(self.page.maxh,py));
  1120. };
  1121. this.init();
  1122. };
  1123. // Inspired by the work of Kin Blas
  1124. // http://webpro.host.adobe.com/people/jblas/momentum/includes/jquery.momentum.0.7.js
  1125. var ScrollMomentumClass = function(nc) {
  1126. var self = this;
  1127. this.nc = nc;
  1128. this.lasty = 0;
  1129. this.speedy = 0;
  1130. this.lasttime = 0;
  1131. this.snapy = false;
  1132. this.demuly = 0;
  1133. this.timer = 0;
  1134. this.time = function() {
  1135. return (new Date()).getTime();
  1136. };
  1137. this.reset = function(py) {
  1138. self.stop();
  1139. self.lasttime = self.time();
  1140. self.speedy = 0;
  1141. self.lasty = py;
  1142. };
  1143. this.update = function(py) {
  1144. self.lasttime = self.time();
  1145. var dy = py - self.lasty;
  1146. var sy = nc.getScrollTop();
  1147. var newy = sy + dy;
  1148. self.snapy = (newy<0)||(newy>self.nc.page.maxh);
  1149. self.speedy = dy;
  1150. self.lasty = py;
  1151. };
  1152. this.stop = function() {
  1153. if (self.timer) {
  1154. clearTimeout(self.timer);
  1155. self.timer = 0;
  1156. }
  1157. };
  1158. this.doSnapy = function(ny) {
  1159. if (ny<0) {
  1160. self.nc.doScroll(0,60);
  1161. }
  1162. else if (ny>self.nc.page.maxh) {
  1163. self.nc.doScroll(self.nc.page.maxh,60);
  1164. }
  1165. };
  1166. this.doMomentum = function() {
  1167. var l = self.lasttime;
  1168. var t = self.time();
  1169. self.speedy = Math.min(60,self.speedy);
  1170. var chk = l && (t - l) <= 50;
  1171. var sy = (self.speedy && chk) ? self.speedy : false;
  1172. if (sy) {
  1173. var tm = t-l;
  1174. var pageh = self.nc.page.maxh;
  1175. self.demuly = 0;
  1176. var onscroll = function(){
  1177. var ny = Math.floor(self.nc.getScrollTop() - (self.speedy*(1-self.demuly)));
  1178. if ((ny<0)||(ny>pageh)) {
  1179. self.demuly+=0.08;
  1180. } else {
  1181. self.demuly+=0.01;
  1182. }
  1183. self.nc.setScrollTop(ny);
  1184. self.nc.showCursor(ny);
  1185. if(self.demuly<1) {
  1186. self.timer = setTimeout(onscroll,tm);
  1187. } else {
  1188. self.timer = 0;
  1189. self.nc.hideCursor();
  1190. self.doSnapy(ny);
  1191. }
  1192. };
  1193. onscroll();
  1194. } else {
  1195. if (self.snapy) {
  1196. self.doSnapy(self.nc.getScrollTop());
  1197. }
  1198. }
  1199. }
  1200. };
  1201. // override jQuery scrollTop
  1202. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  1203. $.cssHooks.scrollTop = {
  1204. get: function(elem,computed,extra) {
  1205. var nice = $.data(elem,'__nicescroll')||false;
  1206. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  1207. },
  1208. set: function(elem,value) {
  1209. var nice = $.data(elem,'__nicescroll')||false;
  1210. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem,value);
  1211. return this;
  1212. }
  1213. };
  1214. $.fx.step["scrollTop"] = function(fx){
  1215. if (fx.start=='') fx.start=$.cssHooks.scrollTop.get(fx.elem);
  1216. $.cssHooks.scrollTop.set(fx.elem,fx.now+fx.unit);
  1217. };
  1218. jQuery.fn.scrollTop = function(value) {
  1219. if (typeof value == "undefined") {
  1220. var nice = (this[0]) ? $.data(this[0],'__nicescroll')||false : false;
  1221. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  1222. } else {
  1223. return this.each(function() {
  1224. var nice = $.data(this,'__nicescroll')||false;
  1225. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this),value);
  1226. });
  1227. }
  1228. }
  1229. var NiceScrollArray = function(doms) {
  1230. var self = this;
  1231. this.length = 0;
  1232. this.name = "nicescrollarray";
  1233. this.each = function(fn) {
  1234. for(var a=0;a<self.length;a++) fn.call(self[a]);
  1235. return self;
  1236. };
  1237. this.push = function(nice) {
  1238. self[self.length]=nice;
  1239. self.length++;
  1240. };
  1241. this.eq = function(idx) {
  1242. return self[idx];
  1243. };
  1244. if (doms) {
  1245. for(a=0;a<doms.length;a++) {
  1246. var nice = $.data(doms[a],'__nicescroll')||false;
  1247. if (nice) {
  1248. this[this.length]=nice;
  1249. this.length++;
  1250. }
  1251. };
  1252. }
  1253. return this;
  1254. };
  1255. function mplex(el,lst,fn) {
  1256. for(var a=0;a<lst.length;a++) fn(el,lst[a]);
  1257. };
  1258. mplex(
  1259. NiceScrollArray.prototype,
  1260. ['show','hide','onResize','resize','remove','stop'],
  1261. function(e,n) {
  1262. e[n] = function(){
  1263. return this.each(function(){
  1264. this[n].call();
  1265. });
  1266. };
  1267. }
  1268. );
  1269. jQuery.fn.getNiceScroll = function(index) {
  1270. if (typeof index == "undefined") {
  1271. return new NiceScrollArray(this);
  1272. } else {
  1273. var nice = $.data(this[index],'__nicescroll')||false;
  1274. return nice;
  1275. }
  1276. };
  1277. jQuery.extend(jQuery.expr[':'], {
  1278. nicescroll: function(a) {
  1279. return ($.data(a,'__nicescroll'))?true:false;
  1280. }
  1281. });
  1282. $.fn.niceScroll = function(wrapper,opt) {
  1283. if (typeof opt=="undefined") {
  1284. if ((typeof wrapper=="object")&&!("jquery" in wrapper)) {
  1285. opt = wrapper;
  1286. wrapper = false;
  1287. }
  1288. }
  1289. var ret = new NiceScrollArray();
  1290. if (typeof opt=="undefined") opt = {};
  1291. if (wrapper||false) {
  1292. opt.doc = $(wrapper);
  1293. opt.win = $(this);
  1294. }
  1295. var docundef = !("doc" in opt);
  1296. if (!docundef&&!("win" in opt)) opt.win = $(this);
  1297. this.each(function() {
  1298. var nice = $(this).data('__nicescroll')||false;
  1299. if (!nice) {
  1300. opt.doc = (docundef) ? $(this) : opt.doc;
  1301. nice = new NiceScrollClass(opt,$(this));
  1302. $(this).data('__nicescroll',nice);
  1303. }
  1304. ret.push(nice);
  1305. });
  1306. return (ret.length==1) ? ret[0] : ret;
  1307. };
  1308. })( jQuery );