jquery.nicescroll.fl.js 37 KB

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