jquery.nicescroll.js 47 KB

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