jquery.nicescroll.js 52 KB

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