jquery.nicescroll.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /* jquery.nicescroll
  2. -- version 2.3.0
  3. -- copyright 2011 InuYaksa*2011
  4. -- licensed under the MIT
  5. --
  6. -- http://areaaperta.com/nicescroll
  7. -- https://github.com/inuyaksa/jquery.nicescroll
  8. --
  9. */
  10. (function($){
  11. // globals
  12. var domfocus = false;
  13. var mousefocus = false;
  14. var zoomactive = false;
  15. // http://stackoverflow.com/questions/2161159/get-script-path
  16. function getScriptPath() {
  17. var scripts= document.getElementsByTagName('script');
  18. var path= scripts[scripts.length-1].src.split('?')[0];
  19. return (path.split('/').length>0) ? path.split('/').slice(0,-1).join('/')+'/' : '';
  20. }
  21. var scriptpath = getScriptPath();
  22. var NiceScrollClass = function(myopt) {
  23. var self = this;
  24. this.version = '2.3.0';
  25. this.name = 'nicescroll';
  26. this.opt = {
  27. doc:$("body"),
  28. win:false,
  29. zindex:9000,
  30. cursoropacitymin:0,
  31. cursoropacitymax:1,
  32. cursorcolor:"#424242",
  33. cursorwidth:"5px",
  34. cursorborder:"1px solid #fff",
  35. cursorborderradius:"5px",
  36. scrollspeed:60,
  37. mousescrollstep:8*6,
  38. touchbehavior:false,
  39. hwacceleration:true,
  40. usetransition:true,
  41. boxzoom:false,
  42. dblclickzoom:true,
  43. gesturezoom:true,
  44. grabcursorenabled:true,
  45. autohidemode:true,
  46. background:"",
  47. iframeautoresize:true
  48. };
  49. if (myopt||false) {
  50. for(var a in self.opt) {
  51. if (myopt[a]!==undefined) self.opt[a] = myopt[a];
  52. }
  53. }
  54. this.id = self.opt.doc[0].id||'';
  55. this.doc = self.opt.doc;
  56. this.ispage = (self.doc[0].nodeName=='BODY'||self.doc[0].nodeName=='HTML');
  57. this.haswrapper = (self.opt.win!==false);
  58. this.win = self.opt.win||(this.ispage?$(window):this.doc);
  59. this.docscroll = this.ispage?$(window):this.win;
  60. this.iframe = false;
  61. this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName == 'IFRAME'));
  62. this.istextarea = (this.win[0].nodeName == 'TEXTAREA');
  63. // Events jump table
  64. this.onmousedown = false;
  65. this.onmouseup = false;
  66. this.onmousemove = false;
  67. this.onmousewheel = false;
  68. this.onkeypress = false;
  69. // Let's start!
  70. this.view = false;
  71. this.page = false;
  72. this.scroll = {x:0,y:0};
  73. this.scrollratio = {x:0,y:0};
  74. this.cursorheight = 20;
  75. this.scrollvaluemax = 0;
  76. do {
  77. this.id = "ascrail"+Math.round(Math.random() * 99999);
  78. } while (document.getElementById(this.id));
  79. this.rail = false;
  80. this.cursor = false;
  81. this.cursorfreezed = false;
  82. this.zoom = false;
  83. this.zoomactive = false;
  84. this.hasfocus = false;
  85. this.hasmousefocus = false;
  86. this.visibility = true;
  87. this.locked = false;
  88. var domtest = document.createElement('DIV');
  89. this.isopera = ("opera" in window);
  90. this.isie = (("all" in document) && ("attachEvent" in domtest) && !this.isopera);
  91. this.isieold = (this.isie && !("msInterpolationMode" in domtest.style)); // IE6 and older
  92. this.isie7 = this.isie&&!this.isieold&&(!("documentMode" in document)||(document.documentMode==7));
  93. this.isie8 = this.isie&&("documentMode" in document)&&(document.documentMode==8);
  94. this.isie9 = this.isie&&("performance" in window)&&(document.documentMode>=9);
  95. this.ismozilla = ("MozAppearance" in domtest.style);
  96. this.cantouch = ("ontouchstart" in document.documentElement);
  97. if (this.cantouch && /iphone|ipad|ipod/i.test(navigator.platform)) {
  98. this.isios = true;
  99. this.isios4 = !("seal" in Object);
  100. } else {
  101. this.isios = false;
  102. this.isios4 = false;
  103. }
  104. if (self.opt.hwacceleration) { // if you dont need dont bother to look for
  105. this.trstyle = (window.opera) ? 'OTransform' : (document.all) ? 'msTransform' : (domtest.style.webkitTransform!==undefined) ? 'webkitTransform' : (domtest.style.MozTransform!==undefined) ? 'MozTransform' : false;
  106. if (this.trstyle && domtest.style[this.trstyle] === undefined) this.trstyle = false;
  107. this.hastransform = (this.trstyle != false);
  108. if (this.hastransform) {
  109. domtest.style[this.trstyle] = "translate3d(1px,2px,3px)";
  110. this.hastranslate3d = /translate3d/.test(domtest.style[this.trstyle]);
  111. }
  112. this.transitionstyle = false;
  113. this.prefixstyle = '';
  114. this.transitionend = false;
  115. var check = ['transition','webkitTransition','MozTransition','OTransition','msTransition','KhtmlTransition'];
  116. var prefix = ['','-webkit-','-moz-','-o-','-ms-','-khtml-'];
  117. var evs = ['transitionEnd','webkitTransitionEnd','transitionend','oTransitionEnd','msTransitionEnd','KhtmlTransitionEnd'];
  118. for(var a=0;a<check.length;a++) {
  119. if (check[a] in domtest.style) {
  120. this.transitionstyle = check[a];
  121. this.prefixstyle = prefix[a];
  122. this.transitionend = evs[a];
  123. break;
  124. }
  125. }
  126. this.hastransition = (this.transitionstyle);
  127. } else {
  128. this.trstyle = false;
  129. this.hastransform = false;
  130. this.hastranslate3d = false;
  131. this.transitionstyle = false;
  132. this.hastransition = false;
  133. this.transitionend = false;
  134. }
  135. this.cursorgrabvalue = '';
  136. if (self.opt.grabcursorenabled&&self.opt.touchbehavior) { // check grab cursor support
  137. function detectCursorGrab() {
  138. if (!self.isie||self.isie9) { // some old IE return false positive
  139. var lst = ['grab','-moz-grab','-webkit-grab'];
  140. for(var a=0;a<lst.length;a++) {
  141. var p = lst[a];
  142. domtest.style['cursor']=p;
  143. if (domtest.style['cursor']==p) return p;
  144. }
  145. }
  146. return 'url(http://www.google.com/intl/en_ALL/mapfiles/openhand.cur),n-resize';
  147. }
  148. this.cursorgrabvalue = detectCursorGrab();
  149. }
  150. domtest = null;
  151. this.ishwscroll = (self.hastransform)&&(self.opt.hwacceleration)&&(self.haswrapper);
  152. this.scrollTop = function(val) {
  153. return (val === undefined) ? self.getScrollTop() : self.setScrollTop(val);
  154. };
  155. if (this.ishwscroll) {
  156. // hw accelerated scroll
  157. self.doc.translate = {x:0,y:0};
  158. this.getScrollTop = function() {
  159. return self.doc.translate.y;
  160. };
  161. if (this.hastranslate3d) {
  162. this.setScrollTop = function(val,silent) {
  163. self.doc.css(self.trstyle,"translate3d(0px,"+(val*-1)+"px,0px)");
  164. self.doc.translate.y = val;
  165. if (!silent) {
  166. if (document.createEvent) {
  167. var e = document.createEvent("UIEvents");
  168. e.initUIEvent("scroll", false, true, window, 1);
  169. self.docscroll[0].dispatchEvent(e);
  170. } else {
  171. var e = document.createEventObject();
  172. self.docscroll[0].fireEvent("onscroll");
  173. e.cancelBubble = true;
  174. }
  175. }
  176. };
  177. } else {
  178. this.setScrollTop = function(val,silent) {
  179. self.doc.css(self.trstyle,"translate(0px,"+(val*-1)+"px)");
  180. self.doc.translate.y = val;
  181. if (!silent) {
  182. if (document.createEvent) {
  183. var e = document.createEvent("UIEvents");
  184. e.initUIEvent("scroll", false, true, window, 1);
  185. self.docscroll[0].dispatchEvent(e);
  186. } else {
  187. var e = document.createEventObject();
  188. self.docscroll[0].fireEvent("onscroll");
  189. e.cancelBubble = true;
  190. }
  191. }
  192. };
  193. }
  194. } else {
  195. // native scroll
  196. this.getScrollTop = function() {
  197. return self.docscroll.scrollTop();
  198. };
  199. this.setScrollTop = function(val) {
  200. return self.docscroll.scrollTop(val);
  201. };
  202. }
  203. this.getTarget = function(e) {
  204. if (!e) return false;
  205. if (e.target) return e.target;
  206. if (e.srcElement) return e.srcElement;
  207. return false;
  208. };
  209. this.hasParent = function(e,id) {
  210. if (!e) return false;
  211. var el = e.target||e.srcElement||e||false;
  212. while (el && el.id != id) {
  213. el = el.parentNode||false;
  214. }
  215. return (el!==false);
  216. };
  217. this.updateScrollBar = function(len) {
  218. if (self.ishwscroll) {
  219. self.rail.css({height:self.win.innerHeight()});
  220. } else {
  221. var pos = self.win.offset();
  222. pos.top+=2;
  223. var brd = (self.win.outerWidth() - self.win.innerWidth())/2;
  224. pos.left+= self.win.innerWidth() + brd - self.rail.width - 1;
  225. self.rail.css({position:"absolute",top:pos.top,left:pos.left,height:(len)?len.h:self.win.innerHeight()});
  226. if (self.zoom) self.zoom.css({position:"absolute",top:pos.top+1,left:pos.left-18});
  227. }
  228. };
  229. // derived by http://blog.joelambert.co.uk/2011/06/01/a-better-settimeoutsetinterval/
  230. var setAnimationFrame = (function(){
  231. return window.requestAnimationFrame ||
  232. window.webkitRequestAnimationFrame ||
  233. window.mozRequestAnimationFrame ||
  234. window.oRequestAnimationFrame ||
  235. window.msRequestAnimationFrame ||
  236. false;
  237. })();
  238. var clearAnimationFrame = (function(){
  239. return window.cancelRequestAnimationFrame ||
  240. window.webkitCancelRequestAnimationFrame ||
  241. window.mozCancelRequestAnimationFrame ||
  242. window.oCancelRequestAnimationFrame ||
  243. window.msCancelRequestAnimationFrame ||
  244. false;
  245. })();
  246. self.hasanimationframe = (setAnimationFrame);
  247. self.hascancelanimationframe = (clearAnimationFrame);
  248. if (!self.hasanimationframe) setAnimationFrame=function(fn){return setTimeout(fn,1000/60)};
  249. if (!self.hascancelanimationframe) clearAnimationFrame=clearInterval;
  250. this.init = function() {
  251. if (!self.ispage || (!self.cantouch && !self.isieold)) {
  252. (self.ispage)?self.doc.css({'overflow-y':'hidden'}):self.docscroll.css({'overflow-y':'hidden'});
  253. if (self.ispage&&self.isie7) $("html").css({'overflow-y':'hidden'}); //IE7 double scrollbar issue
  254. var cursor = $(document.createElement('div'));
  255. cursor.css({
  256. position:"relative",top:0,"float":"right",width:self.opt.cursorwidth,height:"0px",
  257. 'background-color':self.opt.cursorcolor,
  258. border:self.opt.cursorborder,
  259. 'background-clip':'padding-box',
  260. '-webkit-border-radius':self.opt.cursorborderradius,
  261. '-moz-border-radius':self.opt.cursorborderradius,
  262. 'border-radius':self.opt.cursorborderradius
  263. });
  264. cursor.hborder = parseFloat(cursor.outerHeight() - cursor.innerHeight());
  265. self.cursor = cursor;
  266. var rail = $(document.createElement('div'));
  267. rail.attr('id',self.id);
  268. rail.width = 3+Math.max(parseFloat(self.opt.cursorwidth),cursor.outerWidth());
  269. 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});
  270. rail.append(cursor);
  271. self.rail = rail;
  272. var zoom = false;
  273. if (self.opt.boxzoom&&!self.ispage&&!self.isieold) {
  274. zoom = document.createElement('div');
  275. self.bind(zoom,"click",self.doZoom);
  276. self.zoom = $(zoom);
  277. self.zoom.css({"cursor":"pointer",'z-index':self.opt.zindex,'backgroundImage':'url('+scriptpath+'zoomico.png)','height':18,'width':18,'backgroundPosition':'0px 0px'});
  278. if (self.opt.dblclickzoom) self.bind(self.win,"dblclick",self.doZoom);
  279. if (self.cantouch&&self.opt.gesturezoom) {
  280. self.bind(self.win,"gestureend",function(e) {
  281. if (e.scale>1.5) self.doZoomIn(e);
  282. if (e.scale<0.8) self.doZoomOut(e);
  283. return self.cancelEvent(e);
  284. });
  285. }
  286. }
  287. if (self.ispage) {
  288. rail.css({position:"fixed",top:"0px",right:"0px",height:"100%"});
  289. self.doc.append(rail);
  290. } else {
  291. if (self.ishwscroll) {
  292. if (self.win.css('position')=='static') self.win.css('position','relative');
  293. if (self.zoom) {
  294. self.zoom.css({position:"absolute",top:1,right:0,"margin-right":rail.width+2});
  295. self.win.append(self.zoom);
  296. }
  297. rail.css({position:"absolute",top:0,right:0});
  298. self.win.append(rail);
  299. } else {
  300. self.updateScrollBar();
  301. $("body").append(rail);
  302. if (self.zoom) $("body").append(self.zoom);
  303. }
  304. }
  305. if (self.opt.autohidemode===false) {
  306. self.autohidedom = false;
  307. }
  308. else if (self.opt.autohidemode===true) {
  309. self.autohidedom = self.rail;
  310. }
  311. else if (self.opt.autohidemode=="cursor") {
  312. self.autohidedom = self.cursor;
  313. }
  314. self.bind(window,'resize',self.onResize);
  315. self.bind(window,'orientationchange',self.onResize);
  316. if (!self.ispage&&!self.haswrapper) {
  317. self.bind(self.win,(self.isie&&!self.isie9)?"propertychange":"DOMAttrModified",self.onAttributeChange);
  318. }
  319. if (!self.ispage&&self.opt.boxzoom) $(window).resize(self.resizeZoom);
  320. if (self.istextarea) self.win.mouseup(self.onResize);
  321. self.onResize();
  322. if (self.cantouch||self.opt.touchbehavior) {
  323. self.onmousedown = function(e) {
  324. if (self.locked) return self.cancelEvent(e);
  325. self.rail.drag = {x:e.pageX,y:e.pageY,sx:self.scroll.x,sy:self.scroll.y,st:self.getScrollTop(),lx:e.pageX,ly:e.pageY,nx:e.pageX,ny:e.pageY,tt:(new Date()).getTime(),lt:(new Date()).getTime()};
  326. return self.cancelEvent(e);
  327. };
  328. self.onmouseup = function(e) {
  329. if (self.rail.drag) {
  330. self.doScrollMomentum(self.rail.drag);
  331. self.rail.drag = false;
  332. return self.cancelEvent(e);
  333. }
  334. };
  335. self.onmousemove = function(e) {
  336. if (self.rail.drag) {
  337. var my = (e.pageY-self.rail.drag.y);
  338. self.rail.drag.ly = self.rail.drag.ny;
  339. self.rail.drag.ny = e.pageY;
  340. self.rail.drag.lt = (new Date()).getTime();
  341. self.scroll.y = Math.round((self.rail.drag.st-my) * (1/self.scrollratio.y));
  342. if (self.scroll.y<0) self.scroll.y=0;
  343. if (self.scroll.y>self.scrollvaluemax) self.scroll.y=self.scrollvaluemax;
  344. self.showCursor();
  345. self.doScrollTo(self.rail.drag.st-my,true);
  346. return self.cancelEvent(e);
  347. }
  348. };
  349. if (self.cursorgrabvalue) self.win.css({'cursor':self.cursorgrabvalue});
  350. } else {
  351. self.onmousedown = function(e) {
  352. if (self.locked) return self.cancelEvent(e);
  353. self.rail.drag = {x:e.screenX,y:e.screenY,sx:self.scroll.x,sy:self.scroll.y};
  354. return self.cancelEvent(e);
  355. };
  356. self.onmouseup = function(e) {
  357. if (self.rail.drag) {
  358. self.rail.drag = false;
  359. return self.cancelEvent(e);
  360. }
  361. };
  362. self.onmousemove = function(e) {
  363. if (self.rail.drag) {
  364. self.scroll.y = self.rail.drag.sy + (e.screenY-self.rail.drag.y);
  365. if (self.scroll.y<0) self.scroll.y=0;
  366. var my = self.scrollvaluemax;
  367. if (self.scroll.y>my) self.scroll.y=my;
  368. self.showCursor();
  369. self.cursorfreezed = true;
  370. self.doScroll(Math.round(self.scroll.y*self.scrollratio.y));
  371. return self.cancelEvent(e);
  372. }
  373. };
  374. }
  375. if (self.cantouch||self.opt.touchbehavior) self.bind(self.win,"mousedown",self.onmousedown);
  376. self.bind(self.win,"mouseup",self.onmouseup);
  377. self.bind(self.rail,"mousedown",self.onmousedown);
  378. self.bind(self.rail,"mouseup",function(e) {
  379. self.rail.drag = false;
  380. self.hideCursor();
  381. return self.cancelEvent(e);
  382. });
  383. self.bind(document,"mouseup",self.onmouseup);
  384. self.bind(document,"mousemove",self.onmousemove);
  385. if (!self.cantouch) {
  386. self.rail.mouseenter(function() {
  387. self.showCursor();
  388. self.rail.active = true;
  389. });
  390. self.rail.mouseleave(function() {
  391. self.rail.active = false;
  392. if (!self.rail.drag) self.hideCursor();
  393. });
  394. if (!self.isiframe) self.bind((self.isie&&self.ispage) ? document : self.docscroll,"mousewheel",self.onmousewheel);
  395. self.bind(self.rail,"mousewheel",self.onmousewheel);
  396. }
  397. if (!self.ispage&&!self.cantouch) {
  398. if (!self.win.attr("tabindex")) self.win.attr({"tabindex":(new Date()).getTime()});
  399. self.win.focus(function(e) {
  400. domfocus = (self.getTarget(e)).id||true;
  401. self.hasfocus = true;
  402. self.noticeCursor();
  403. });
  404. self.win.blur(function(e) {
  405. domfocus = false;
  406. self.hasfocus = false;
  407. });
  408. self.win.mouseenter(function(e) {
  409. mousefocus = (self.getTarget(e)).id||true;
  410. self.hasmousefocus = true;
  411. self.noticeCursor();
  412. });
  413. self.win.mouseleave(function() {
  414. mousefocus = false;
  415. self.hasmousefocus = false;
  416. });
  417. };
  418. //Thanks to http://www.quirksmode.org !!
  419. self.onkeypress = function(e) {
  420. e = (e) ? e : window.e;
  421. var tg = self.getTarget(e);
  422. if (tg&&/INPUT|TEXTAREA|SELECT/.test(tg.nodeName)) {
  423. var tp = tg.getAttribute('type')||tg.type||false;
  424. if ((!tp)||!(/submit|button|cancel/i.tp)) return true;
  425. }
  426. if (self.hasfocus||(self.hasmousefocus&&!domfocus)||(self.ispage&&!domfocus&&!mousefocus)) {
  427. var key = e.keyCode;
  428. if (self.locked&&key!=27) return self.cancelEvent(e);
  429. var ret = true;
  430. switch (key) {
  431. case 38:
  432. case 63233: //safari
  433. self.doScrollBy(24*3,true);
  434. ret = false;
  435. break;
  436. case 40:
  437. case 63235: //safari
  438. self.doScrollBy(-24*3,true);
  439. ret = false;
  440. break;
  441. case 33:
  442. case 63276: // safari
  443. self.doScrollBy(self.view.h,true);
  444. ret = false;
  445. break;
  446. case 34:
  447. case 63277: // safari
  448. self.doScrollBy(-self.view.h,true);
  449. ret = false;
  450. break;
  451. case 36:
  452. case 63273: // safari
  453. self.doScrollTo(0,true);
  454. ret = false;
  455. break;
  456. case 35:
  457. case 63275: // safari
  458. self.doScrollTo(self.page.h,true);
  459. ret = false;
  460. break;
  461. case 27: // ESC
  462. if (self.zoomactive) {
  463. self.doZoom();
  464. ret = false;
  465. }
  466. break;
  467. }
  468. if (!ret) return self.cancelEvent(e);
  469. }
  470. };
  471. self.bind(document,(self.isopera)?"keypress":"keydown",self.onkeypress);
  472. }
  473. if (this.doc[0].nodeName == 'IFRAME') {
  474. function oniframeload(e) {
  475. var doc = 'contentDocument' in this ? this.contentDocument : this.contentWindow.document;
  476. if (self.isiframe) {
  477. self.docscroll = $(doc);
  478. self.iframe = {
  479. html:self.doc.contents().find('html')[0],
  480. body:self.doc.contents().find('body')[0]
  481. };
  482. }
  483. if (self.opt.iframeautoresize&&!self.isiframe) {
  484. self.win.scrollTop(0); // reset position
  485. self.doc.height(""); //reset height to fix browser bug
  486. var cc=self.doc.contents();
  487. var hh=Math.max(cc.find('html').attr('scrollHeight'),cc.find('body').attr('scrollHeight'));
  488. self.doc.height(hh);
  489. }
  490. self.onResize();
  491. if (self.isie7) $(doc).find('html').css({'overflow-y':'hidden'});
  492. $(doc.body).css({'overflow-y':'hidden'});
  493. if ('contentWindow' in this) {
  494. self.bind(this.contentWindow,"scroll",self.onscroll); //IE8 & minor
  495. } else {
  496. self.bind(doc,"scroll",self.onscroll);
  497. }
  498. self.bind(doc,"mouseup",self.onmouseup);
  499. self.bind(doc,"mousewheel",self.onmousewheel);
  500. self.bind(doc,(self.isopera)?"keypress":"keydown",self.onkeypress);
  501. if (self.cantouch||self.opt.touchbehavior) {
  502. self.bind(doc,"mousedown",self.onmousedown);
  503. if (self.cursorgrabvalue) $(doc).css({'cursor':self.cursorgrabvalue});
  504. }
  505. self.bind(doc,"mousemove",self.onmousemove);
  506. if (self.zoom) {
  507. if (self.opt.dblclickzoom) self.bind(doc,'dblclick',self.doZoom);
  508. if (self.cantouch&&self.opt.gesturezoom) {
  509. self.bind(doc,"gesturechange",function(e) {
  510. if (e.scale>1.5) self.doZoomIn(e);
  511. if (e.scale<0.8) self.doZoomOut(e);
  512. return self.cancelEvent(e);
  513. });
  514. }
  515. }
  516. };
  517. if (this.doc[0].readyState&&this.doc[0].readyState=="complete"){
  518. setTimeout(function(){oniframeload.call(self.doc[0],false)},500);
  519. }
  520. this.doc.load(oniframeload);
  521. }
  522. };
  523. this.showCursor = function() {
  524. if (self.cursortimeout) {
  525. clearTimeout(self.cursortimeout);
  526. self.cursortimeout = 0;
  527. }
  528. if (!self.rail) return;
  529. if (self.autohidedom) self.autohidedom.stop().css({opacity:self.opt.cursoropacitymax});
  530. self.cursor.css({height:self.cursorheight,top:self.scroll.y});
  531. if (self.zoom) self.zoom.stop().css({opacity:self.opt.cursoropacitymax});
  532. };
  533. this.hideCursor = function(tm) {
  534. if (self.cursortimeout) return;
  535. if (!self.rail) return;
  536. if (!self.autohidedom) return;
  537. self.cursortimeout = setTimeout(function() {
  538. if (!self.rail.active) {
  539. self.autohidedom.stop().animate({opacity:self.opt.cursoropacitymin});
  540. if (self.zoom) self.zoom.stop().animate({opacity:self.opt.cursoropacitymin});
  541. }
  542. self.cursortimeout = 0;
  543. },tm||800);
  544. };
  545. this.noticeCursor = function(tm) {
  546. self.showCursor();
  547. self.hideCursor(tm);
  548. };
  549. this.getContentSize = function() {
  550. var pg =
  551. (self.ispage) ?
  552. {
  553. w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),
  554. h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)
  555. }
  556. : (self.haswrapper) ?
  557. {
  558. w:self.doc.outerWidth()+parseInt(self.win.css('paddingLeft'))+parseInt(self.win.css('paddingRight')),
  559. h:self.doc.outerHeight()+parseInt(self.win.css('paddingTop'))+parseInt(self.win.css('paddingBottom'))
  560. }
  561. : (self.iframe) ?
  562. {
  563. w:Math.max(self.iframe.html.scrollWidth,self.iframe.body.scrollWidth),
  564. h:Math.max(self.iframe.html.scrollHeight,self.iframe.body.scrollHeight)
  565. }
  566. :
  567. {
  568. w:self.docscroll[0].scrollWidth,
  569. h:self.docscroll[0].scrollHeight
  570. };
  571. pg.w-=1;
  572. pg.h-=1;
  573. return pg;
  574. };
  575. this.onAttributeChange = function(e) {
  576. if (e&&e.target) {
  577. if ((e.target !== self.win[0])&&(e.target!==self.doc[0])) return true;
  578. var attr = ("attrChange" in e)?e.attrName:("propertyName" in e)?e.propertyName:false;
  579. if (attr&&/width|height|style|block/i.test(attr)) {
  580. if (self.lastattributechange) self.lastattributechange=clearTimeout(self.lastattributechange);
  581. self.lastattributechange = setTimeout(function(){
  582. self.lastattributechange = 0;
  583. self.onResize();
  584. },60);
  585. }
  586. }
  587. };
  588. this.onResize = function() {
  589. if (!self.haswrapper&&!self.ispage) {
  590. var vis = (self.win.css('display')!='none');
  591. if (vis&&!self.visibility) self.show();
  592. if (!vis&&self.visibility) self.hide();
  593. if (!self.visibility) return false;
  594. }
  595. self.view = {
  596. w:(self.ispage) ? self.win.width() : self.win.innerWidth(),
  597. h:(self.ispage) ? self.win.height() : self.win.innerHeight()
  598. };
  599. self.page = self.getContentSize();
  600. if (self.view.h>=self.page.h) {
  601. self.hide();
  602. self.scrollvaluemax = 0;
  603. self.scroll.y = 0;
  604. self.scrollratio = {x:0,y:0};
  605. self.cursorheight = 0;
  606. self.locked = true;
  607. self.setScrollTop(0);
  608. return false;
  609. }
  610. else if (!self.visibility) self.show();
  611. self.locked = false;
  612. if (self.istextarea&&self.win.css('resize')&&self.win.css('resize')!='none') self.view.h-=20;
  613. if (!self.ispage) self.updateScrollBar(self.view);
  614. self.cursorheight = Math.min(self.view.h,Math.round(self.view.h * (self.view.h / self.page.h)));
  615. self.scrollvaluemax = self.view.h-self.cursorheight-self.cursor.hborder-2;
  616. self.scrollratio = {
  617. x:0,
  618. y:((self.page.h - self.view.h)/self.scrollvaluemax)
  619. };
  620. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  621. self.noticeCursor();
  622. };
  623. this.bind = function(dom,name,fn,bubble) { // touch-oriented & fixing jquery bind
  624. var el = (dom.length) ? dom[0] : dom;
  625. if (el.addEventListener) {
  626. if (self.cantouch && /mouseup|mousedown|mousemove/.test(name)) { // touch device support
  627. var tt = (name=='mousedown')?'touchstart':(name=='mouseup')?'touchend':'touchmove';
  628. el.addEventListener(tt,function(e){
  629. if(e.touches.length<2){var ev=(e.touches.length>0)?e.touches[0]:e;ev.original=e;fn.call(this,ev);}
  630. },bubble||false);
  631. }
  632. el.addEventListener(name,fn,bubble||false);
  633. if (name=='mousewheel') el.addEventListener("DOMMouseScroll",fn,bubble||false);
  634. if (self.cantouch && name=="mouseup") el.addEventListener("touchcancel",fn,bubble||false);
  635. }
  636. else if (el.attachEvent) {
  637. el.attachEvent("on"+name,function(e) {
  638. if (e&&!("pageY" in e)&&("screenY" in e)) {
  639. e.pageX = e.screenX;
  640. e.pageY = e.screenY;
  641. }
  642. return ((fn.call(el,e)===false)||bubble===false) ? self.cancelEvent(e) : true;
  643. });
  644. }
  645. else {
  646. el["on"+name] = function(e) {
  647. var rt=fn.call(el,e);
  648. return (rt===false||bubble===false) ? self.cancelEvent(e) : true;
  649. };
  650. }
  651. };
  652. // Thanks to http://www.switchonthecode.com !!
  653. this.cancelEvent = function(e) {
  654. if (self.cantouch) {
  655. e = e.original ? e.original : e||false;
  656. } else {
  657. e = e ? e : window.event||false;
  658. }
  659. if (!e) return false;
  660. if(e.stopPropagation) e.stopPropagation();
  661. if(e.preventDefault) e.preventDefault();
  662. e.cancelBubble = true;
  663. e.cancel = true;
  664. e.returnValue = false;
  665. return false;
  666. };
  667. this.show = function() {
  668. self.visibility = true;
  669. self.rail.css('display','block');
  670. };
  671. this.hide = function() {
  672. self.visibility = false;
  673. self.rail.css('display','none');
  674. };
  675. this.onmousewheel = function(e) {
  676. if (self.locked) return self.cancelEvent(e);
  677. e = e ? e : window.event;
  678. if (self.rail.drag) return self.cancelEvent(e);
  679. var delta = 0;
  680. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  681. if (delta) {
  682. self.doScrollBy(delta*self.opt.mousescrollstep,true);
  683. }
  684. return self.cancelEvent(e);
  685. };
  686. this.stop = function() {
  687. if (self.timer) clearAnimationFrame(self.timer);
  688. self.timer = 0;
  689. self.cursorfreezed = false;
  690. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  691. self.noticeCursor();
  692. };
  693. if (self.ishwscroll&&self.hastransition&&self.opt.usetransition) {
  694. this.prepareTransition = function(dif,trend) {
  695. var sp = Math.round(self.opt.scrollspeed*10);
  696. var ex = Math.min(sp,Math.round((dif / 20) * self.opt.scrollspeed));
  697. var trans = (ex>20) ? self.prefixstyle+'transform '+ex+'ms ease-out 0s' : '';
  698. if (!self.lasttransitionstyle||self.lasttransitionstyle!=trans) {
  699. self.lasttransitionstyle = trans;
  700. self.doc.css(self.transitionstyle,trans);
  701. }
  702. };
  703. this.doScroll = function(y) {
  704. self.newscrolly = y;
  705. if (self.timer) return;
  706. self.timer = setTimeout(function() {
  707. var top = self.getScrollTop();
  708. var dif = (top>self.newscrolly) ? top-self.newscrolly : self.newscrolly-top;
  709. self.prepareTransition(dif)
  710. self.setScrollTop(self.newscrolly);
  711. self.timer = 0;
  712. },self.opt.scrollspeed);
  713. self.noticeCursor();
  714. };
  715. // self.bind(self.doc,'transitionend',function(e){console.log(e)},false); TEST!! Later or soon I use it! (I hope so)
  716. } else {
  717. this.doScroll = function(y) {
  718. self.newscrolly = y;
  719. if (self.timer) return;
  720. var rt = 1/4; //*(60/self.opt.scrollspeed));
  721. var sync = 0;
  722. var lastsync = 0;
  723. function scrolling() {
  724. sync=(new Date()).getTime();
  725. if (sync<lastsync) return (self.timer = setAnimationFrame(scrolling));
  726. lastsync = (new Date()).getTime()+self.opt.scrollspeed;
  727. sync=0;
  728. var gp = self.newscrolly - self.getScrollTop();
  729. var df = (gp>0) ? Math.ceil(gp*rt) : Math.floor(gp*rt);
  730. var sc = self.getScrollTop()+df;
  731. self.setScrollTop(sc);
  732. if (sc == self.newscrolly) {
  733. // clearAnimationFrame(self.timer);
  734. self.timer = 0;
  735. self.cursorfreezed = false;
  736. } else {
  737. self.timer = setAnimationFrame(scrolling);
  738. }
  739. };
  740. self.timer = setAnimationFrame(scrolling);
  741. self.noticeCursor();
  742. };
  743. }
  744. this.doScrollBy = function(stp,absolute) {
  745. if (absolute) stp = Math.round(stp * 1/self.scrollratio.y);
  746. var ny = self.scroll.y-stp;
  747. if (ny<0) ny=0;
  748. var my = self.scrollvaluemax;
  749. if (ny>my) ny=my;
  750. self.cursorfreezed = false;
  751. self.doScroll(Math.floor(ny*self.scrollratio.y));
  752. };
  753. this.doScrollTo = function(pos,absolute) {
  754. ny=(absolute)?Math.round(pos * 1/self.scrollratio.y):pos;
  755. if (ny<0) pos=0;
  756. var my = self.scrollvaluemax;
  757. if (ny>my) pos=(absolute)?Math.round(my*self.scrollratio.y):my;
  758. self.cursorfreezed = false;
  759. self.doScroll((absolute)?pos:Math.round(pos*self.scrollratio.y));
  760. };
  761. this.doScrollMomentum = function(mom) {
  762. var dy = mom.ly-mom.ny;
  763. var tt = ((new Date()).getTime()-mom.lt);
  764. var my = Math.floor((dy*3)/((tt+1)*0.2));
  765. if (my) self.doScrollBy(my,true);
  766. };
  767. self.onscroll = function(e) {
  768. var tm = (new Date()).getTime();
  769. if (!self.lastcontentcheck || self.lastcontentcheck<tm) {
  770. self.lastcontentcheck=tm+500;
  771. var pg = self.getContentSize();
  772. if (pg.h!=self.page.h) self.onResize();
  773. }
  774. if (self.rail.drag) return;
  775. if (!self.cursorfreezed) self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  776. self.noticeCursor();
  777. };
  778. self.docscroll.scroll(function(e) {
  779. self.onscroll(e);
  780. });
  781. this.doZoomIn = function(e) {
  782. if (self.zoomactive) return;
  783. self.zoomactive = true;
  784. self.zoomrestore = {
  785. style:{}
  786. };
  787. var lst = ['position','top','left','zIndex','backgroundColor','marginTop','marginBottom','marginLeft','marginRight'];
  788. var win = self.win[0].style;
  789. for(var a in lst) {
  790. var pp = lst[a];
  791. self.zoomrestore.style[pp] = (typeof win[pp]!='undefined') ? win[pp] : '';
  792. }
  793. self.zoomrestore.style.width = self.win.css('width');
  794. self.zoomrestore.style.height = self.win.css('height');
  795. self.zoomrestore.padding = {
  796. w:self.win.outerWidth()-self.win.width(),
  797. h:self.win.outerHeight()-self.win.height()
  798. };
  799. if (self.isios4) {
  800. self.zoomrestore.scrollTop = $(window).scrollTop();
  801. $(window).scrollTop(0);
  802. }
  803. self.win.css({
  804. "position":(self.isios4)?"absolute":"fixed",
  805. "top":0,
  806. "left":0,
  807. "z-index":self.opt.zindex+100,
  808. "margin":"0px"
  809. });
  810. var bkg = self.win.css("backgroundColor");
  811. if (bkg==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor","#fff");
  812. self.rail.css({"z-index":self.opt.zindex+110});
  813. self.zoom.css({"z-index":self.opt.zindex+112});
  814. self.zoom.css('backgroundPosition','0px -18px');
  815. self.resizeZoom();
  816. return self.cancelEvent(e);
  817. };
  818. this.doZoomOut = function(e) {
  819. if (!self.zoomactive) return;
  820. self.zoomactive = false;
  821. self.win.css("margin","");
  822. self.win.css(self.zoomrestore.style);
  823. if (self.isios4) {
  824. $(window).scrollTop(self.zoomrestore.scrollTop);
  825. }
  826. self.rail.css({"z-index":(self.ispage)?self.opt.zindex:self.opt.zindex+2});
  827. self.zoom.css({"z-index":self.opt.zindex});
  828. self.zoomrestore = false;
  829. self.zoom.css('backgroundPosition','0px 0px');
  830. self.onResize();
  831. return self.cancelEvent(e);
  832. };
  833. this.doZoom = function(e) {
  834. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  835. };
  836. this.resizeZoom = function() {
  837. if (!self.zoomactive) return;
  838. var py = self.getScrollTop(); //preserve scrolling position
  839. self.win.css({
  840. width:$(window).width()-self.zoomrestore.padding.w+"px",
  841. height:$(window).height()-self.zoomrestore.padding.h+"px"
  842. });
  843. self.setScrollTop(py);
  844. self.onResize();
  845. };
  846. this.init();
  847. };
  848. $.fn.niceScroll = function(wrapper,opt) {
  849. if ((typeof wrapper=="object") && (typeof opt=="undefined")) {
  850. opt = wrapper;
  851. wrapper = false;
  852. }
  853. var ret = [];
  854. if (typeof opt=="undefined") opt = {};
  855. if (wrapper||false) {
  856. opt.doc = $(wrapper);
  857. opt.win = $(this);
  858. }
  859. var docundef = !("doc" in opt);
  860. this.each(function() {
  861. var nice = $(this).data('__nicescroll')||false;
  862. if (!nice) {
  863. opt.doc = (docundef) ? $(this) : opt.doc;
  864. nice = new NiceScrollClass(opt);
  865. $(this).data('__nicescroll',nice);
  866. }
  867. ret.push(nice);
  868. });
  869. return (ret.length==1) ? ret[0] : ret;
  870. };
  871. // override jQuery scrollTop
  872. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  873. $.cssHooks.scrollTop = {
  874. get: function(elem,computed,extra) {
  875. var nice = $.data(elem,'__nicescroll')||false;
  876. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  877. },
  878. set: function(elem,value) {
  879. var nice = $.data(elem,'__nicescroll')||false;
  880. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem,value);
  881. return this;
  882. }
  883. };
  884. jQuery.fn.scrollTop = function(value) {
  885. if (typeof value == "undefined") {
  886. var nice = $.data(this,'__nicescroll')||false;
  887. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  888. } else {
  889. return this.each(function() {
  890. var nice = $.data(this,'__nicescroll')||false;
  891. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this),value);
  892. });
  893. }
  894. }
  895. jQuery.fn.getNiceScroll = function(index) {
  896. if (typeof index == "undefined") {
  897. for(a=0;a<this.length;a++) {
  898. var nice = $.data(this[a],'__nicescroll')||false;
  899. if (nice) return nice;
  900. };
  901. return false;
  902. } else {
  903. var nice = $.data(this[index],'__nicescroll')||false;
  904. return nice;
  905. }
  906. }
  907. })( jQuery );