jquery.nicescroll.js 24 KB

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