jquery.nicescroll.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /* jquery.nicescroll
  2. -- versione 2.0.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. var domfocus = false;
  12. var mousefocus = false;
  13. var zoomactive = false;
  14. // http://stackoverflow.com/questions/2161159/get-script-path
  15. function getScriptPath() {
  16. var scripts= document.getElementsByTagName('script');
  17. var path= scripts[scripts.length-1].src.split('?')[0];
  18. return (path.split('/').length>0) ? path.split('/').slice(0, -1).join('/')+'/' : '';
  19. }
  20. var scriptpath = getScriptPath();
  21. var NiceScrollClass = function(myopt) {
  22. var self = this;
  23. this.opt = {
  24. doc:$("body"),
  25. win:false,
  26. zindex:9000,
  27. cursoropacitymin:0,
  28. cursoropacitymax:1,
  29. cursorcolor:"#424242",
  30. cursorwidth:"5px",
  31. cursorborder:"1px solid #fff",
  32. cursorborderradius:"4px",
  33. scrollspeed:60,
  34. mousescrollstep:8*6,
  35. touchbehavior:false,
  36. hwacceleration:true,
  37. boxzoom:false,
  38. dblclickzoom:true,
  39. gesturezoom:true
  40. };
  41. if (myopt||false) {
  42. for(var a in self.opt) {
  43. if (myopt[a]!==undefined) self.opt[a] = myopt[a];
  44. }
  45. }
  46. this.id = self.opt.doc[0].id||'';
  47. this.doc = self.opt.doc;
  48. this.ispage = (self.doc[0].nodeName=='BODY'||self.doc[0].nodeName=='HTML');
  49. this.haswrapper = (self.opt.win!==false);
  50. this.win = self.opt.win||(this.ispage?$(window):this.doc);
  51. this.docscroll = this.ispage?$(window):this.win;
  52. this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName == 'IFRAME'));
  53. /*
  54. if (this.isiframe) {
  55. this.docscroll = (this.doc[0].document)?$(this.doc[0].document.body):this.doc;
  56. this.doc.load(function() {
  57. var doc = 'contentDocument' in this? this.contentDocument : this.contentWindow.document;
  58. self.docscroll = $(doc.body);
  59. self.onResize();
  60. $(doc.body).css({'overflow-y':'hidden'});
  61. $(doc).scroll(self.onscroll);
  62. $(doc).mouseup(function() {self.rail.drag = false;});
  63. self.bind(doc,"mousewheel",self.onmousewheel);
  64. $(doc).keydown(self.onkeypress);
  65. });
  66. }
  67. else if (this.doc[0].nodeName == 'IFRAME') {
  68. this.doc.load(function() {
  69. self.onResize();
  70. var doc = 'contentDocument' in this? this.contentDocument : this.contentWindow.document;
  71. $(doc).find('body,html').css({'overflow-y':'hidden'});
  72. $(doc).scroll(self.onscroll);
  73. $(doc).mouseup(function() {self.rail.drag = false;});
  74. self.bind(doc,"mousewheel",self.onmousewheel);
  75. $(doc).keydown(self.onkeypress);
  76. });
  77. }
  78. */
  79. if (this.doc[0].nodeName == 'IFRAME') {
  80. this.doc.load(function() {
  81. var doc = 'contentDocument' in this? this.contentDocument : this.contentWindow.document;
  82. if (self.isiframe) self.docscroll = $(doc.body);
  83. self.onResize();
  84. $(doc.body).css({'overflow-y':'hidden'});
  85. $(doc).scroll(self.onscroll);
  86. $(doc).mouseup(function(){self.rail.drag = false;});
  87. self.bind(doc,"mousewheel",self.onmousewheel);
  88. $(doc).keydown(self.onkeypress);
  89. if (self.cantouch||self.opt.touchbehavior) {
  90. self.bind(doc,"mousedown",function(e) {
  91. self.rail.drag = {x:e.pageX,y:e.pageY,sx:self.scroll.x,sy:self.scroll.y,st:self.getScrollTop()};
  92. return self.cancelEvent(e);
  93. });
  94. self.bind(doc,"mouseup",function(e) {
  95. self.rail.drag = false;
  96. return self.cancelEvent(e);
  97. });
  98. self.bind(doc,"mousemove",function(e) {
  99. if (self.rail.drag) {
  100. self.doScrollTo(self.rail.drag.st - (e.pageY-self.rail.drag.y),true);
  101. return self.cancelEvent(e);
  102. }
  103. });
  104. }
  105. if (self.zoom) {
  106. self.bind(doc,'dblclick',self.doZoom);
  107. if (self.cantouch&&self.opt.gesturezoom) {
  108. self.bind(doc,"gesturechange",function(e) {
  109. if (e.scale>1.5) self.doZoomIn(e);
  110. if (e.scale<0.8) self.doZoomOut(e);
  111. return self.cancelEvent(e);
  112. });
  113. }
  114. }
  115. });
  116. }
  117. this.view = false;
  118. this.page = false;
  119. this.scroll = {x:0,y:0};
  120. this.scrollratio = {x:0,y:0};
  121. this.cursorheight = 20;
  122. this.scrollvaluemax = 0;
  123. do {
  124. this.id = "ascrail"+Math.round(Math.random() * 99999);
  125. } while (document.getElementById(this.id));
  126. this.rail = false;
  127. this.cursor = false;
  128. this.cursorfreezed = false;
  129. this.zoom = false;
  130. this.zoomactive = false;
  131. this.hasfocus = false;
  132. this.hasmousefocus = false;
  133. var domtest = document.createElement('DIV');
  134. this.isie = (document.all && !document.opera);
  135. this.isieold = (this.isie && !('msInterpolationMode' in domtest.style));
  136. this.cantouch = ('ontouchstart' in document.documentElement);
  137. if (self.opt.hwacceleration) { // if you dont need dont bother to look for
  138. this.trstyle = (window.opera) ? 'OTransform' : (document.all) ? 'msTransform' : (domtest.style.webkitTransform!==undefined) ? 'webkitTransform' : (domtest.style.MozTransform!==undefined) ? 'MozTransform' : false;
  139. if (this.trstyle && domtest.style[this.trstyle] === undefined) this.trstyle = false;
  140. this.hastransform = (this.trstyle != false);
  141. if (this.hastransform) {
  142. domtest.style[this.trstyle] = "translate3d(1px,2px,3px)";
  143. this.hastranslate3d = /translate3d/.test(domtest.style[this.trstyle]);
  144. }
  145. } else {
  146. this.trstyle = false;
  147. this.hastransform = false;
  148. }
  149. this.ishwscroll = (self.hastransform)&&(self.opt.hwacceleration)&&(self.haswrapper);
  150. this.scrollTop = function(val) {
  151. return (val === undefined) ? self.getScrollTop() : self.setScrollTop(val);
  152. };
  153. if (this.ishwscroll) {
  154. // hw accelerated scroll
  155. self.doc.translate = {x:0,y:0};
  156. this.getScrollTop = function() {
  157. return self.doc.translate.y;
  158. };
  159. if (this.hastranslate3d) {
  160. this.setScrollTop = function(val) {
  161. self.doc.css(self.trstyle,"translate3d(0px,"+(val*-1)+"px,0px)");
  162. self.doc.translate.y = val;
  163. if (document.createEvent) {
  164. var e = document.createEvent("UIEvents");
  165. e.initUIEvent("scroll", false, true, window, 1);
  166. self.docscroll[0].dispatchEvent(e);
  167. } else {
  168. var e = document.createEventObject();
  169. self.docscroll[0].fireEvent("onscroll");
  170. e.cancelBubble = true;
  171. }
  172. };
  173. } else {
  174. this.setScrollTop = function(val) {
  175. self.doc.css(self.trstyle,"translate(0px,"+(val*-1)+"px)");
  176. self.doc.translate.y = val;
  177. if (document.createEvent) {
  178. var e = document.createEvent("UIEvents");
  179. e.initUIEvent("scroll", false, true, window, 1);
  180. self.docscroll[0].dispatchEvent(e);
  181. } else {
  182. var e = document.createEventObject();
  183. self.docscroll[0].fireEvent("onscroll");
  184. e.cancelBubble = true;
  185. }
  186. };
  187. }
  188. } else {
  189. // native scroll
  190. this.getScrollTop = function() {
  191. return self.docscroll.scrollTop();
  192. };
  193. this.setScrollTop = function(val) {
  194. return self.docscroll.scrollTop(val);
  195. };
  196. }
  197. this.getTarget = function(e) {
  198. if (!e) return false;
  199. if (e.target) return e.target;
  200. if (e.srcElement) return e.srcElement;
  201. return false;
  202. };
  203. this.hasParent = function(e,id) {
  204. if (!e) return false;
  205. var el = e.target||e.srcElement||e||false;
  206. while (el && el.id != id) {
  207. el = el.parentNode||false;
  208. }
  209. return (el!==false);
  210. };
  211. this.updateScrollBar = function() {
  212. if (self.ishwscroll) {
  213. self.rail.css({height:self.win.innerHeight()});
  214. } else {
  215. var pos = self.win.offset();
  216. pos.top+=2;
  217. pos.left+=self.win.outerWidth()-self.rail.width-5;
  218. self.rail.css({position:"absolute",top:pos.top,left:pos.left,height:self.win.outerHeight()});
  219. if (self.zoom) self.zoom.css({position:"absolute",top:pos.top+1,left:pos.left-18});
  220. }
  221. };
  222. this.init = function() {
  223. if (!self.ispage || (!self.cantouch && !this.isieold)) {
  224. (self.ispage)?self.doc.css({'overflow-y':'hidden'}):self.docscroll.css({'overflow-y':'hidden'});
  225. var rail = $(document.createElement('div'));
  226. rail.attr('id',self.id);
  227. rail.width = 4+parseFloat(self.opt.cursorwidth);
  228. rail.css({"padding-left":"4px","padding-right":"1px",width:self.rail.width+"px",'z-index':self.opt.zindex,opacity:self.cursoropacitymin});
  229. self.rail = rail;
  230. var zoom = false;
  231. if (self.opt.boxzoom&&!self.ispage&&!self.isieold) {
  232. zoom = document.createElement('div');
  233. self.bind(zoom,"click",self.doZoom);
  234. self.zoom = $(zoom);
  235. self.zoom.css({"cursor":"pointer",'z-index':self.opt.zindex,'backgroundImage':'url('+scriptpath+'zoomico.png)','height':18,'width':18,'backgroundPosition':'0px 0px'});
  236. if (self.opt.dblclickzoom) self.bind(self.win,"dblclick",self.doZoom);
  237. if (self.cantouch&&self.opt.gesturezoom) {
  238. self.bind(self.win,"gesturechange",function(e) {
  239. if (e.scale>1.5) self.doZoomIn(e);
  240. if (e.scale<0.8) self.doZoomOut(e);
  241. return self.cancelEvent(e);
  242. });
  243. }
  244. }
  245. if (self.ispage) {
  246. rail.css({position:"fixed",top:"0px",right:"0px",height:"100%"});
  247. self.doc.append(rail);
  248. } else {
  249. if (self.ishwscroll) {
  250. if (self.win.css('position')=='static') self.win.css('position','relative');
  251. if (self.zoom) {
  252. self.zoom.css({position:"absolute",top:1,right:0,"margin-right":rail.width+2});
  253. self.win.append(self.zoom);
  254. }
  255. rail.css({position:"absolute",top:0,right:0});
  256. self.win.append(rail);
  257. } else {
  258. self.updateScrollBar();
  259. $("body").append(rail);
  260. if (self.zoom) $("body").append(self.zoom);
  261. }
  262. }
  263. var cursor = $(document.createElement('div'));
  264. cursor.css({
  265. position:"relative",top:0,left:0,width:self.opt.cursorwidth,height:"0px",
  266. 'background-color':self.opt.cursorcolor,
  267. border:self.opt.cursorborder,
  268. '-webkit-border-radius':self.opt.cursorborderradius,
  269. '-moz-border-radius':self.opt.cursorborderradius,
  270. 'border-radius':self.opt.cursorborderradius
  271. });
  272. self.cursor = cursor;
  273. self.rail.append(cursor);
  274. self.win.resize(self.onResize);
  275. self.doc.resize(self.onResize);
  276. if (!self.ispage&&self.opt.boxzoom) $(window).resize(self.resizeZoom);
  277. self.onResize();
  278. if (self.cantouch||self.opt.touchbehavior) {
  279. self.bind(self.win,"mousedown",function(e) {
  280. self.rail.drag = {x:e.pageX,y:e.pageY,sx:self.scroll.x,sy:self.scroll.y,st:self.getScrollTop()};
  281. return self.cancelEvent(e);
  282. });
  283. self.bind(self.win,"mouseup",function(e) {
  284. self.rail.drag = false;
  285. return self.cancelEvent(e);
  286. });
  287. self.bind(self.rail,"mousedown",function(e) {
  288. self.rail.drag = {x:e.pageX,y:e.pageY,sx:self.scroll.x,sy:self.scroll.y,st:self.getScrollTop()};
  289. return self.cancelEvent(e);
  290. });
  291. self.bind(self.rail,"mouseup",function(e) {
  292. self.rail.drag = false;
  293. return self.cancelEvent(e);
  294. });
  295. self.bind(document,"mousemove",function(e) {
  296. if (self.rail.drag) {
  297. self.doScrollTo(self.rail.drag.st - (e.pageY-self.rail.drag.y),true);
  298. return self.cancelEvent(e);
  299. }
  300. });
  301. } else {
  302. self.bind(self.rail,"mousedown",function(e) {
  303. self.rail.drag = {x:e.screenX,y:e.screenY,sx:self.scroll.x,sy:self.scroll.y};
  304. return self.cancelEvent(e);
  305. });
  306. self.bind(self.rail,"mouseup",function(e) {
  307. self.rail.drag = false;
  308. self.hideCursor();
  309. return self.cancelEvent(e);
  310. });
  311. self.bind(document,"mousemove",function(e) {
  312. if (self.rail.drag) {
  313. self.scroll.y = self.rail.drag.sy + (e.screenY-self.rail.drag.y);
  314. if (self.scroll.y<0) self.scroll.y=0;
  315. var my = self.scrollvaluemax;
  316. if (self.scroll.y>my) self.scroll.y=my;
  317. self.showCursor();
  318. self.cursorfreezed = true;
  319. self.doScroll(Math.round(self.scroll.y*self.scrollratio.y));
  320. return self.cancelEvent(e);
  321. }
  322. });
  323. }
  324. self.bind(document,"mouseup",function(e) {
  325. self.rail.drag = false;
  326. self.hideCursor();
  327. });
  328. if (!self.cantouch) {
  329. self.rail.mouseenter(function() {
  330. self.showCursor();
  331. self.rail.active = true;
  332. });
  333. self.rail.mouseleave(function() {
  334. self.rail.active = false;
  335. if (!self.rail.drag) self.hideCursor();
  336. });
  337. if (!self.isiframe) self.bind((self.isie&&self.ispage) ? document : self.docscroll,"mousewheel",self.onmousewheel);
  338. self.bind(self.rail,"mousewheel",self.onmousewheel);
  339. }
  340. if (!self.ispage&&!self.cantouch) {
  341. if (!self.win.attr("tabindex")) self.win.attr({"tabindex":(new Date()).getTime()});
  342. self.win.focus(function(e) {
  343. domfocus = (self.getTarget(e)).id||true;
  344. self.hasfocus = true;
  345. self.noticeCursor();
  346. });
  347. self.win.blur(function(e) {
  348. domfocus = false;
  349. self.hasfocus = false;
  350. });
  351. self.win.mouseenter(function(e) {
  352. mousefocus = (self.getTarget(e)).id||true;
  353. self.hasmousefocus = true;
  354. self.noticeCursor();
  355. });
  356. self.win.mouseleave(function() {
  357. mousefocus = false;
  358. self.hasmousefocus = false;
  359. });
  360. };
  361. //Thanks to http://www.quirksmode.org !!
  362. self.onkeypress = function(e) {
  363. e = (e) ? e : window.e;
  364. var tg = self.getTarget(e);
  365. if (tg&&/(INPUT|TEXTAREA|SELECT)/.test(tg.nodeName)) return;
  366. if (self.hasfocus||(self.hasmousefocus&&!domfocus)||(self.ispage&&!domfocus&&!mousefocus)) {
  367. var key = e.keyCode;
  368. var ret = true;
  369. switch (key) {
  370. case 38:
  371. case 63233: //safari
  372. self.doScrollBy(12);
  373. ret = false;
  374. break;
  375. case 40:
  376. case 63235: //safari
  377. self.doScrollBy(-12);
  378. ret = false;
  379. break;
  380. case 33:
  381. case 63276: // safari
  382. self.doScrollBy(self.view.h,true);
  383. ret = false;
  384. break;
  385. case 34:
  386. case 63277: // safari
  387. self.doScrollBy(-self.view.h,true);
  388. ret = false;
  389. break;
  390. case 36:
  391. case 63273: // safari
  392. self.doScrollTo(0,true);
  393. ret = false;
  394. break;
  395. case 35:
  396. case 63275: // safari
  397. self.doScrollTo(self.page.h,true);
  398. ret = false;
  399. break;
  400. case 27: // ESC
  401. if (self.zoomactive) {
  402. self.doZoom();
  403. ret = false;
  404. }
  405. break;
  406. }
  407. if (!ret) return self.cancelEvent(e);
  408. }
  409. };
  410. self.bind(document,"keydown",self.onkeypress);
  411. }
  412. };
  413. this.showCursor = function() {
  414. if (self.cursortimeout) {
  415. clearTimeout(self.cursortimeout);
  416. self.cursortimeout = 0;
  417. }
  418. if (!self.rail) return;
  419. self.rail.stop().css({opacity:self.opt.cursoropacitymax});
  420. self.cursor.css({height:self.cursorheight,top:self.scroll.y});
  421. if (self.zoom) self.zoom.stop().css({opacity:self.opt.cursoropacitymax});
  422. };
  423. this.hideCursor = function(tm) {
  424. if (self.cursortimeout) return;
  425. if (!self.rail) return;
  426. self.cursortimeout = setTimeout(function() {
  427. if (!self.rail.active) {
  428. $(self.rail).stop().animate({opacity:self.opt.cursoropacitymin});
  429. if (self.zoom) self.zoom.stop().animate({opacity:self.opt.cursoropacitymin});
  430. }
  431. self.cursortimeout = 0;
  432. },tm||800);
  433. };
  434. this.noticeCursor = function(tm) {
  435. self.showCursor();
  436. self.hideCursor(tm);
  437. };
  438. this.getContentSize = function() {
  439. var pg =
  440. (self.ispage) ?
  441. {
  442. w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),
  443. h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)
  444. }
  445. : (self.haswrapper) ?
  446. {
  447. w:self.doc.outerWidth()+parseInt(self.win.css('paddingLeft'))+parseInt(self.win.css('paddingRight')),
  448. h:self.doc.outerHeight()+parseInt(self.win.css('paddingTop'))+parseInt(self.win.css('paddingBottom'))
  449. }
  450. :
  451. {
  452. w:self.docscroll[0].scrollWidth,
  453. h:self.docscroll[0].scrollHeight
  454. };
  455. pg.w-=1;
  456. pg.h-=1;
  457. return pg;
  458. };
  459. this.onResize = function() {
  460. if (!self.ispage) self.updateScrollBar();
  461. self.view = {
  462. w:(self.ispage) ? self.win.width() : self.win.innerWidth(),
  463. h:(self.ispage) ? self.win.height() : self.win.innerHeight()
  464. };
  465. self.page = self.getContentSize();
  466. self.cursorheight = Math.min(self.view.h,Math.round(self.view.h * (self.view.h / self.page.h)));
  467. self.scrollvaluemax = self.view.h-self.cursorheight-2;
  468. self.scrollratio = {
  469. x:0,
  470. y:((self.page.h - self.view.h)/self.scrollvaluemax)
  471. };
  472. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  473. self.noticeCursor();
  474. };
  475. this.bind = function(dom,name,fn,bubble) { // fixing jquery bind & touch-oriented
  476. var el = (dom.length) ? dom[0] : dom;
  477. if (el.addEventListener) {
  478. if (self.cantouch && /mouseup|mousedown|mousemove/.test(name)) { // touch device support
  479. var tt = (name=='mousedown')?'touchstart':(name=='mouseup')?'touchend':'touchmove';
  480. el.addEventListener(tt,function(e){
  481. if(e.touches.length<2){var ev=(e.touches.length>0)?e.touches[0]:e;ev.original=e;fn.call(this,ev);}
  482. },bubble||false);
  483. }
  484. el.addEventListener(name,fn,bubble||false);
  485. if (name=='mousewheel') el.addEventListener("DOMMouseScroll",fn,bubble||false);
  486. }
  487. else if (el.attachEvent) {
  488. el.attachEvent("on"+name,function(e) {
  489. if (!fn.call(el,e)||!bubble) return self.cancelEvent(e);
  490. });
  491. }
  492. else {
  493. el["on"+name] = function(e) {
  494. var rt=fn.call(el,e);
  495. if (!rt||!bubble) return self.cancelEvent(e);
  496. };
  497. }
  498. };
  499. // Thanks to http://www.switchonthecode.com !!
  500. this.cancelEvent = function(e) {
  501. if (self.cantouch) {
  502. e = e ? e.original : false;
  503. } else {
  504. e = e ? e : window.event||false;
  505. }
  506. if (!e) return false;
  507. if(e.stopPropagation) e.stopPropagation();
  508. if(e.preventDefault) e.preventDefault();
  509. e.cancelBubble = true;
  510. e.cancel = true;
  511. e.returnValue = false;
  512. return false;
  513. };
  514. this.onmousewheel = function(e) {
  515. e = e ? e : window.event;
  516. if (self.rail.drag) return self.cancelEvent(e);
  517. var delta = 0;
  518. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  519. if (delta) {
  520. self.doScrollBy(delta*self.opt.mousescrollstep,true);
  521. }
  522. return self.cancelEvent(e);
  523. };
  524. this.stop = function() {
  525. if (self.timer) clearInterval(self.timer);
  526. self.timer = 0;
  527. self.cursorfreezed = false;
  528. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  529. self.noticeCursor();
  530. };
  531. this.doScroll = function(y) {
  532. self.newscrolly = y;
  533. if (self.timer) return;
  534. self.timer = setInterval(function() {
  535. var gp = self.newscrolly - self.getScrollTop();
  536. var df = (gp>0) ? Math.ceil(gp/4) : Math.floor(gp/4);
  537. var sc = self.getScrollTop()+df;
  538. self.setScrollTop(sc);
  539. if (sc == self.newscrolly) {
  540. clearInterval(self.timer);
  541. self.timer = 0;
  542. self.cursorfreezed = false;
  543. }
  544. },self.opt.scrollspeed);
  545. self.noticeCursor();
  546. };
  547. this.doScrollBy = function(stp,absolute) {
  548. if (absolute) stp = Math.round(stp * 1/self.scrollratio.y);
  549. var ny = self.scroll.y-stp;
  550. if (ny<0) ny=0;
  551. var my = self.scrollvaluemax;
  552. if (ny>my) ny=my;
  553. self.cursorfreezed = false;
  554. self.doScroll(Math.floor(ny*self.scrollratio.y));
  555. };
  556. this.doScrollTo = function(pos,absolute) {
  557. if (absolute) pos = Math.round(pos * 1/self.scrollratio.y);
  558. ny=pos;
  559. if (ny<0) ny=0;
  560. var my = self.scrollvaluemax;
  561. if (ny>my) ny=my;
  562. self.cursorfreezed = false;
  563. self.doScroll(Math.floor(ny*self.scrollratio.y));
  564. };
  565. self.onscroll = function(e) {
  566. var tm = (new Date()).getTime();
  567. if (!self.lastcontentcheck || self.lastcontentcheck<tm) {
  568. self.lastcontentcheck=tm+500;
  569. var pg = self.getContentSize();
  570. if (pg.h!=self.page.h) self.onResize();
  571. }
  572. if (self.rail.drag) return;
  573. if (!self.cursorfreezed) self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  574. self.noticeCursor();
  575. };
  576. self.docscroll.scroll(function(e) {
  577. self.onscroll(e);
  578. });
  579. this.doZoomIn = function(e) {
  580. if (self.zoomactive) return;
  581. self.zoomrestore = {
  582. style:{}
  583. };
  584. for(var a in self.win[0].style) {
  585. self.zoomrestore.style[a] = self.win[0].style[a];
  586. }
  587. self.zoomrestore.padding = {
  588. w:self.win.outerWidth()-self.win.width(),
  589. h:self.win.outerHeight()-self.win.height()
  590. };
  591. self.win.css({
  592. "position":"fixed",
  593. "top":0,
  594. "left":0,
  595. "z-index":self.opt.zindex+100
  596. });
  597. var bkg = self.win.css("backgroundColor");
  598. if (bkg==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor","#fff");
  599. self.rail.css({"z-index":self.opt.zindex+110});
  600. self.zoom.css({"z-index":self.opt.zindex+112});
  601. self.zoomactive = true;
  602. self.zoom.css('backgroundPosition','0px -18px');
  603. self.resizeZoom();
  604. return self.cancelEvent(e);
  605. };
  606. this.doZoomOut = function(e) {
  607. if (!self.zoomactive) return;
  608. var res = self.zoomrestore.style;
  609. self.win.css({
  610. "position":res.position||'',
  611. "top":res.top||'',
  612. "left":res.left||'',
  613. "width":res.width||'',
  614. "height":res.height||'',
  615. "z-index":res.zIndex||'',
  616. "backgroundColor":res.backgroundColor||''
  617. });
  618. self.rail.css({"z-index":self.opt.zindex});
  619. self.zoom.css({"z-index":self.opt.zindex});
  620. self.zoomactive = false;
  621. self.zoomrestore = false;
  622. self.zoom.css('backgroundPosition','0px 0px');
  623. self.win.resize();
  624. return self.cancelEvent(e);
  625. };
  626. this.doZoom = function(e) {
  627. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  628. };
  629. this.resizeZoom = function() {
  630. if (!self.zoomactive) return;
  631. var py = self.getScrollTop(); //preserve scrolling position
  632. self.win.css({
  633. width:$(window).width()-self.zoomrestore.padding.w+"px",
  634. height:$(window).height()-self.zoomrestore.padding.h+"px"
  635. });
  636. self.setScrollTop(py);
  637. self.win.resize();
  638. };
  639. this.init();
  640. };
  641. $.fn.niceScroll = function(wrapper,opt) {
  642. if ((typeof wrapper=="object") && (typeof opt=="undefined")) {
  643. opt = wrapper;
  644. wrapper = false;
  645. }
  646. var ret = [];
  647. if (typeof opt=="undefined") opt = {};
  648. if (wrapper||false) {
  649. opt.doc = $(wrapper);
  650. opt.win = $(this);
  651. }
  652. var docundef = !("doc" in opt);
  653. this.each(function() {
  654. opt.doc = (docundef) ? $(this) : opt.doc;
  655. ret.push(new NiceScrollClass(opt));
  656. });
  657. return (ret.length==1) ? ret[0] : ret;
  658. };
  659. })( jQuery );