jquery.nicescroll.js 49 KB

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