jquery.nicescroll.js 44 KB

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