jquery.nicescroll.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /* jquery.nicescroll
  2. -- version 2.8.5
  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.5';
  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. self.view = {
  725. w:(self.ispage) ? self.win.width() : self.win.innerWidth(),
  726. h:(self.ispage) ? self.win.height() : self.win.innerHeight()
  727. };
  728. self.page = (page) ? page : self.getContentSize();
  729. self.page.maxh = Math.max(0,self.page.h - self.view.h);
  730. if (self.page.maxh == premaxh) return self; //nothing to do
  731. if (self.page.maxh==0) {
  732. self.hide();
  733. self.scrollvaluemax = 0;
  734. self.scroll.y = 0;
  735. self.scrollratio = {x:0,y:0};
  736. self.cursorheight = 0;
  737. self.locked = true;
  738. self.setScrollTop(0);
  739. return false;
  740. }
  741. else if (!self.visibility) self.show();
  742. self.locked = false;
  743. if (self.istextarea&&self.win.css('resize')&&self.win.css('resize')!='none') self.view.h-=20;
  744. if (!self.ispage) self.updateScrollBar(self.view);
  745. self.cursorheight = Math.min(self.view.h,Math.round(self.view.h * (self.view.h / self.page.h)));
  746. self.cursorheight = Math.max(self.opt.cursorminheight,self.cursorheight);
  747. self.scrollvaluemax = self.view.h-self.cursorheight-self.cursor.hborder;
  748. self.scrollratio = {
  749. x:0,
  750. y:(self.page.maxh/self.scrollvaluemax)
  751. };
  752. var sy = self.getScrollTop();
  753. if (sy>self.page.maxh) {
  754. self.doScroll(self.page.maxh);
  755. } else {
  756. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  757. self.noticeCursor();
  758. }
  759. return self;
  760. };
  761. this.resize = this.onResize; // hide internal method -- in future name can change
  762. this._bind = function(el,name,fn,bubble) { // primitive bind
  763. self.events.push({e:el,n:name,f:fn});
  764. if (el.addEventListener) {
  765. el.addEventListener(name,fn,bubble||false);
  766. }
  767. else if (el.attachEvent) {
  768. el.attachEvent("on"+name,fn);
  769. }
  770. else {
  771. el["on"+name] = fn;
  772. }
  773. };
  774. this.bind = function(dom,name,fn,bubble) { // touch-oriented & fixing jquery bind
  775. var el = ("jquery" in dom) ? dom[0] : dom;
  776. if (el.addEventListener) {
  777. if (self.cantouch && /mouseup|mousedown|mousemove/.test(name)) { // touch device support
  778. var tt = (name=='mousedown')?'touchstart':(name=='mouseup')?'touchend':'touchmove';
  779. self._bind(el,tt,function(e){
  780. if(e.touches.length<2){var ev=(e.touches.length>0)?e.touches[0]:e;ev.original=e;fn.call(this,ev);}
  781. },bubble||false);
  782. }
  783. self._bind(el,name,fn,bubble||false);
  784. if (name=='mousewheel') self._bind(el,"DOMMouseScroll",fn,bubble||false);
  785. if (self.cantouch && name=="mouseup") self._bind(el,"touchcancel",fn,bubble||false);
  786. }
  787. else {
  788. self._bind(el,name,function(e) {
  789. e = e||window.event||false;
  790. if (e) {
  791. if (e.srcElement) e.target=e.srcElement;
  792. }
  793. return ((fn.call(el,e)===false)||bubble===false) ? self.cancelEvent(e) : true;
  794. });
  795. }
  796. };
  797. this._unbind = function(el,name,fn) { // primitive unbind
  798. if (el.removeEventListener) {
  799. el.removeEventListener(name,fn,false);
  800. }
  801. else if (el.detachEvent) {
  802. el.detachEvent('on'+name,fn);
  803. } else {
  804. el['on'+name] = false;
  805. }
  806. };
  807. this.unbindAll = function() {
  808. for(var a=0;a<self.events.length;a++) {
  809. var r = self.events[a];
  810. self._unbind(r.e,r.n,r.f);
  811. }
  812. };
  813. // Thanks to http://www.switchonthecode.com !!
  814. this.cancelEvent = function(e) {
  815. if (self.cantouch) {
  816. e = e.original ? e.original : e||false;
  817. } else {
  818. e = e ? e : window.event||false;
  819. }
  820. if (!e) return false;
  821. if(e.stopPropagation) e.stopPropagation();
  822. if(e.preventDefault) e.preventDefault();
  823. e.cancelBubble = true;
  824. e.cancel = true;
  825. e.returnValue = false;
  826. return false;
  827. };
  828. this.show = function() {
  829. if (self.page.maxh!=0) {
  830. self.visibility = true;
  831. self.rail.css('display','block');
  832. }
  833. return self;
  834. };
  835. this.hide = function() {
  836. self.visibility = false;
  837. self.rail.css('display','none');
  838. return self;
  839. };
  840. this.remove = function() {
  841. self.doZoomOut();
  842. self.unbindAll();
  843. self.events = [];
  844. self.rail.remove();
  845. if (self.zoom) self.zoom.remove();
  846. self.cursor = false;
  847. self.rail = false;
  848. self.zoom = false;
  849. for(var a=0;a<self.saved.css.length;a++) {
  850. var d=self.saved.css[a];
  851. d[0].css(d[1],(typeof d[2]=="undefined") ? '' : d[2]);
  852. }
  853. self.saved = false;
  854. self.me.data('__nicescroll',''); //erase all traces
  855. return self;
  856. };
  857. this.isScrollable = function(e) {
  858. var dom = (e.target) ? e.target : e;
  859. while (dom&&dom.nodeName&&!(/BODY|HTML/.test(dom.nodeName))) {
  860. var dd = $(dom);
  861. var ov = dd.css('overflowY')||dd.css('overflow')||'';
  862. if (/scroll|auto/.test(ov)) return (dom.clientHeight!=dom.scrollHeight);
  863. dom = (dom.parentNode) ? dom.parentNode : false;
  864. }
  865. return false;
  866. };
  867. this.onmousewheel = function(e) {
  868. if (self.locked&&self.page.maxh==0) return true;
  869. if (self.opt.preservenativescrolling&&self.checkarea) {
  870. self.checkarea = false;
  871. self.nativescrollingarea = self.isScrollable(e);
  872. }
  873. if (self.nativescrollingarea) return true; // this isn't my business
  874. if (self.locked) return self.cancelEvent(e);
  875. if (self.rail.drag) return self.cancelEvent(e);
  876. var delta = 0;
  877. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  878. if (delta) {
  879. if (self.scrollmom) self.scrollmom.y.stop();
  880. self.doScrollBy(delta*self.opt.mousescrollstep);
  881. }
  882. return self.cancelEvent(e);
  883. };
  884. this.stop = function() {
  885. self.cancelScroll();
  886. if (self.scrollmon) self.scrollmon.stop();
  887. self.cursorfreezed = false;
  888. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  889. self.noticeCursor();
  890. return self;
  891. };
  892. if (self.ishwscroll&&self.hastransition&&self.opt.usetransition) {
  893. this.prepareTransition = function(dif,trend) {
  894. var sp = Math.round(self.opt.scrollspeed*10);
  895. var ex = Math.min(sp,Math.round((dif / 20) * self.opt.scrollspeed));
  896. var trans = (ex>20) ? self.prefixstyle+'transform '+ex+'ms ease-out 0s' : '';
  897. if (!self.lasttransitionstyle||self.lasttransitionstyle!=trans) {
  898. self.lasttransitionstyle = trans;
  899. self.doc.css(self.transitionstyle,trans);
  900. }
  901. return ex;
  902. };
  903. this.doScroll = function(y,spd) { //trans
  904. var sy = self.getScrollTop();
  905. if (y<0&&sy<=0) return self.noticeCursor();
  906. else if (y>self.page.maxh&&sy>=self.page.maxh) {
  907. self.checkContentSize();
  908. return self.noticeCursor();
  909. }
  910. self.newscrolly = y;
  911. self.newscrollspeed = spd||false;
  912. if (self.timer) return false;
  913. if (!self.scrollendtrapped) {
  914. self.scrollendtrapped = true;
  915. self.bind(self.doc,self.transitionend,self.onScrollEnd,false); //I have got to do something usefull!!
  916. }
  917. self.timer = setTimeout(function(){
  918. var top = self.getScrollTop();
  919. var dif = (self.newscrollspeed)?self.newscrollspeed:Math.abs(top-self.newscrolly);
  920. var ms = self.prepareTransition(dif);
  921. self.timer = setTimeout(function(){
  922. if (self.newscrolly<0&&!self.opt.bouncescroll) self.newscrolly = 0
  923. else if (self.newscrolly>self.page.maxh&&!self.opt.bouncescroll) self.newscrolly = self.page.maxh;
  924. if (self.newscrolly==self.getScrollTop()) {
  925. self.timer = 0;
  926. self.onScrollEnd();
  927. } else {
  928. var py = self.getScrollTop();
  929. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  930. if (ms>0) {
  931. self.timerscroll = {
  932. ts:(new Date()).getTime(),
  933. s:self.getScrollTop(),
  934. e:self.newscrolly,
  935. sp:ms,
  936. bz: new BezierClass(py,self.newscrolly,ms,0,1,0,1)
  937. };
  938. if (!self.cursorfreezed) self.timerscroll.tm=setInterval(function(){self.showCursor(self.getScrollTop())},60);
  939. }
  940. self.setScrollTop(self.newscrolly);
  941. self.timer = 0;
  942. }
  943. // self.noticeCursor();
  944. },15);
  945. },self.opt.scrollspeed);
  946. };
  947. this.cancelScroll = function() {
  948. if (!self.scrollendtrapped) return true;
  949. var py = self.getScrollTop();
  950. self.scrollendtrapped = false;
  951. self._unbind(self.doc,self.transitionend,self.onScrollEnd);
  952. self.prepareTransition(0);
  953. self.setScrollTop(py); // fire event onscroll
  954. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  955. self.timerscroll = false;
  956. self.cursorfreezed = false;
  957. // self.scrollstart = false;
  958. self.noticeCursor(false,py);
  959. return self;
  960. };
  961. this.onScrollEnd = function() {
  962. self.scrollendtrapped = false;
  963. self._unbind(self.doc,self.transitionend,self.onScrollEnd);
  964. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  965. self.timerscroll = false;
  966. self.cursorfreezed = false;
  967. var py = self.getScrollTop();
  968. self.setScrollTop(py); // fire event onscroll
  969. self.noticeCursor(false,py);
  970. if (py<0) self.doScroll(0,60)
  971. else if (py>self.page.maxh) self.doScroll(self.page.maxh,60);
  972. // else self.checkContentSize();
  973. };
  974. } else {
  975. this.doScroll = function(y) { //no-trans
  976. var py = self.getScrollTop();
  977. self.newscrolly = y;
  978. if (!self.bouncescroll) {
  979. if (self.newscrolly<0) {
  980. if (self.newspeedy) self.newspeedy.x = 0;
  981. self.newscrolly = 0;
  982. }
  983. else if (self.newscrolly>self.page.maxh) {
  984. if (self.newspeedy) self.newspeedy.x = self.page.maxh;
  985. self.newscrolly = self.page.maxh;
  986. }
  987. }
  988. var mg = Math.floor(Math.abs(y-py)/40);
  989. if (mg>0) {
  990. var ms = Math.min(10,mg)*100;
  991. self.bzscroll = (self.bzscroll) ? self.bzscroll.update(y,ms) : new BezierClass(py,y,ms,0,1,0,1);
  992. } else {
  993. self.bzscroll = false;
  994. }
  995. if (self.timer) return;
  996. if (py==self.page.maxh&&y>=self.page.maxh) self.checkContentSize();
  997. var sync = 1;
  998. function scrolling() {
  999. if (self.cancelAnimationFrame) return true;
  1000. sync = 1-sync;
  1001. if (sync) return (self.timer = setAnimationFrame(scrolling)||1);
  1002. var sy = self.getScrollTop();
  1003. var sc = (self.bzscroll) ? self.bzscroll.getNow() : self.newscrolly;
  1004. var dr=sc-sy;
  1005. if ((dr<0&&sc<self.newscrolly)||(dr>0&&sc>self.newscrolly)) sc = self.newscrolly;
  1006. self.setScrollTop(sc);
  1007. if (sc == self.newscrolly) {
  1008. // clearAnimationFrame(self.timer);
  1009. self.timer = 0;
  1010. self.cursorfreezed = false;
  1011. self.bzscroll = false;
  1012. if (sc<0) self.doScroll(0);
  1013. else if (sc>self.page.maxh) self.doScroll(self.page.maxh);
  1014. // else self.checkContentSize();
  1015. } else {
  1016. self.timer = setAnimationFrame(scrolling)||1;
  1017. }
  1018. };
  1019. self.cancelAnimationFrame=false;
  1020. self.timer = 1;
  1021. scrolling();
  1022. if (py==self.page.maxh&&y>=py) self.checkContentSize();
  1023. self.noticeCursor();
  1024. };
  1025. this.cancelScroll = function() {
  1026. if (self.timer) clearAnimationFrame(self.timer);
  1027. self.timer = 0;
  1028. self.bzscroll = false;
  1029. return self;
  1030. };
  1031. }
  1032. this.doScrollBy = function(stp,relative) {
  1033. var ny = 0;
  1034. if (relative) {
  1035. ny = Math.floor((self.scroll.y-stp)*self.scrollratio.y)
  1036. } else {
  1037. var sy = (self.timer) ? self.newscrolly : self.getScrollTop(true);
  1038. ny = sy-stp;
  1039. }
  1040. if (self.bouncescroll) {
  1041. var haf = Math.round(self.view.h/2);
  1042. if (ny<-haf) ny=-haf
  1043. else if (ny>(self.page.maxh+haf)) ny = (self.page.maxh+haf);
  1044. }
  1045. self.cursorfreezed = false;
  1046. self.doScroll(ny);
  1047. };
  1048. this.doScrollTo = function(pos,relative) {
  1049. var ny = (relative) ? Math.round(pos*self.scrollratio.y) : pos;
  1050. if (ny<0) ny=0
  1051. else if (ny>self.page.maxh) ny = self.page.maxh;
  1052. self.cursorfreezed = false;
  1053. self.doScroll(pos);
  1054. };
  1055. this.checkContentSize = function() {
  1056. var pg = self.getContentSize();
  1057. if (pg.h!=self.page.h) self.resize(false,pg);
  1058. };
  1059. self.onscroll = function(e) {
  1060. if (self.rail.drag) return;
  1061. if (!self.cursorfreezed) {
  1062. self.delayed('onscroll',function(){
  1063. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1064. self.noticeCursor();
  1065. },30);
  1066. }
  1067. };
  1068. self.bind(self.docscroll,"scroll",self.onscroll);
  1069. this.doZoomIn = function(e) {
  1070. if (self.zoomactive) return;
  1071. self.zoomactive = true;
  1072. self.zoomrestore = {
  1073. style:{}
  1074. };
  1075. var lst = ['position','top','left','zIndex','backgroundColor','marginTop','marginBottom','marginLeft','marginRight'];
  1076. var win = self.win[0].style;
  1077. for(var a in lst) {
  1078. var pp = lst[a];
  1079. self.zoomrestore.style[pp] = (typeof win[pp]!='undefined') ? win[pp] : '';
  1080. }
  1081. self.zoomrestore.style.width = self.win.css('width');
  1082. self.zoomrestore.style.height = self.win.css('height');
  1083. self.zoomrestore.padding = {
  1084. w:self.win.outerWidth()-self.win.width(),
  1085. h:self.win.outerHeight()-self.win.height()
  1086. };
  1087. if (self.isios4) {
  1088. self.zoomrestore.scrollTop = $(window).scrollTop();
  1089. $(window).scrollTop(0);
  1090. }
  1091. self.win.css({
  1092. "position":(self.isios4)?"absolute":"fixed",
  1093. "top":0,
  1094. "left":0,
  1095. "z-index":self.opt.zindex+100,
  1096. "margin":"0px"
  1097. });
  1098. var bkg = self.win.css("backgroundColor");
  1099. if (bkg==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor","#fff");
  1100. self.rail.css({"z-index":self.opt.zindex+110});
  1101. self.zoom.css({"z-index":self.opt.zindex+112});
  1102. self.zoom.css('backgroundPosition','0px -18px');
  1103. self.resizeZoom();
  1104. return self.cancelEvent(e);
  1105. };
  1106. this.doZoomOut = function(e) {
  1107. if (!self.zoomactive) return;
  1108. self.zoomactive = false;
  1109. self.win.css("margin","");
  1110. self.win.css(self.zoomrestore.style);
  1111. if (self.isios4) {
  1112. $(window).scrollTop(self.zoomrestore.scrollTop);
  1113. }
  1114. self.rail.css({"z-index":(self.ispage)?self.opt.zindex:self.opt.zindex+2});
  1115. self.zoom.css({"z-index":self.opt.zindex});
  1116. self.zoomrestore = false;
  1117. self.zoom.css('backgroundPosition','0px 0px');
  1118. self.onResize();
  1119. return self.cancelEvent(e);
  1120. };
  1121. this.doZoom = function(e) {
  1122. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  1123. };
  1124. this.resizeZoom = function() {
  1125. if (!self.zoomactive) return;
  1126. var py = self.getScrollTop(); //preserve scrolling position
  1127. self.win.css({
  1128. width:$(window).width()-self.zoomrestore.padding.w+"px",
  1129. height:$(window).height()-self.zoomrestore.padding.h+"px"
  1130. });
  1131. self.onResize();
  1132. self.setScrollTop(Math.min(self.page.maxh,py));
  1133. };
  1134. this.init();
  1135. };
  1136. // Inspired by the work of Kin Blas
  1137. // http://webpro.host.adobe.com/people/jblas/momentum/includes/jquery.momentum.0.7.js
  1138. var ScrollMomentumClass = function(nc) {
  1139. var self = this;
  1140. this.nc = nc;
  1141. this.lasty = 0;
  1142. this.speedy = 0;
  1143. this.lasttime = 0;
  1144. this.snapy = false;
  1145. this.demuly = 0;
  1146. this.timer = 0;
  1147. this.time = function() {
  1148. return (new Date()).getTime();
  1149. };
  1150. this.reset = function(py) {
  1151. self.stop();
  1152. self.lasttime = self.time();
  1153. self.speedy = 0;
  1154. self.lasty = py;
  1155. };
  1156. this.update = function(py) {
  1157. self.lasttime = self.time();
  1158. var dy = py - self.lasty;
  1159. var sy = nc.getScrollTop();
  1160. var newy = sy + dy;
  1161. self.snapy = (newy<0)||(newy>self.nc.page.maxh);
  1162. self.speedy = dy;
  1163. self.lasty = py;
  1164. };
  1165. this.stop = function() {
  1166. if (self.timer) {
  1167. clearTimeout(self.timer);
  1168. self.timer = 0;
  1169. }
  1170. };
  1171. this.doSnapy = function(ny) {
  1172. if (ny<0) {
  1173. self.nc.doScroll(0,60);
  1174. }
  1175. else if (ny>self.nc.page.maxh) {
  1176. self.nc.doScroll(self.nc.page.maxh,60);
  1177. }
  1178. };
  1179. this.doMomentum = function() {
  1180. var l = self.lasttime;
  1181. var t = self.time();
  1182. self.speedy = Math.min(60,self.speedy);
  1183. var chk = l && (t - l) <= 50;
  1184. var sy = (self.speedy && chk) ? self.speedy : false;
  1185. if (sy) {
  1186. var tm = t-l;
  1187. var pageh = self.nc.page.maxh;
  1188. self.demuly = 0;
  1189. var onscroll = function(){
  1190. var ny = Math.floor(self.nc.getScrollTop() - (self.speedy*(1-self.demuly)));
  1191. if ((ny<0)||(ny>pageh)) {
  1192. self.demuly+=0.08;
  1193. } else {
  1194. self.demuly+=0.01;
  1195. }
  1196. self.nc.setScrollTop(ny);
  1197. self.nc.showCursor(ny);
  1198. if(self.demuly<1) {
  1199. self.timer = setTimeout(onscroll,tm);
  1200. } else {
  1201. self.timer = 0;
  1202. self.nc.hideCursor();
  1203. self.doSnapy(ny);
  1204. }
  1205. };
  1206. onscroll();
  1207. } else {
  1208. if (self.snapy) {
  1209. self.doSnapy(self.nc.getScrollTop());
  1210. }
  1211. }
  1212. }
  1213. };
  1214. // override jQuery scrollTop
  1215. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  1216. $.cssHooks.scrollTop = {
  1217. get: function(elem,computed,extra) {
  1218. var nice = $.data(elem,'__nicescroll')||false;
  1219. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  1220. },
  1221. set: function(elem,value) {
  1222. var nice = $.data(elem,'__nicescroll')||false;
  1223. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem,value);
  1224. return this;
  1225. }
  1226. };
  1227. /* ====================================> TO INSPECT
  1228. $.fx.step["scrollTop"] = function(fx){
  1229. if (fx.start=='') fx.start=$.cssHooks.scrollTop.get(fx.elem);
  1230. $.cssHooks.scrollTop.set(fx.elem,fx.now+fx.unit);
  1231. };
  1232. */
  1233. jQuery.fn.scrollTop = function(value) {
  1234. if (typeof value == "undefined") {
  1235. var nice = (this[0]) ? $.data(this[0],'__nicescroll')||false : false;
  1236. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  1237. } else {
  1238. return this.each(function() {
  1239. var nice = $.data(this,'__nicescroll')||false;
  1240. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this),value);
  1241. });
  1242. }
  1243. }
  1244. var NiceScrollArray = function(doms) {
  1245. var self = this;
  1246. this.length = 0;
  1247. this.name = "nicescrollarray";
  1248. this.each = function(fn) {
  1249. for(var a=0;a<self.length;a++) fn.call(self[a]);
  1250. return self;
  1251. };
  1252. this.push = function(nice) {
  1253. self[self.length]=nice;
  1254. self.length++;
  1255. };
  1256. this.eq = function(idx) {
  1257. return self[idx];
  1258. };
  1259. if (doms) {
  1260. for(a=0;a<doms.length;a++) {
  1261. var nice = $.data(doms[a],'__nicescroll')||false;
  1262. if (nice) {
  1263. this[this.length]=nice;
  1264. this.length++;
  1265. }
  1266. };
  1267. }
  1268. return this;
  1269. };
  1270. function mplex(el,lst,fn) {
  1271. for(var a=0;a<lst.length;a++) fn(el,lst[a]);
  1272. };
  1273. mplex(
  1274. NiceScrollArray.prototype,
  1275. ['show','hide','onResize','resize','remove','stop'],
  1276. function(e,n) {
  1277. e[n] = function(){
  1278. return this.each(function(){
  1279. this[n].call();
  1280. });
  1281. };
  1282. }
  1283. );
  1284. jQuery.fn.getNiceScroll = function(index) {
  1285. if (typeof index == "undefined") {
  1286. return new NiceScrollArray(this);
  1287. } else {
  1288. var nice = $.data(this[index],'__nicescroll')||false;
  1289. return nice;
  1290. }
  1291. };
  1292. jQuery.extend(jQuery.expr[':'], {
  1293. nicescroll: function(a) {
  1294. return ($.data(a,'__nicescroll'))?true:false;
  1295. }
  1296. });
  1297. $.fn.niceScroll = function(wrapper,opt) {
  1298. if (typeof opt=="undefined") {
  1299. if ((typeof wrapper=="object")&&!("jquery" in wrapper)) {
  1300. opt = wrapper;
  1301. wrapper = false;
  1302. }
  1303. }
  1304. var ret = new NiceScrollArray();
  1305. if (typeof opt=="undefined") opt = {};
  1306. if (wrapper||false) {
  1307. opt.doc = $(wrapper);
  1308. opt.win = $(this);
  1309. }
  1310. var docundef = !("doc" in opt);
  1311. if (!docundef&&!("win" in opt)) opt.win = $(this);
  1312. this.each(function() {
  1313. var nice = $(this).data('__nicescroll')||false;
  1314. if (!nice) {
  1315. opt.doc = (docundef) ? $(this) : opt.doc;
  1316. nice = new NiceScrollClass(opt,$(this));
  1317. $(this).data('__nicescroll',nice);
  1318. }
  1319. ret.push(nice);
  1320. });
  1321. return (ret.length==1) ? ret[0] : ret;
  1322. };
  1323. window.NiceScroll = {
  1324. getjQuery:function(){return jQuery}
  1325. };
  1326. })( jQuery );