jquery.nicescroll.js 50 KB

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