jquery.nicescroll.js 45 KB

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