Advertisement
firoze

browser scrollbar style

Mar 16th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 99.06 KB | None | 0 0
  1. // browser scroll bar style
  2.  
  3. // css
  4. #ascrail2000{
  5. background-color:red;
  6. cursor:pointer;
  7. }
  8. #ascrail2000>div{
  9. background-color:#4e97cc!important;
  10. width:8px!important;
  11. cursor:pointer;
  12. border-radius:0!important;
  13. -webkit-border-radius:0!important;
  14. -moz-border-radius:0!important;
  15. }
  16. #success{width: 100%;
  17. padding: 10px;
  18. text-align: center;
  19. color: green;
  20. display:none;
  21. }
  22. #error{
  23. width: 100%;
  24. padding: 10px;
  25. text-align: center;
  26. color: red;
  27. display:none;
  28. }
  29.  
  30.  
  31.  
  32.  
  33. // browser nice scroll bar js with active
  34.  
  35.  
  36. // active js
  37. $(document).ready(function(){
  38. $("html").niceScroll({ autohidemode: false });
  39. });
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /* jquery.nicescroll
  48. -- version 3.1.0
  49. -- copyright 2011-12 InuYaksa*2012
  50. -- licensed under the MIT
  51. --
  52. -- http://areaaperta.com/nicescroll
  53. -- https://github.com/inuyaksa/jquery.nicescroll
  54. --
  55. */
  56.  
  57. (function(jQuery){
  58.  
  59. // globals
  60. var domfocus = false;
  61. var mousefocus = false;
  62. var zoomactive = false;
  63. var tabindexcounter = 5000;
  64. var ascrailcounter = 2000;
  65.  
  66. var $ = jQuery; // sandbox
  67.  
  68. // http://stackoverflow.com/questions/2161159/get-script-path
  69. function getScriptPath() {
  70. var scripts=document.getElementsByTagName('script');
  71. var path=scripts[scripts.length-1].src.split('?')[0];
  72. return (path.split('/').length>0) ? path.split('/').slice(0,-1).join('/')+'/' : '';
  73. }
  74. var scriptpath = getScriptPath();
  75.  
  76. // derived by http://blog.joelambert.co.uk/2011/06/01/a-better-settimeoutsetinterval/
  77. var setAnimationFrame = (function(){
  78. return window.requestAnimationFrame ||
  79. window.webkitRequestAnimationFrame ||
  80. window.mozRequestAnimationFrame ||
  81. window.oRequestAnimationFrame ||
  82. window.msRequestAnimationFrame ||
  83. false;
  84. })();
  85. var clearAnimationFrame = (function(){
  86. return window.cancelRequestAnimationFrame ||
  87. window.webkitCancelRequestAnimationFrame ||
  88. window.mozCancelRequestAnimationFrame ||
  89. window.oCancelRequestAnimationFrame ||
  90. window.msCancelRequestAnimationFrame ||
  91. false;
  92. })();
  93.  
  94. var browserdetected = false;
  95.  
  96. var getBrowserDetection = function() {
  97.  
  98. if (browserdetected) return browserdetected;
  99.  
  100. var domtest = document.createElement('DIV');
  101.  
  102. var d = {};
  103.  
  104. d.haspointerlock = "pointerLockElement" in document || "mozPointerLockElement" in document || "webkitPointerLockElement" in document;
  105.  
  106. d.isopera = ("opera" in window);
  107. d.isopera12 = (d.isopera&&("getUserMedia" in navigator));
  108.  
  109. d.isie = (("all" in document) && ("attachEvent" in domtest) && !d.isopera);
  110. d.isieold = (d.isie && !("msInterpolationMode" in domtest.style)); // IE6 and older
  111. d.isie7 = d.isie&&!d.isieold&&(!("documentMode" in document)||(document.documentMode==7));
  112. d.isie8 = d.isie&&("documentMode" in document)&&(document.documentMode==8);
  113. d.isie9 = d.isie&&("performance" in window)&&(document.documentMode>=9);
  114. d.isie10 = d.isie&&("performance" in window)&&(document.documentMode>=10);
  115.  
  116. d.isie9mobile = /iemobile.9/i.test(navigator.userAgent); //wp 7.1 mango
  117. if (d.isie9mobile) d.isie9 = false;
  118. d.isie7mobile = (!d.isie9mobile&&d.isie7) && /iemobile/i.test(navigator.userAgent); //wp 7.0
  119.  
  120. d.ismozilla = ("MozAppearance" in domtest.style);
  121.  
  122. d.iswebkit = ("WebkitAppearance" in domtest.style);
  123.  
  124. d.ischrome = ("chrome" in window);
  125. d.ischrome22 = (d.ischrome&&d.haspointerlock);
  126.  
  127. d.cantouch = ("ontouchstart" in document.documentElement)||("ontouchstart" in window); // detection for Chrome Touch Emulation
  128. d.hasmstouch = (window.navigator.msPointerEnabled||false); // IE10+ pointer events
  129.  
  130. d.ismac = /^mac$/i.test(navigator.platform);
  131.  
  132. d.isios = (d.cantouch && /iphone|ipad|ipod/i.test(navigator.platform));
  133. d.isios4 = ((d.isios)&&!("seal" in Object));
  134.  
  135. d.isandroid = (/android/i.test(navigator.userAgent));
  136.  
  137. d.trstyle = false;
  138. d.hastransform = false;
  139. d.hastranslate3d = false;
  140. d.transitionstyle = false;
  141. d.hastransition = false;
  142. d.transitionend = false;
  143.  
  144. var check = ['transform','msTransform','webkitTransform','MozTransform','OTransform'];
  145. for(var a=0;a<check.length;a++){
  146. if (typeof domtest.style[check[a]] != "undefined") {
  147. d.trstyle = check[a];
  148. break;
  149. }
  150. }
  151. d.hastransform = (d.trstyle != false);
  152. if (d.hastransform) {
  153. domtest.style[d.trstyle] = "translate3d(1px,2px,3px)";
  154. d.hastranslate3d = /translate3d/.test(domtest.style[d.trstyle]);
  155. }
  156.  
  157. d.transitionstyle = false;
  158. d.prefixstyle = '';
  159. d.transitionend = false;
  160. var check = ['transition','webkitTransition','MozTransition','OTransition','OTransition','msTransition','KhtmlTransition'];
  161. var prefix = ['','-webkit-','-moz-','-o-','-o','-ms-','-khtml-'];
  162. var evs = ['transitionend','webkitTransitionEnd','transitionend','otransitionend','oTransitionEnd','msTransitionEnd','KhtmlTransitionEnd'];
  163. for(var a=0;a<check.length;a++) {
  164. if (check[a] in domtest.style) {
  165. d.transitionstyle = check[a];
  166. d.prefixstyle = prefix[a];
  167. d.transitionend = evs[a];
  168. break;
  169. }
  170. }
  171. d.hastransition = (d.transitionstyle);
  172.  
  173. function detectCursorGrab() {
  174. var lst = ['-moz-grab','-webkit-grab','grab'];
  175. if ((d.ischrome&&!d.ischrome22)||d.isie) lst=[]; // force setting for IE returns false positive and chrome cursor bug
  176. for(var a=0;a<lst.length;a++) {
  177. var p = lst[a];
  178. domtest.style['cursor']=p;
  179. if (domtest.style['cursor']==p) return p;
  180. }
  181. return 'url(http://www.google.com/intl/en_ALL/mapfiles/openhand.cur),n-resize'; // thank you google for custom cursor!
  182. }
  183. d.cursorgrabvalue = detectCursorGrab();
  184.  
  185. d.hasmousecapture = ("setCapture" in domtest);
  186.  
  187. domtest = null; //memory released
  188.  
  189. browserdetected = d;
  190.  
  191. return d;
  192. }
  193.  
  194. var NiceScrollClass = function(myopt,me) {
  195.  
  196. var self = this;
  197.  
  198. this.version = '3.1.0';
  199. this.name = 'nicescroll';
  200.  
  201. this.me = me;
  202.  
  203. this.opt = {
  204. doc:$("body"),
  205. win:false,
  206. zindex:9000,
  207. cursoropacitymin:0,
  208. cursoropacitymax:1,
  209. cursorcolor:"#424242",
  210. cursorwidth:"5px",
  211. cursorborder:"1px solid #fff",
  212. cursorborderradius:"5px",
  213. scrollspeed:60,
  214. mousescrollstep:8*3,
  215. touchbehavior:false,
  216. hwacceleration:true,
  217. usetransition:true,
  218. boxzoom:false,
  219. dblclickzoom:true,
  220. gesturezoom:true,
  221. grabcursorenabled:true,
  222. autohidemode:true,
  223. background:"",
  224. iframeautoresize:true,
  225. cursorminheight:32,
  226. preservenativescrolling:true,
  227. railoffset:false,
  228. bouncescroll:true,
  229. spacebarenabled:true,
  230. railpadding:{top:0,right:0,left:0,bottom:0},
  231. disableoutline:true,
  232. horizrailenabled:true,
  233. railalign:"right",
  234. railvalign:"bottom",
  235. enabletranslate3d:true,
  236. enablemousewheel:true,
  237. enablekeyboard:true,
  238. smoothscroll:true,
  239. sensitiverail:true
  240. };
  241.  
  242. // Options for internal use
  243. this.opt.snapbackspeed = 80;
  244.  
  245. if (myopt||false) {
  246. for(var a in self.opt) {
  247. if (typeof myopt[a] != "undefined") self.opt[a] = myopt[a];
  248. }
  249. }
  250.  
  251. this.doc = self.opt.doc;
  252. this.iddoc = (this.doc&&this.doc[0])?this.doc[0].id||'':'';
  253. this.ispage = /BODY|HTML/.test((self.opt.win)?self.opt.win[0].nodeName:this.doc[0].nodeName);
  254. this.haswrapper = (self.opt.win!==false);
  255. this.win = self.opt.win||(this.ispage?$(window):this.doc);
  256. this.docscroll = (this.ispage&&!this.haswrapper)?$(window):this.win;
  257. this.body = $("body");
  258. this.viewport = false;
  259.  
  260. this.isfixed = false;
  261.  
  262. this.iframe = false;
  263. this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName == 'IFRAME'));
  264.  
  265. this.istextarea = (this.win[0].nodeName == 'TEXTAREA');
  266.  
  267. this.forcescreen = false; //force to use screen position on events
  268.  
  269. this.canshowonmouseevent = (self.opt.autohidemode!="scroll");
  270.  
  271. // Events jump table
  272. this.onmousedown = false;
  273. this.onmouseup = false;
  274. this.onmousemove = false;
  275. this.onmousewheel = false;
  276. this.onkeypress = false;
  277. this.ongesturezoom = false;
  278. this.onclick = false;
  279.  
  280. // Nicescroll custom events
  281. this.onscrollstart = false;
  282. this.onscrollend = false;
  283. this.onscrollcancel = false;
  284.  
  285. this.onzoomin = false;
  286. this.onzoomout = false;
  287.  
  288. // Let's start!
  289. this.view = false;
  290. this.page = false;
  291.  
  292. this.scroll = {x:0,y:0};
  293. this.scrollratio = {x:0,y:0};
  294. this.cursorheight = 20;
  295. this.scrollvaluemax = 0;
  296.  
  297. this.scrollrunning = false;
  298.  
  299. this.scrollmom = false;
  300.  
  301. this.observer = false;
  302.  
  303. do {
  304. this.id = "ascrail"+(ascrailcounter++);
  305. } while (document.getElementById(this.id));
  306.  
  307. this.rail = false;
  308. this.cursor = false;
  309. this.cursorfreezed = false;
  310.  
  311. this.zoom = false;
  312. this.zoomactive = false;
  313.  
  314. this.hasfocus = false;
  315. this.hasmousefocus = false;
  316.  
  317. this.visibility = true;
  318. this.locked = false;
  319. this.hidden = false; // rails always hidden
  320. this.cursoractive = true; // user can interact with cursors
  321.  
  322. this.nativescrollingarea = false;
  323.  
  324. this.events = []; // event list for unbind
  325.  
  326. this.saved = {};
  327.  
  328. this.delaylist = {};
  329. this.synclist = {};
  330.  
  331. this.lastdeltax = 0;
  332. this.lastdeltay = 0;
  333.  
  334. this.detected = getBrowserDetection();
  335.  
  336. var cap = $.extend({},this.detected);
  337.  
  338. this.canhwscroll = (cap.hastransform&&self.opt.hwacceleration);
  339. this.ishwscroll = (this.canhwscroll&&self.haswrapper);
  340.  
  341. this.istouchcapable = false; // desktop devices with touch screen support
  342.  
  343. //## Check Chrome desktop with touch support
  344. if (cap.cantouch&&cap.ischrome&&!cap.isios&&!cap.isandroid) {
  345. this.istouchcapable = true;
  346. cap.cantouch = false; // parse normal desktop events
  347. }
  348.  
  349. //## Firefox 18 nightly build (desktop) false positive (or desktop with touch support)
  350. if (cap.cantouch&&cap.ismozilla&&!cap.isios) {
  351. this.istouchcapable = true;
  352. cap.cantouch = false; // parse normal desktop events
  353. }
  354.  
  355. this.delayed = function(name,fn,tm,lazy) {
  356. var dd = self.delaylist[name];
  357. var nw = (new Date()).getTime();
  358. if (!lazy&&dd&&dd.tt) return false;
  359. if (dd&&dd.tt) clearTimeout(dd.tt);
  360. if (dd&&dd.last+tm>nw&&!dd.tt) {
  361. self.delaylist[name] = {
  362. last:nw+tm,
  363. tt:setTimeout(function(){self.delaylist[name].tt=0;fn.call();},tm)
  364. }
  365. }
  366. else if (!dd||!dd.tt) {
  367. self.delaylist[name] = {
  368. last:nw,
  369. tt:0
  370. }
  371. setTimeout(function(){fn.call();},0);
  372. }
  373. };
  374.  
  375. this.synched = function(name,fn) {
  376.  
  377. function requestSync() {
  378. if (self.onsync) return;
  379. setAnimationFrame(function(){
  380. self.onsync = false;
  381. for(name in self.synclist){
  382. var fn = self.synclist[name];
  383. if (fn) fn.call(self);
  384. self.synclist[name] = false;
  385. }
  386. });
  387. self.onsync = true;
  388. };
  389.  
  390. self.synclist[name] = fn;
  391. requestSync();
  392. return name;
  393. };
  394.  
  395. this.unsynched = function(name) {
  396. if (self.synclist[name]) self.synclist[name] = false;
  397. }
  398.  
  399. this.css = function(el,pars) { // save & set
  400. for(var n in pars) {
  401. self.saved.css.push([el,n,el.css(n)]);
  402. el.css(n,pars[n]);
  403. }
  404. };
  405.  
  406. this.scrollTop = function(val) {
  407. return (typeof val == "undefined") ? self.getScrollTop() : self.setScrollTop(val);
  408. };
  409.  
  410. this.scrollLeft = function(val) {
  411. return (typeof val == "undefined") ? self.getScrollLeft() : self.setScrollLeft(val);
  412. };
  413.  
  414. // derived by by Dan Pupius www.pupius.net
  415. BezierClass = function(st,ed,spd,p1,p2,p3,p4) {
  416. this.st = st;
  417. this.ed = ed;
  418. this.spd = spd;
  419.  
  420. this.p1 = p1||0;
  421. this.p2 = p2||1;
  422. this.p3 = p3||0;
  423. this.p4 = p4||1;
  424.  
  425. this.ts = (new Date()).getTime();
  426. this.df = this.ed-this.st;
  427. };
  428. BezierClass.prototype = {
  429. B2:function(t){ return 3*t*t*(1-t) },
  430. B3:function(t){ return 3*t*(1-t)*(1-t) },
  431. B4:function(t){ return (1-t)*(1-t)*(1-t) },
  432. getNow:function(){
  433. var nw = (new Date()).getTime();
  434. var pc = 1-((nw-this.ts)/this.spd);
  435. var bz = this.B2(pc) + this.B3(pc) + this.B4(pc);
  436. return (pc<0) ? this.ed : this.st+Math.round(this.df*bz);
  437. },
  438. update:function(ed,spd){
  439. this.st = this.getNow();
  440. this.ed = ed;
  441. this.spd = spd;
  442. this.ts = (new Date()).getTime();
  443. this.df = this.ed-this.st;
  444. return this;
  445. }
  446. };
  447.  
  448. if (this.ishwscroll) {
  449. // hw accelerated scroll
  450. this.doc.translate = {x:0,y:0,tx:"0px",ty:"0px"};
  451.  
  452. //this one can help to enable hw accel on ios6 http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/
  453. if (cap.hastranslate3d&&cap.isios) this.doc.css("-webkit-backface-visibility","hidden"); // prevent flickering http://stackoverflow.com/questions/3461441/
  454.  
  455. //derived from http://stackoverflow.com/questions/11236090/
  456. function getMatrixValues() {
  457. var tr = self.doc.css(cap.trstyle);
  458. if (tr&&(tr.substr(0,6)=="matrix")) {
  459. return tr.replace(/^.*\((.*)\)$/g, "$1").replace(/px/g,'').split(/, +/);
  460. }
  461. return false;
  462. }
  463.  
  464. this.getScrollTop = function(last) {
  465. if (!last) {
  466. var mtx = getMatrixValues();
  467. if (mtx) return (mtx.length==16) ? -mtx[13] : -mtx[5]; //matrix3d 16 on IE10
  468. if (self.timerscroll&&self.timerscroll.bz) return self.timerscroll.bz.getNow();
  469. }
  470. return self.doc.translate.y;
  471. };
  472.  
  473. this.getScrollLeft = function(last) {
  474. if (!last) {
  475. var mtx = getMatrixValues();
  476. if (mtx) return (mtx.length==16) ? -mtx[12] : -mtx[4]; //matrix3d 16 on IE10
  477. if (self.timerscroll&&self.timerscroll.bh) return self.timerscroll.bh.getNow();
  478. }
  479. return self.doc.translate.x;
  480. };
  481.  
  482. if (document.createEvent) {
  483. this.notifyScrollEvent = function(el) {
  484. var e = document.createEvent("UIEvents");
  485. e.initUIEvent("scroll", false, true, window, 1);
  486. el.dispatchEvent(e);
  487. };
  488. }
  489. else if (document.fireEvent) {
  490. this.notifyScrollEvent = function(el) {
  491. var e = document.createEventObject();
  492. el.fireEvent("onscroll");
  493. e.cancelBubble = true;
  494. };
  495. }
  496. else {
  497. this.notifyScrollEvent = function(el,add) {}; //NOPE
  498. }
  499.  
  500. if (cap.hastranslate3d&&self.opt.enabletranslate3d) {
  501. this.setScrollTop = function(val,silent) {
  502. self.doc.translate.y = val;
  503. self.doc.translate.ty = (val*-1)+"px";
  504. self.doc.css(cap.trstyle,"translate3d("+self.doc.translate.tx+","+self.doc.translate.ty+",0px)");
  505. if (!silent) self.notifyScrollEvent(self.win[0]);
  506. };
  507. this.setScrollLeft = function(val,silent) {
  508. self.doc.translate.x = val;
  509. self.doc.translate.tx = (val*-1)+"px";
  510. self.doc.css(cap.trstyle,"translate3d("+self.doc.translate.tx+","+self.doc.translate.ty+",0px)");
  511. if (!silent) self.notifyScrollEvent(self.win[0]);
  512. };
  513. } else {
  514. this.setScrollTop = function(val,silent) {
  515. self.doc.translate.y = val;
  516. self.doc.translate.ty = (val*-1)+"px";
  517. self.doc.css(cap.trstyle,"translate("+self.doc.translate.tx+","+self.doc.translate.ty+")");
  518. if (!silent) self.notifyScrollEvent(self.win[0]);
  519. };
  520. this.setScrollLeft = function(val,silent) {
  521. self.doc.translate.x = val;
  522. self.doc.translate.tx = (val*-1)+"px";
  523. self.doc.css(cap.trstyle,"translate("+self.doc.translate.tx+","+self.doc.translate.ty+")");
  524. if (!silent) self.notifyScrollEvent(self.win[0]);
  525. };
  526. }
  527. } else {
  528. // native scroll
  529. this.getScrollTop = function() {
  530. return self.docscroll.scrollTop();
  531. };
  532. this.setScrollTop = function(val) {
  533. return self.docscroll.scrollTop(val);
  534. };
  535. this.getScrollLeft = function() {
  536. return self.docscroll.scrollLeft();
  537. };
  538. this.setScrollLeft = function(val) {
  539. return self.docscroll.scrollLeft(val);
  540. };
  541. }
  542.  
  543. this.getTarget = function(e) {
  544. if (!e) return false;
  545. if (e.target) return e.target;
  546. if (e.srcElement) return e.srcElement;
  547. return false;
  548. };
  549.  
  550. this.hasParent = function(e,id) {
  551. if (!e) return false;
  552. var el = e.target||e.srcElement||e||false;
  553. while (el && el.id != id) {
  554. el = el.parentNode||false;
  555. }
  556. return (el!==false);
  557. };
  558.  
  559. //inspired by http://forum.jquery.com/topic/width-includes-border-width-when-set-to-thin-medium-thick-in-ie
  560. var _convertBorderWidth = {"thin":1,"medium":3,"thick":5};
  561. function getWidthToPixel(dom,prop,chkheight) {
  562. var wd = dom.css(prop);
  563. var px = parseFloat(wd);
  564. if (isNaN(px)) {
  565. px = _convertBorderWidth[wd]||0;
  566. var brd = (px==3) ? ((chkheight)?(self.win.outerHeight() - self.win.innerHeight()):(self.win.outerWidth() - self.win.innerWidth())) : 1; //DON'T TRUST CSS
  567. if (self.isie8&&px) px+=1;
  568. return (brd) ? px : 0;
  569. }
  570. return px;
  571. };
  572.  
  573. this.getOffset = function() {
  574. if (self.isfixed) return {top:parseFloat(self.win.css('top')),left:parseFloat(self.win.css('left'))};
  575. if (!self.viewport) return self.win.offset();
  576. var ww = self.win.offset();
  577. var vp = self.viewport.offset();
  578. return {top:ww.top-vp.top+self.viewport.scrollTop(),left:ww.left-vp.left+self.viewport.scrollLeft()};
  579. };
  580.  
  581. this.updateScrollBar = function(len) {
  582. if (self.ishwscroll) {
  583. self.rail.css({height:self.win.innerHeight()});
  584. if (self.railh) self.railh.css({width:self.win.innerWidth()});
  585. } else {
  586. var wpos = self.getOffset();
  587. var pos = {top:wpos.top,left:wpos.left};
  588. pos.top+= getWidthToPixel(self.win,'border-top-width',true);
  589. var brd = (self.win.outerWidth() - self.win.innerWidth())/2;
  590. pos.left+= (self.rail.align) ? self.win.outerWidth() - getWidthToPixel(self.win,'border-right-width') - self.rail.width : getWidthToPixel(self.win,'border-left-width');
  591.  
  592. var off = self.opt.railoffset;
  593. if (off) {
  594. if (off.top) pos.top+=off.top;
  595. if (self.rail.align&&off.left) pos.left+=off.left;
  596. }
  597.  
  598. if (!self.locked) self.rail.css({top:pos.top,left:pos.left,height:(len)?len.h:self.win.innerHeight()});
  599.  
  600. if (self.zoom) {
  601. self.zoom.css({top:pos.top+1,left:(self.rail.align==1) ? pos.left-20 : pos.left+self.rail.width+4});
  602. }
  603.  
  604. if (self.railh&&!self.locked) {
  605. var pos = {top:wpos.top,left:wpos.left};
  606. var y = (self.railh.align) ? pos.top + getWidthToPixel(self.win,'border-top-width',true) + self.win.innerHeight() - self.railh.height : pos.top + getWidthToPixel(self.win,'border-top-width',true);
  607. var x = pos.left + getWidthToPixel(self.win,'border-left-width');
  608. self.railh.css({top:y,left:x,width:self.railh.width});
  609. }
  610.  
  611.  
  612. }
  613. };
  614.  
  615. this.doRailClick = function(e,dbl,hr) {
  616.  
  617. var fn,pg,cur,pos;
  618.  
  619. if (self.rail.drag&&self.rail.drag.pt!=1) return;
  620. if (self.locked) return;
  621. if (self.rail.drag) return;
  622.  
  623. self.cancelScroll();
  624.  
  625. self.cancelEvent(e);
  626.  
  627. if (dbl) {
  628. fn = (hr) ? self.doScrollLeft : self.doScrollTop;
  629. cur = (hr) ? ((e.pageX - self.railh.offset().left - (self.cursorwidth/2)) * self.scrollratio.x) : ((e.pageY - self.rail.offset().top - (self.cursorheight/2)) * self.scrollratio.y);
  630. fn(cur);
  631. } else {
  632. fn = (hr) ? self.doScrollLeftBy : self.doScrollBy;
  633. cur = (hr) ? self.scroll.x : self.scroll.y;
  634. pos = (hr) ? e.pageX - self.railh.offset().left : e.pageY - self.rail.offset().top;
  635. pg = (hr) ? self.view.w : self.view.h;
  636. (cur>=pos) ? fn(pg) : fn(-pg);
  637. }
  638.  
  639. }
  640.  
  641. self.hasanimationframe = (setAnimationFrame);
  642. self.hascancelanimationframe = (clearAnimationFrame);
  643.  
  644. if (!self.hasanimationframe) {
  645. setAnimationFrame=function(fn){return setTimeout(fn,16)}; // 1000/60)};
  646. clearAnimationFrame=clearInterval;
  647. }
  648. else if (!self.hascancelanimationframe) clearAnimationFrame=function(){self.cancelAnimationFrame=true};
  649.  
  650. this.init = function() {
  651.  
  652. self.saved.css = [];
  653.  
  654. if (cap.isie7mobile) return true; // SORRY, DO NOT WORK!
  655.  
  656. if (cap.hasmstouch) self.css((self.ispage)?$("html"):self.win,{'-ms-touch-action':'none'});
  657.  
  658. /*
  659. self.ispage = true;
  660. self.haswrapper = true;
  661. // self.win = $(window);
  662. self.docscroll = $("body");
  663. // self.doc = $("body");
  664. */
  665.  
  666. if (!self.ispage || (!cap.cantouch && !cap.isieold && !cap.isie9mobile)) {
  667.  
  668. var cont = self.docscroll;
  669. if (self.ispage) cont = (self.haswrapper)?self.win:self.doc;
  670.  
  671. if (!cap.isie9mobile) self.css(cont,{'overflow-y':'hidden'});
  672.  
  673. if (self.ispage&&cap.isie7) {
  674. if (self.doc[0].nodeName=='BODY') self.css($("html"),{'overflow-y':'hidden'}); //IE7 double scrollbar issue
  675. else if (self.doc[0].nodeName=='HTML') self.css($("body"),{'overflow-y':'hidden'}); //IE7 double scrollbar issue
  676. }
  677.  
  678. if (cap.isios&&!self.ispage&&!self.haswrapper) self.css($("body"),{"-webkit-overflow-scrolling":"touch"}); //force hw acceleration
  679.  
  680. var cursor = $(document.createElement('div'));
  681. cursor.css({
  682. position:"relative",top:0,"float":"right",width:self.opt.cursorwidth,height:"0px",
  683. 'background-color':self.opt.cursorcolor,
  684. border:self.opt.cursorborder,
  685. 'background-clip':'padding-box',
  686. '-webkit-border-radius':self.opt.cursorborderradius,
  687. '-moz-border-radius':self.opt.cursorborderradius,
  688. 'border-radius':self.opt.cursorborderradius
  689. });
  690.  
  691. cursor.hborder = parseFloat(cursor.outerHeight() - cursor.innerHeight());
  692. self.cursor = cursor;
  693.  
  694. var rail = $(document.createElement('div'));
  695. rail.attr('id',self.id);
  696.  
  697. var v,a,kp = ["left","right"]; //"top","bottom"
  698. for(var n in kp) {
  699. a=kp[n];
  700. v = self.opt.railpadding[a];
  701. (v) ? rail.css("padding-"+a,v+"px") : self.opt.railpadding[a] = 0;
  702. }
  703.  
  704. rail.append(cursor);
  705.  
  706. rail.width = Math.max(parseFloat(self.opt.cursorwidth),cursor.outerWidth()) + self.opt.railpadding['left'] + self.opt.railpadding['right'];
  707. rail.css({width:rail.width+"px",'zIndex':(self.ispage)?self.opt.zindex:self.opt.zindex+2,"background":self.opt.background});
  708.  
  709. rail.visibility = true;
  710. rail.scrollable = true;
  711.  
  712. rail.align = (self.opt.railalign=="left") ? 0 : 1;
  713.  
  714. self.rail = rail;
  715.  
  716. self.rail.drag = false;
  717.  
  718. var zoom = false;
  719. if (self.opt.boxzoom&&!self.ispage&&!cap.isieold) {
  720. zoom = document.createElement('div');
  721. self.bind(zoom,"click",self.doZoom);
  722. self.zoom = $(zoom);
  723. self.zoom.css({"cursor":"pointer",'z-index':self.opt.zindex,'backgroundImage':'url('+scriptpath+'zoomico.png)','height':18,'width':18,'backgroundPosition':'0px 0px'});
  724. if (self.opt.dblclickzoom) self.bind(self.win,"dblclick",self.doZoom);
  725. if (cap.cantouch&&self.opt.gesturezoom) {
  726. self.ongesturezoom = function(e) {
  727. if (e.scale>1.5) self.doZoomIn(e);
  728. if (e.scale<0.8) self.doZoomOut(e);
  729. return self.cancelEvent(e);
  730. };
  731. self.bind(self.win,"gestureend",self.ongesturezoom);
  732. }
  733. }
  734.  
  735. // init HORIZ
  736.  
  737. self.railh = false;
  738.  
  739. if (self.opt.horizrailenabled) {
  740.  
  741. self.css(cont,{'overflow-x':'hidden'});
  742.  
  743. var cursor = $(document.createElement('div'));
  744. cursor.css({
  745. position:"relative",top:0,height:self.opt.cursorwidth,width:"0px",
  746. 'background-color':self.opt.cursorcolor,
  747. border:self.opt.cursorborder,
  748. 'background-clip':'padding-box',
  749. '-webkit-border-radius':self.opt.cursorborderradius,
  750. '-moz-border-radius':self.opt.cursorborderradius,
  751. 'border-radius':self.opt.cursorborderradius
  752. });
  753.  
  754. cursor.wborder = parseFloat(cursor.outerWidth() - cursor.innerWidth());
  755. self.cursorh = cursor;
  756.  
  757. var railh = $(document.createElement('div'));
  758. railh.attr('id',self.id+'-hr');
  759. railh.height = 1+Math.max(parseFloat(self.opt.cursorwidth),cursor.outerHeight());
  760. railh.css({height:railh.height+"px",'zIndex':(self.ispage)?self.opt.zindex:self.opt.zindex+2,"background":self.opt.background});
  761.  
  762. railh.append(cursor);
  763.  
  764. railh.visibility = true;
  765. railh.scrollable = true;
  766.  
  767. railh.align = (self.opt.railvalign=="top") ? 0 : 1;
  768.  
  769. self.railh = railh;
  770.  
  771. self.railh.drag = false;
  772.  
  773. }
  774.  
  775. //
  776.  
  777. if (self.ispage) {
  778. rail.css({position:"fixed",top:"0px",height:"100%"});
  779. (rail.align) ? rail.css({right:"0px"}) : rail.css({left:"0px"});
  780. self.body.append(rail);
  781. if (self.railh) {
  782. railh.css({position:"fixed",left:"0px",width:"100%"});
  783. (railh.align) ? railh.css({bottom:"0px"}) : railh.css({top:"0px"});
  784. self.body.append(railh);
  785. }
  786. } else {
  787. if (self.ishwscroll) {
  788. if (self.win.css('position')=='static') self.css(self.win,{'position':'relative'});
  789. var bd = (self.win[0].nodeName == 'HTML') ? self.body : self.win;
  790. if (self.zoom) {
  791. self.zoom.css({position:"absolute",top:1,right:0,"margin-right":rail.width+4});
  792. bd.append(self.zoom);
  793. }
  794. rail.css({position:"absolute",top:0});
  795. (rail.align) ? rail.css({right:0}) : rail.css({left:0});
  796. bd.append(rail);
  797. if (railh) {
  798. railh.css({position:"absolute",left:0,bottom:0});
  799. (railh.align) ? railh.css({bottom:0}) : railh.css({top:0});
  800. bd.append(railh);
  801. }
  802. } else {
  803. self.isfixed = (self.win.css("position")=="fixed");
  804. var rlpos = (self.isfixed) ? "fixed" : "absolute";
  805.  
  806. if (!self.isfixed) self.viewport = self.getViewport(self.win[0]);
  807. if (self.viewport) self.body = self.viewport;
  808.  
  809. rail.css({position:rlpos});
  810. if (self.zoom) self.zoom.css({position:rlpos});
  811. self.updateScrollBar();
  812. self.body.append(rail);
  813. if (self.zoom) self.body.append(self.zoom);
  814. if (self.railh) {
  815. railh.css({position:rlpos});
  816. self.body.append(railh);
  817. }
  818. }
  819.  
  820. if (cap.isios) self.css(self.win,{'-webkit-tap-highlight-color':'rgba(0,0,0,0)','-webkit-touch-callout':'none'}); // prevent grey layer on click
  821.  
  822. if (cap.isie&&self.opt.disableoutline) self.win.attr("hideFocus","true"); // IE, prevent dotted rectangle on focused div
  823. if (cap.iswebkit&&self.opt.disableoutline) self.win.css({"outline":"none"});
  824.  
  825. }
  826.  
  827. if (self.opt.autohidemode===false) {
  828. self.autohidedom = false;
  829. }
  830. else if (self.opt.autohidemode===true) {
  831. self.autohidedom = $().add(self.rail);
  832. if (self.railh) self.autohidedom=self.autohidedom.add(self.railh);
  833. }
  834. else if (self.opt.autohidemode=="scroll") {
  835. self.autohidedom = $().add(self.rail);
  836. if (self.railh) self.autohidedom=self.autohidedom.add(self.railh);
  837. }
  838. else if (self.opt.autohidemode=="cursor") {
  839. self.autohidedom = $().add(self.cursor);
  840. if (self.railh) self.autohidedom=self.autohidedom.add(self.railh.cursor);
  841. }
  842. else if (self.opt.autohidemode=="hidden") {
  843. self.autohidedom = false;
  844. self.hide();
  845. self.locked = false;
  846. }
  847.  
  848. if (cap.isie9mobile) {
  849.  
  850. self.scrollmom = new ScrollMomentumClass2D(self);
  851.  
  852. /*
  853. var trace = function(msg) {
  854. var db = $("#debug");
  855. if (isNaN(msg)&&(typeof msg != "string")) {
  856. var x = [];
  857. for(var a in msg) {
  858. x.push(a+":"+msg[a]);
  859. }
  860. msg ="{"+x.join(",")+"}";
  861. }
  862. if (db.children().length>0) {
  863. db.children().eq(0).before("<div>"+msg+"</div>");
  864. } else {
  865. db.append("<div>"+msg+"</div>");
  866. }
  867. }
  868. window.onerror = function(msg,url,ln) {
  869. trace("ERR: "+msg+" at "+ln);
  870. }
  871. */
  872.  
  873. self.onmangotouch = function(e) {
  874. var py = self.getScrollTop();
  875. var px = self.getScrollLeft();
  876.  
  877. if ((py == self.scrollmom.lastscrolly)&&(px == self.scrollmom.lastscrollx)) return true;
  878. // $("#debug").html('DRAG:'+py);
  879.  
  880. var dfy = py-self.mangotouch.sy;
  881. var dfx = px-self.mangotouch.sx;
  882. var df = Math.round(Math.sqrt(Math.pow(dfx,2)+Math.pow(dfy,2)));
  883. if (df==0) return;
  884.  
  885. var dry = (dfy<0)?-1:1;
  886. var drx = (dfx<0)?-1:1;
  887.  
  888. var tm = +new Date();
  889. if (self.mangotouch.lazy) clearTimeout(self.mangotouch.lazy);
  890.  
  891. if (((tm-self.mangotouch.tm)>80)||(self.mangotouch.dry!=dry)||(self.mangotouch.drx!=drx)) {
  892. // trace('RESET+'+(tm-self.mangotouch.tm));
  893. self.scrollmom.stop();
  894. self.scrollmom.reset(px,py);
  895. self.mangotouch.sy = py;
  896. self.mangotouch.ly = py;
  897. self.mangotouch.sx = px;
  898. self.mangotouch.lx = px;
  899. self.mangotouch.dry = dry;
  900. self.mangotouch.drx = drx;
  901. self.mangotouch.tm = tm;
  902. } else {
  903.  
  904. self.scrollmom.stop();
  905. self.scrollmom.update(self.mangotouch.sx-dfx,self.mangotouch.sy-dfy);
  906. var gap = tm - self.mangotouch.tm;
  907. self.mangotouch.tm = tm;
  908.  
  909. // trace('MOVE:'+df+" - "+gap);
  910.  
  911. var ds = Math.max(Math.abs(self.mangotouch.ly-py),Math.abs(self.mangotouch.lx-px));
  912. self.mangotouch.ly = py;
  913. self.mangotouch.lx = px;
  914.  
  915. if (ds>2) {
  916. self.mangotouch.lazy = setTimeout(function(){
  917. // trace('END:'+ds+'+'+gap);
  918. self.mangotouch.lazy = false;
  919. self.mangotouch.dry = 0;
  920. self.mangotouch.drx = 0;
  921. self.mangotouch.tm = 0;
  922. self.scrollmom.doMomentum(30);
  923. },100);
  924. }
  925. }
  926. }
  927.  
  928. var top = self.getScrollTop();
  929. var lef = self.getScrollLeft();
  930. self.mangotouch = {sy:top,ly:top,dry:0,sx:lef,lx:lef,drx:0,lazy:false,tm:0};
  931.  
  932. self.bind(self.docscroll,"scroll",self.onmangotouch);
  933.  
  934. } else {
  935.  
  936. if (cap.cantouch||self.istouchcapable||self.opt.touchbehavior||cap.hasmstouch) {
  937.  
  938. self.scrollmom = new ScrollMomentumClass2D(self);
  939.  
  940. self.ontouchstart = function(e) {
  941. if (e.pointerType&&e.pointerType!=2) return false;
  942.  
  943. if (!self.locked) {
  944.  
  945. if (cap.hasmstouch) {
  946. var tg = (e.target) ? e.target : false;
  947. while (tg) {
  948. var nc = $(tg).getNiceScroll();
  949. if ((nc.length>0)&&(nc[0].me == self.me)) break;
  950. if (nc.length>0) return false;
  951. if ((tg.nodeName=='DIV')&&(tg.id==self.id)) break;
  952. tg = (tg.parentNode) ? tg.parentNode : false;
  953. }
  954. }
  955.  
  956. self.cancelScroll();
  957.  
  958. var tg = self.getTarget(e);
  959.  
  960. if (tg) {
  961. var skp = (/INPUT/i.test(tg.nodeName))&&(/range/i.test(tg.type));
  962. if (skp) return self.stopPropagation(e);
  963. }
  964.  
  965. if (!("clientX" in e) && ("changedTouches" in e)) {
  966. e.clientX = e.changedTouches[0].clientX;
  967. e.clientY = e.changedTouches[0].clientY;
  968. }
  969.  
  970. if (self.forcescreen) {
  971. var le = e;
  972. var e = {"original":(e.original)?e.original:e};
  973. e.clientX = le.screenX;
  974. e.clientY = le.screenY;
  975. }
  976.  
  977. self.rail.drag = {x:e.clientX,y:e.clientY,sx:self.scroll.x,sy:self.scroll.y,st:self.getScrollTop(),sl:self.getScrollLeft(),pt:2};
  978.  
  979. if (self.opt.touchbehavior&&self.isiframe&&cap.isie) {
  980. var wp = self.win.position();
  981. self.rail.drag.x+=wp.left;
  982. self.rail.drag.y+=wp.top;
  983. }
  984.  
  985. self.hasmoving = false;
  986. self.lastmouseup = false;
  987. self.scrollmom.reset(e.clientX,e.clientY);
  988. if (!cap.cantouch&&!this.istouchcapable&&!cap.hasmstouch) {
  989.  
  990. var ip = (tg)?/INPUT|SELECT|TEXTAREA/i.test(tg.nodeName):false;
  991. if (!ip) {
  992. if (!self.ispage&&cap.hasmousecapture) tg.setCapture();
  993. return self.cancelEvent(e);
  994. }
  995. if (/SUBMIT|CANCEL|BUTTON/i.test($(tg).attr('type'))) {
  996. pc = {"tg":tg,"click":false};
  997. self.preventclick = pc;
  998. }
  999.  
  1000. }
  1001. }
  1002.  
  1003. };
  1004.  
  1005. self.ontouchend = function(e) {
  1006. if (e.pointerType&&e.pointerType!=2) return false;
  1007. if (self.rail.drag&&(self.rail.drag.pt==2)) {
  1008. self.scrollmom.doMomentum();
  1009. self.rail.drag = false;
  1010. if (self.hasmoving) {
  1011. self.hasmoving = false;
  1012. self.lastmouseup = true;
  1013. self.hideCursor();
  1014. if (cap.hasmousecapture) document.releaseCapture();
  1015. if (!cap.cantouch) return self.cancelEvent(e);
  1016. }
  1017. }
  1018.  
  1019. };
  1020.  
  1021. var moveneedoffset = (self.opt.touchbehavior&&self.isiframe&&!cap.hasmousecapture);
  1022.  
  1023. self.ontouchmove = function(e,byiframe) {
  1024.  
  1025. if (e.pointerType&&e.pointerType!=2) return false;
  1026.  
  1027. if (self.rail.drag&&(self.rail.drag.pt==2)) {
  1028. if (cap.cantouch&&(typeof e.original == "undefined")) return true; // prevent ios "ghost" events by clickable elements
  1029.  
  1030. self.hasmoving = true;
  1031.  
  1032. if (self.preventclick&&!self.preventclick.click) {
  1033. self.preventclick.click = self.preventclick.tg.onclick||false;
  1034. self.preventclick.tg.onclick = self.onpreventclick;
  1035. }
  1036.  
  1037. var ev = $.extend({"original":e},e);
  1038. e = ev;
  1039.  
  1040. if (("changedTouches" in e)) {
  1041. e.clientX = e.changedTouches[0].clientX;
  1042. e.clientY = e.changedTouches[0].clientY;
  1043. }
  1044.  
  1045. if (self.forcescreen) {
  1046. var le = e;
  1047. var e = {"original":(e.original)?e.original:e};
  1048. e.clientX = le.screenX;
  1049. e.clientY = le.screenY;
  1050. }
  1051.  
  1052. var ofx = ofy = 0;
  1053.  
  1054. if (moveneedoffset&&!byiframe) {
  1055. var wp = self.win.position();
  1056. ofx=-wp.left;
  1057. ofy=-wp.top;
  1058. }
  1059.  
  1060. var fy = e.clientY + ofy;
  1061. var my = (fy-self.rail.drag.y);
  1062.  
  1063. var ny = self.rail.drag.st-my;
  1064.  
  1065. if (self.ishwscroll&&self.opt.bouncescroll) {
  1066. if (ny<0) {
  1067. ny = Math.round(ny/2);
  1068. // fy = 0;
  1069. }
  1070. else if (ny>self.page.maxh) {
  1071. ny = self.page.maxh+Math.round((ny-self.page.maxh)/2);
  1072. // fy = 0;
  1073. }
  1074. } else {
  1075. if (ny<0) {ny=0;fy=0}
  1076. if (ny>self.page.maxh) {ny=self.page.maxh;fy=0}
  1077. }
  1078.  
  1079. var fx = e.clientX + ofx;
  1080.  
  1081. if (self.railh&&self.railh.scrollable) {
  1082.  
  1083. var mx = (fx-self.rail.drag.x);
  1084.  
  1085. var nx = self.rail.drag.sl-mx;
  1086.  
  1087. if (self.ishwscroll&&self.opt.bouncescroll) {
  1088. if (nx<0) {
  1089. nx = Math.round(nx/2);
  1090. // fx = 0;
  1091. }
  1092. else if (nx>self.page.maxw) {
  1093. nx = self.page.maxw+Math.round((nx-self.page.maxw)/2);
  1094. // fx = 0;
  1095. }
  1096. } else {
  1097. if (nx<0) {nx=0;fx=0}
  1098. if (nx>self.page.maxw) {nx=self.page.maxw;fx=0}
  1099. }
  1100.  
  1101. }
  1102.  
  1103. self.synched("touchmove",function(){
  1104. if (self.rail.drag&&(self.rail.drag.pt==2)) {
  1105. if (self.prepareTransition) self.prepareTransition(0);
  1106. if (self.rail.scrollable) self.setScrollTop(ny);
  1107. self.scrollmom.update(fx,fy);
  1108. if (self.railh&&self.railh.scrollable) {
  1109. self.setScrollLeft(nx);
  1110. self.showCursor(ny,nx);
  1111. } else {
  1112. self.showCursor(ny);
  1113. }
  1114. if (cap.isie10) document.selection.clear();
  1115. }
  1116. });
  1117.  
  1118. if (!cap.ischrome&&!self.istouchcapable) return self.cancelEvent(e); //chrome touch emulation doesn't like!
  1119. }
  1120.  
  1121. };
  1122.  
  1123. }
  1124.  
  1125. if (cap.cantouch||self.opt.touchbehavior) {
  1126.  
  1127. self.onpreventclick = function(e) {
  1128. if (self.preventclick) {
  1129. self.preventclick.tg.onclick = self.preventclick.click;
  1130. self.preventclick = false;
  1131. return self.cancelEvent(e);
  1132. }
  1133. }
  1134.  
  1135. self.onmousedown = self.ontouchstart;
  1136.  
  1137. self.onmouseup = self.ontouchend;
  1138.  
  1139. self.onclick = (cap.isios) ? false : function(e) {
  1140. if (self.lastmouseup) {
  1141. self.lastmouseup = false;
  1142. return self.cancelEvent(e);
  1143. } else {
  1144. return true;
  1145. }
  1146. };
  1147.  
  1148. self.onmousemove = self.ontouchmove;
  1149.  
  1150. if (cap.cursorgrabvalue) {
  1151. self.css((self.ispage)?self.doc:self.win,{'cursor':cap.cursorgrabvalue});
  1152. self.css(self.rail,{'cursor':cap.cursorgrabvalue});
  1153. }
  1154.  
  1155. } else {
  1156.  
  1157. self.onmousedown = function(e,hronly) {
  1158. if (self.rail.drag&&self.rail.drag.pt!=1) return;
  1159. if (self.locked) return self.cancelEvent(e);
  1160. self.cancelScroll();
  1161. self.rail.drag = {x:e.clientX,y:e.clientY,sx:self.scroll.x,sy:self.scroll.y,pt:1,hr:(!!hronly)};
  1162. var tg = self.getTarget(e);
  1163. if (!self.ispage&&cap.hasmousecapture) tg.setCapture();
  1164. if (self.isiframe&&!cap.hasmousecapture) {
  1165. self.saved["csspointerevents"] = self.doc.css("pointer-events");
  1166. self.css(self.doc,{"pointer-events":"none"});
  1167. }
  1168. return self.cancelEvent(e);
  1169. };
  1170. self.onmouseup = function(e) {
  1171. if (self.rail.drag) {
  1172. if (cap.hasmousecapture) document.releaseCapture();
  1173. if (self.isiframe&&!cap.hasmousecapture) self.doc.css("pointer-events",self.saved["csspointerevents"]);
  1174. if(self.rail.drag.pt!=1)return;
  1175. self.rail.drag = false;
  1176. //if (!self.rail.active) self.hideCursor();
  1177. return self.cancelEvent(e);
  1178. }
  1179. };
  1180. self.onmousemove = function(e) {
  1181.  
  1182. if (self.rail.drag) {
  1183. if(self.rail.drag.pt!=1)return;
  1184.  
  1185. if (cap.ischrome&&e.which==0) return self.onmouseup(e);
  1186.  
  1187. self.cursorfreezed = true;
  1188.  
  1189. if (self.rail.drag.hr) {
  1190. self.scroll.x = self.rail.drag.sx + (e.clientX-self.rail.drag.x);
  1191. if (self.scroll.x<0) self.scroll.x=0;
  1192. var mw = self.scrollvaluemaxw;
  1193. if (self.scroll.x>mw) self.scroll.x=mw;
  1194. } else {
  1195. self.scroll.y = self.rail.drag.sy + (e.clientY-self.rail.drag.y);
  1196. if (self.scroll.y<0) self.scroll.y=0;
  1197. var my = self.scrollvaluemax;
  1198. if (self.scroll.y>my) self.scroll.y=my;
  1199. }
  1200.  
  1201. self.synched('mousemove',function(){
  1202. if (self.rail.drag&&(self.rail.drag.pt==1)) {
  1203. self.showCursor();
  1204. if (self.rail.drag.hr) self.doScrollLeft(Math.round(self.scroll.x*self.scrollratio.x));
  1205. else self.doScrollTop(Math.round(self.scroll.y*self.scrollratio.y));
  1206. }
  1207. });
  1208. return self.cancelEvent(e);
  1209. } else {
  1210. self.checkarea = true;
  1211. }
  1212.  
  1213. };
  1214. }
  1215.  
  1216. if (cap.cantouch||self.opt.touchbehavior) {
  1217. self.bind(self.win,"mousedown",self.onmousedown);
  1218. }
  1219.  
  1220. if (cap.hasmstouch) {
  1221. self.css(self.rail,{'-ms-touch-action':'none'});
  1222. self.css(self.cursor,{'-ms-touch-action':'none'});
  1223.  
  1224. self.bind(self.win,"MSPointerDown",self.ontouchstart);
  1225. self.bind(document,"MSPointerUp",self.ontouchend);
  1226. self.bind(document,"MSPointerMove",self.ontouchmove);
  1227. self.bind(self.cursor,"MSGestureHold",function(e){e.preventDefault();});
  1228. self.bind(self.cursor,"contextmenu",function(e){e.preventDefault();});
  1229. }
  1230.  
  1231. if (this.istouchcapable) { //device with screen touch enabled
  1232. self.bind(self.win,"touchstart",self.ontouchstart);
  1233. self.bind(document,"touchend",self.ontouchend);
  1234. self.bind(document,"touchcancel",self.ontouchend);
  1235. self.bind(document,"touchmove",self.ontouchmove);
  1236. }
  1237.  
  1238. self.bind(self.cursor,"mousedown",self.onmousedown);
  1239. self.bind(self.cursor,"mouseup",self.onmouseup);
  1240.  
  1241. if (self.railh) {
  1242. self.bind(self.cursorh,"mousedown",function(e){self.onmousedown(e,true)});
  1243. self.bind(self.cursorh,"mouseup",function(e){
  1244. if (self.rail.drag&&self.rail.drag.pt==2) return;
  1245. self.rail.drag = false;
  1246. self.hasmoving = false;
  1247. self.hideCursor();
  1248. if (cap.hasmousecapture) document.releaseCapture();
  1249. return self.cancelEvent(e);
  1250. });
  1251. }
  1252.  
  1253. self.bind(document,"mouseup",self.onmouseup);
  1254. if (cap.hasmousecapture) self.bind(self.win,"mouseup",self.onmouseup);
  1255.  
  1256. self.bind(document,"mousemove",self.onmousemove);
  1257. if (self.onclick) self.bind(document,"click",self.onclick);
  1258.  
  1259. if (!cap.cantouch&&!self.opt.touchbehavior) {
  1260.  
  1261. self.rail.mouseenter(function() {
  1262. if (self.canshowonmouseevent) self.showCursor();
  1263. self.rail.active = true;
  1264. });
  1265. self.rail.mouseleave(function() {
  1266. self.rail.active = false;
  1267. if (!self.rail.drag) self.hideCursor();
  1268. });
  1269.  
  1270. if (self.opt.sensitiverail) {
  1271. self.rail.click(function(e){self.doRailClick(e,false,false)});
  1272. self.rail.dblclick(function(e){self.doRailClick(e,true,false)});
  1273. self.cursor.click(function(e){self.cancelEvent(e)});
  1274. self.cursor.dblclick(function(e){self.cancelEvent(e)});
  1275. }
  1276.  
  1277. if (self.railh) {
  1278. self.railh.mouseenter(function() {
  1279. if (self.canshowonmouseevent) self.showCursor();
  1280. self.rail.active = true;
  1281. });
  1282. self.railh.mouseleave(function() {
  1283. self.rail.active = false;
  1284. if (!self.rail.drag) self.hideCursor();
  1285. });
  1286. }
  1287.  
  1288. if (self.zoom) {
  1289. self.zoom.mouseenter(function() {
  1290. if (self.canshowonmouseevent) self.showCursor();
  1291. self.rail.active = true;
  1292. });
  1293. self.zoom.mouseleave(function() {
  1294. self.rail.active = false;
  1295. if (!self.rail.drag) self.hideCursor();
  1296. });
  1297. }
  1298.  
  1299. }
  1300.  
  1301. if (self.opt.enablemousewheel) {
  1302. if (!self.isiframe) self.bind((cap.isie&&self.ispage) ? document : self.docscroll,"mousewheel",self.onmousewheel);
  1303. self.bind(self.rail,"mousewheel",self.onmousewheel);
  1304. if (self.railh) self.bind(self.railh,"mousewheel",self.onmousewheelhr);
  1305. }
  1306.  
  1307. if (!self.ispage&&!cap.cantouch&&!(/HTML|BODY/.test(self.win[0].nodeName))) {
  1308. if (!self.win.attr("tabindex")) self.win.attr({"tabindex":tabindexcounter++});
  1309.  
  1310. self.win.focus(function(e) {
  1311. domfocus = (self.getTarget(e)).id||true;
  1312. self.hasfocus = true;
  1313. if (self.canshowonmouseevent) self.noticeCursor();
  1314. });
  1315. self.win.blur(function(e) {
  1316. domfocus = false;
  1317. self.hasfocus = false;
  1318. });
  1319.  
  1320. self.win.mouseenter(function(e) {
  1321. mousefocus = (self.getTarget(e)).id||true;
  1322. self.hasmousefocus = true;
  1323. if (self.canshowonmouseevent) self.noticeCursor();
  1324. });
  1325. self.win.mouseleave(function() {
  1326. mousefocus = false;
  1327. self.hasmousefocus = false;
  1328. });
  1329.  
  1330. };
  1331.  
  1332. } // !ie9mobile
  1333.  
  1334. //Thanks to http://www.quirksmode.org !!
  1335. self.onkeypress = function(e) {
  1336. if (self.locked&&self.page.maxh==0) return true;
  1337.  
  1338. e = (e) ? e : window.e;
  1339. var tg = self.getTarget(e);
  1340. if (tg&&/INPUT|TEXTAREA|SELECT|OPTION/.test(tg.nodeName)) {
  1341. var tp = tg.getAttribute('type')||tg.type||false;
  1342. if ((!tp)||!(/submit|button|cancel/i.tp)) return true;
  1343. }
  1344.  
  1345. if (self.hasfocus||(self.hasmousefocus&&!domfocus)||(self.ispage&&!domfocus&&!mousefocus)) {
  1346. var key = e.keyCode;
  1347. var ctrl = e.ctrlKey||false;
  1348.  
  1349. if (self.locked&&key!=27) return self.cancelEvent(e);
  1350.  
  1351. var ret = false;
  1352. switch (key) {
  1353. case 38:
  1354. case 63233: //safari
  1355. self.doScrollBy(24*3);
  1356. ret = true;
  1357. break;
  1358. case 40:
  1359. case 63235: //safari
  1360. self.doScrollBy(-24*3);
  1361. ret = true;
  1362. break;
  1363. case 37:
  1364. case 63232: //safari
  1365. if (self.railh) {
  1366. (ctrl) ? self.doScrollLeft(0) : self.doScrollLeftBy(24*3);
  1367. ret = true;
  1368. }
  1369. break;
  1370. case 39:
  1371. case 63234: //safari
  1372. if (self.railh) {
  1373. (ctrl) ? self.doScrollLeft(self.page.maxw) : self.doScrollLeftBy(-24*3);
  1374. ret = true;
  1375. }
  1376. break;
  1377. case 33:
  1378. case 63276: // safari
  1379. self.doScrollBy(self.view.h);
  1380. ret = true;
  1381. break;
  1382. case 34:
  1383. case 63277: // safari
  1384. self.doScrollBy(-self.view.h);
  1385. ret = true;
  1386. break;
  1387. case 36:
  1388. case 63273: // safari
  1389. (self.railh&&ctrl) ? self.doScrollPos(0,0) : self.doScrollTo(0);
  1390. ret = true;
  1391. break;
  1392. case 35:
  1393. case 63275: // safari
  1394. (self.railh&&ctrl) ? self.doScrollPos(self.page.maxw,self.page.maxh) : self.doScrollTo(self.page.maxh);
  1395. ret = true;
  1396. break;
  1397. case 32:
  1398. if (self.opt.spacebarenabled) {
  1399. self.doScrollBy(-self.view.h);
  1400. ret = true;
  1401. }
  1402. break;
  1403. case 27: // ESC
  1404. if (self.zoomactive) {
  1405. self.doZoom();
  1406. ret = true;
  1407. }
  1408. break;
  1409. }
  1410. if (ret) return self.cancelEvent(e);
  1411. }
  1412. };
  1413.  
  1414. if (self.opt.enablekeyboard) self.bind(document,(cap.isopera&&!cap.isopera12)?"keypress":"keydown",self.onkeypress);
  1415.  
  1416. self.bind(window,'resize',self.resize);
  1417. self.bind(window,'orientationchange',self.resize);
  1418.  
  1419. self.bind(window,"load",self.resize);
  1420.  
  1421. if (cap.ischrome&&!self.ispage&&!self.haswrapper) { //chrome void scrollbar bug
  1422. var tmp=self.win.attr("style");
  1423. var ww = parseFloat(self.win.css("width"))+1;
  1424. self.win.css('width',ww);
  1425. self.synched("chromefix",function(){self.win.attr("style",tmp)});
  1426. }
  1427.  
  1428. // Trying a cross-browser implementation - good luck!
  1429.  
  1430. self.onAttributeChange = function(e) {
  1431. self.lazyResize();
  1432. }
  1433.  
  1434. if (!self.ispage&&!self.haswrapper) {
  1435. // thanks to Filip http://stackoverflow.com/questions/1882224/
  1436. if ("WebKitMutationObserver" in window) {
  1437. self.observer = new WebKitMutationObserver(function(mutations) {
  1438. mutations.forEach(self.onAttributeChange);
  1439. });
  1440. self.observer.observe(self.win[0],{attributes:true,subtree:false});
  1441. } else {
  1442. self.bind(self.win,(cap.isie&&!cap.isie9)?"propertychange":"DOMAttrModified",self.onAttributeChange);
  1443. if (cap.isie9) self.win[0].attachEvent("onpropertychange",self.onAttributeChange); //IE9 DOMAttrModified bug
  1444. }
  1445. }
  1446.  
  1447. //
  1448.  
  1449. if (!self.ispage&&self.opt.boxzoom) self.bind(window,"resize",self.resizeZoom);
  1450. if (self.istextarea) self.bind(self.win,"mouseup",self.resize);
  1451.  
  1452. self.resize();
  1453.  
  1454. }
  1455.  
  1456. if (this.doc[0].nodeName == 'IFRAME') {
  1457. function oniframeload(e) {
  1458. self.iframexd = false;
  1459. try {
  1460. var doc = 'contentDocument' in this ? this.contentDocument : this.contentWindow.document;
  1461. var a = doc.domain;
  1462. } catch(e){self.iframexd = true;doc=false};
  1463.  
  1464. if (self.iframexd) {
  1465. if ("console" in window) console.log('NiceScroll error: policy restriced iframe');
  1466. return true; //cross-domain - I can't manage this
  1467. }
  1468.  
  1469. self.forcescreen = true;
  1470.  
  1471. if (self.isiframe) {
  1472. self.iframe = {
  1473. "doc":$(doc),
  1474. "html":self.doc.contents().find('html')[0],
  1475. "body":self.doc.contents().find('body')[0]
  1476. };
  1477. self.getContentSize = function(){
  1478. return {
  1479. w:Math.max(self.iframe.html.scrollWidth,self.iframe.body.scrollWidth),
  1480. h:Math.max(self.iframe.html.scrollHeight,self.iframe.body.scrollHeight)
  1481. }
  1482. }
  1483. self.docscroll = $(self.iframe.body);//$(this.contentWindow);
  1484. }
  1485.  
  1486. if (!cap.isios&&self.opt.iframeautoresize&&!self.isiframe) {
  1487. self.win.scrollTop(0); // reset position
  1488. self.doc.height(""); //reset height to fix browser bug
  1489. var hh=Math.max(doc.getElementsByTagName('html')[0].scrollHeight,doc.body.scrollHeight);
  1490. self.doc.height(hh);
  1491. }
  1492. self.resize();
  1493.  
  1494. if (cap.isie7) self.css($(self.iframe.html),{'overflow-y':'hidden'});
  1495. //self.css($(doc.body),{'overflow-y':'hidden'});
  1496. self.css($(self.iframe.body),{'overflow-y':'hidden'});
  1497.  
  1498. if ('contentWindow' in this) {
  1499. self.bind(this.contentWindow,"scroll",self.onscroll); //IE8 & minor
  1500. } else {
  1501. self.bind(doc,"scroll",self.onscroll);
  1502. }
  1503.  
  1504. if (self.opt.enablemousewheel) {
  1505. self.bind(doc,"mousewheel",self.onmousewheel);
  1506. }
  1507.  
  1508. if (self.opt.enablekeyboard) self.bind(doc,(cap.isopera)?"keypress":"keydown",self.onkeypress);
  1509.  
  1510. if (cap.cantouch||self.opt.touchbehavior) {
  1511. self.bind(doc,"mousedown",self.onmousedown);
  1512. self.bind(doc,"mousemove",function(e){self.onmousemove(e,true)});
  1513. if (cap.cursorgrabvalue) self.css($(doc.body),{'cursor':cap.cursorgrabvalue});
  1514. }
  1515.  
  1516. self.bind(doc,"mouseup",self.onmouseup);
  1517.  
  1518. if (self.zoom) {
  1519. if (self.opt.dblclickzoom) self.bind(doc,'dblclick',self.doZoom);
  1520. if (self.ongesturezoom) self.bind(doc,"gestureend",self.ongesturezoom);
  1521. }
  1522. };
  1523.  
  1524. if (this.doc[0].readyState&&this.doc[0].readyState=="complete"){
  1525. setTimeout(function(){oniframeload.call(self.doc[0],false)},500);
  1526. }
  1527. self.bind(this.doc,"load",oniframeload);
  1528.  
  1529. }
  1530.  
  1531. };
  1532.  
  1533. this.showCursor = function(py,px) {
  1534. if (self.cursortimeout) {
  1535. clearTimeout(self.cursortimeout);
  1536. self.cursortimeout = 0;
  1537. }
  1538. if (!self.rail) return;
  1539. if (self.autohidedom) {
  1540. self.autohidedom.stop().css({opacity:self.opt.cursoropacitymax});
  1541. self.cursoractive = true;
  1542. }
  1543.  
  1544. if ((typeof py != "undefined")&&(py!==false)) {
  1545. self.scroll.y = Math.round(py * 1/self.scrollratio.y);
  1546. }
  1547. if (typeof px != "undefined") {
  1548. self.scroll.x = Math.round(px * 1/self.scrollratio.x);
  1549. }
  1550.  
  1551. self.cursor.css({height:self.cursorheight,top:self.scroll.y});
  1552. if (self.cursorh) {
  1553. (!self.rail.align&&self.rail.visibility) ? self.cursorh.css({width:self.cursorwidth,left:self.scroll.x+self.rail.width}) : self.cursorh.css({width:self.cursorwidth,left:self.scroll.x});
  1554. self.cursoractive = true;
  1555. }
  1556.  
  1557. if (self.zoom) self.zoom.stop().css({opacity:self.opt.cursoropacitymax});
  1558. };
  1559.  
  1560. this.hideCursor = function(tm) {
  1561. if (self.cursortimeout) return;
  1562. if (!self.rail) return;
  1563. if (!self.autohidedom) return;
  1564. self.cursortimeout = setTimeout(function() {
  1565. if (!self.rail.active||!self.showonmouseevent) {
  1566. self.autohidedom.stop().animate({opacity:self.opt.cursoropacitymin});
  1567. if (self.zoom) self.zoom.stop().animate({opacity:self.opt.cursoropacitymin});
  1568. self.cursoractive = false;
  1569. }
  1570. self.cursortimeout = 0;
  1571. },tm||400);
  1572. };
  1573.  
  1574. this.noticeCursor = function(tm,py,px) {
  1575. self.showCursor(py,px);
  1576. if (!self.rail.active) self.hideCursor(tm);
  1577. };
  1578.  
  1579. this.getContentSize =
  1580. (self.ispage) ?
  1581. function(){
  1582. return {
  1583. w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),
  1584. h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)
  1585. }
  1586. }
  1587. : (self.haswrapper) ?
  1588. function(){
  1589. return {
  1590. w:self.doc.outerWidth()+parseInt(self.win.css('paddingLeft'))+parseInt(self.win.css('paddingRight')),
  1591. h:self.doc.outerHeight()+parseInt(self.win.css('paddingTop'))+parseInt(self.win.css('paddingBottom'))
  1592. }
  1593. }
  1594. : function() {
  1595. return {
  1596. w:self.docscroll[0].scrollWidth,
  1597. h:self.docscroll[0].scrollHeight
  1598. }
  1599. };
  1600.  
  1601. this.onResize = function(e,page) {
  1602.  
  1603. if (!self.win) return false;
  1604.  
  1605. if (!self.haswrapper&&!self.ispage) {
  1606. if (self.win.css('display')=='none') {
  1607. if (self.visibility) self.hideRail().hideRailHr();
  1608. return false;
  1609. } else {
  1610. if (!self.hidden&&!self.visibility) self.showRail().showRailHr();
  1611. }
  1612. }
  1613.  
  1614. var premaxh = self.page.maxh;
  1615. var premaxw = self.page.maxw;
  1616.  
  1617. var preview = {h:self.view.h,w:self.view.w};
  1618.  
  1619. self.view = {
  1620. w:(self.ispage) ? self.win.width() : parseInt(self.win[0].clientWidth),
  1621. h:(self.ispage) ? self.win.height() : parseInt(self.win[0].clientHeight)
  1622. };
  1623.  
  1624. self.page = (page) ? page : self.getContentSize();
  1625.  
  1626. self.page.maxh = Math.max(0,self.page.h - self.view.h);
  1627. self.page.maxw = Math.max(0,self.page.w - self.view.w);
  1628.  
  1629. if ((self.page.maxh==premaxh)&&(self.page.maxw==premaxw)&&(self.view.w==preview.w)) {
  1630. // test position
  1631. if (!self.ispage) {
  1632. var pos = self.win.offset();
  1633. if (self.lastposition) {
  1634. var lst = self.lastposition;
  1635. if ((lst.top==pos.top)&&(lst.left==pos.left)) return self; //nothing to do
  1636. }
  1637. self.lastposition = pos;
  1638. } else {
  1639. return self; //nothing to do
  1640. }
  1641. }
  1642.  
  1643. if (self.page.maxh==0) {
  1644. self.hideRail();
  1645. self.scrollvaluemax = 0;
  1646. self.scroll.y = 0;
  1647. self.scrollratio.y = 0;
  1648. self.cursorheight = 0;
  1649. self.setScrollTop(0);
  1650. self.rail.scrollable = false;
  1651. } else {
  1652. self.rail.scrollable = true;
  1653. }
  1654.  
  1655. if (self.page.maxw==0) {
  1656. self.hideRailHr();
  1657. self.scrollvaluemaxw = 0;
  1658. self.scroll.x = 0;
  1659. self.scrollratio.x = 0;
  1660. self.cursorwidth = 0;
  1661. self.setScrollLeft(0);
  1662. self.railh.scrollable = false;
  1663. } else {
  1664. self.railh.scrollable = true;
  1665. }
  1666.  
  1667. self.locked = (self.page.maxh==0)&&(self.page.maxw==0);
  1668. if (self.locked) {
  1669. if (!self.ispage) self.updateScrollBar(self.view);
  1670. return false;
  1671. }
  1672.  
  1673. if (!self.hidden&&!self.visibility) {
  1674. self.showRail().showRailHr();
  1675. }
  1676. else if (!self.hidden&&!self.railh.visibility) self.showRailHr();
  1677.  
  1678. if (self.istextarea&&self.win.css('resize')&&self.win.css('resize')!='none') self.view.h-=20;
  1679. if (!self.ispage) self.updateScrollBar(self.view);
  1680.  
  1681. self.cursorheight = Math.min(self.view.h,Math.round(self.view.h * (self.view.h / self.page.h)));
  1682. self.cursorheight = Math.max(self.opt.cursorminheight,self.cursorheight);
  1683.  
  1684. self.cursorwidth = Math.min(self.view.w,Math.round(self.view.w * (self.view.w / self.page.w)));
  1685. self.cursorwidth = Math.max(self.opt.cursorminheight,self.cursorwidth);
  1686.  
  1687. self.scrollvaluemax = self.view.h-self.cursorheight-self.cursor.hborder;
  1688.  
  1689. if (self.railh) {
  1690. self.railh.width = (self.page.maxh>0) ? (self.view.w-self.rail.width) : self.view.w;
  1691. self.scrollvaluemaxw = self.railh.width-self.cursorwidth-self.cursorh.wborder;
  1692. }
  1693.  
  1694. self.scrollratio = {
  1695. x:(self.page.maxw/self.scrollvaluemaxw),
  1696. y:(self.page.maxh/self.scrollvaluemax)
  1697. };
  1698.  
  1699. var sy = self.getScrollTop();
  1700. if (sy>self.page.maxh) {
  1701. self.doScroll(self.page.maxh);
  1702. } else {
  1703. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1704. self.scroll.x = Math.round(self.getScrollLeft() * (1/self.scrollratio.x));
  1705. if (self.cursoractive) self.noticeCursor();
  1706. }
  1707.  
  1708. if (self.scroll.y&&(self.getScrollTop()==0)) self.doScrollTo(Math.floor(self.scroll.y*self.scrollratio.y));
  1709.  
  1710. return self;
  1711. };
  1712.  
  1713. this.resize = function(){self.delayed('resize',self.onResize,30);return self;} // event debounce
  1714.  
  1715. this.lazyResize = function() {
  1716. self.delayed('resize',self.resize,250);
  1717. }
  1718.  
  1719. this._bind = function(el,name,fn,bubble) { // primitive bind
  1720. self.events.push({e:el,n:name,f:fn,b:bubble});
  1721. if (el.addEventListener) {
  1722. el.addEventListener(name,fn,bubble||false);
  1723. }
  1724. else if (el.attachEvent) {
  1725. el.attachEvent("on"+name,fn);
  1726. }
  1727. else {
  1728. el["on"+name] = fn;
  1729. }
  1730. };
  1731.  
  1732. this.bind = function(dom,name,fn,bubble) { // touch-oriented & fixing jquery bind
  1733. var el = ("jquery" in dom) ? dom[0] : dom;
  1734. if (el.addEventListener) {
  1735. if (cap.cantouch && /mouseup|mousedown|mousemove/.test(name)) { // touch device support
  1736. var tt=(name=='mousedown')?'touchstart':(name=='mouseup')?'touchend':'touchmove';
  1737. self._bind(el,tt,function(e){
  1738. if (e.touches) {
  1739. if (e.touches.length<2) {var ev=(e.touches.length)?e.touches[0]:e;ev.original=e;fn.call(this,ev);}
  1740. }
  1741. else if (e.changedTouches) {var ev=e.changedTouches[0];ev.original=e;fn.call(this,ev);} //blackberry
  1742. },bubble||false);
  1743. }
  1744. self._bind(el,name,fn,bubble||false);
  1745. if (name=='mousewheel') self._bind(el,"DOMMouseScroll",fn,bubble||false);
  1746. if (cap.cantouch && name=="mouseup") self._bind(el,"touchcancel",fn,bubble||false);
  1747. }
  1748. else {
  1749. self._bind(el,name,function(e) {
  1750. e = e||window.event||false;
  1751. if (e) {
  1752. if (e.srcElement) e.target=e.srcElement;
  1753. }
  1754. return ((fn.call(el,e)===false)||bubble===false) ? self.cancelEvent(e) : true;
  1755. });
  1756. }
  1757. };
  1758.  
  1759. this._unbind = function(el,name,fn,bub) { // primitive unbind
  1760. if (el.removeEventListener) {
  1761. el.removeEventListener(name,fn,bub);
  1762. }
  1763. else if (el.detachEvent) {
  1764. el.detachEvent('on'+name,fn);
  1765. } else {
  1766. el['on'+name] = false;
  1767. }
  1768. };
  1769.  
  1770. this.unbindAll = function() {
  1771. for(var a=0;a<self.events.length;a++) {
  1772. var r = self.events[a];
  1773. self._unbind(r.e,r.n,r.f,r.b);
  1774. }
  1775. };
  1776.  
  1777. // Thanks to http://www.switchonthecode.com !!
  1778. this.cancelEvent = function(e) {
  1779. var e = (e.original) ? e.original : (e) ? e : window.event||false;
  1780. if (!e) return false;
  1781. if(e.preventDefault) e.preventDefault();
  1782. if(e.stopPropagation) e.stopPropagation();
  1783. if(e.preventManipulation) e.preventManipulation(); //IE10
  1784. e.cancelBubble = true;
  1785. e.cancel = true;
  1786. e.returnValue = false;
  1787. return false;
  1788. };
  1789.  
  1790. this.stopPropagation = function(e) {
  1791. var e = (e.original) ? e.original : (e) ? e : window.event||false;
  1792. if (!e) return false;
  1793. if (e.stopPropagation) return e.stopPropagation();
  1794. if (e.cancelBubble) e.cancelBubble=true;
  1795. return false;
  1796. }
  1797.  
  1798. this.showRail = function() {
  1799. if ((self.page.maxh!=0)&&(self.ispage||self.win.css('display')!='none')) {
  1800. self.visibility = true;
  1801. self.rail.visibility = true;
  1802. self.rail.css('display','block');
  1803. }
  1804. return self;
  1805. };
  1806.  
  1807. this.showRailHr = function() {
  1808. if (!self.railh) return self;
  1809. if ((self.page.maxw!=0)&&(self.ispage||self.win.css('display')!='none')) {
  1810. self.railh.visibility = true;
  1811. self.railh.css('display','block');
  1812. }
  1813. return self;
  1814. };
  1815.  
  1816. this.hideRail = function() {
  1817. self.visibility = false;
  1818. self.rail.visibility = false;
  1819. self.rail.css('display','none');
  1820. return self;
  1821. };
  1822.  
  1823. this.hideRailHr = function() {
  1824. if (!self.railh) return self;
  1825. self.railh.visibility = false;
  1826. self.railh.css('display','none');
  1827. return self;
  1828. };
  1829.  
  1830. this.show = function() {
  1831. self.hidden = false;
  1832. self.locked = false;
  1833. return self.showRail().showRailHr();
  1834. };
  1835.  
  1836. this.hide = function() {
  1837. self.hidden = true;
  1838. self.locked = true;
  1839. return self.hideRail().hideRailHr();
  1840. };
  1841.  
  1842. this.toggle = function() {
  1843. return (self.hidden) ? self.show() : self.hide();
  1844. };
  1845.  
  1846. this.remove = function() {
  1847. self.doZoomOut();
  1848. self.unbindAll();
  1849. if (self.observer !== false) self.observer.disconnect();
  1850. self.events = [];
  1851. if (self.cursor) {
  1852. self.cursor.remove();
  1853. self.cursor = null;
  1854. }
  1855. if (self.cursorh) {
  1856. self.cursorh.remove();
  1857. self.cursorh = null;
  1858. }
  1859. if (self.rail) {
  1860. self.rail.remove();
  1861. self.rail = null;
  1862. }
  1863. if (self.railh) {
  1864. self.railh.remove();
  1865. self.railh = null;
  1866. }
  1867. if (self.zoom) {
  1868. self.zoom.remove();
  1869. self.zoom = null;
  1870. }
  1871. for(var a=0;a<self.saved.css.length;a++) {
  1872. var d=self.saved.css[a];
  1873. d[0].css(d[1],(typeof d[2]=="undefined") ? '' : d[2]);
  1874. }
  1875. self.saved = false;
  1876. self.me.data('__nicescroll',''); //erase all traces
  1877. self.me = null;
  1878. self.doc = null;
  1879. self.docscroll = null;
  1880. self.win = null;
  1881. return self;
  1882. };
  1883.  
  1884. this.scrollstart = function(fn) {
  1885. this.onscrollstart = fn;
  1886. return self;
  1887. }
  1888. this.scrollend = function(fn) {
  1889. this.onscrollend = fn;
  1890. return self;
  1891. }
  1892. this.scrollcancel = function(fn) {
  1893. this.onscrollcancel = fn;
  1894. return self;
  1895. }
  1896.  
  1897. this.zoomin = function(fn) {
  1898. this.onzoomin = fn;
  1899. return self;
  1900. }
  1901. this.zoomout = function(fn) {
  1902. this.onzoomout = fn;
  1903. return self;
  1904. }
  1905.  
  1906. this.isScrollable = function(e) {
  1907. var dom = (e.target) ? e.target : e;
  1908. while (dom&&(dom.nodeType==1)&&!(/BODY|HTML/.test(dom.nodeName))) {
  1909. var dd = $(dom);
  1910. var ov = dd.css('overflowY')||dd.css('overflowX')||dd.css('overflow')||'';
  1911. if (/scroll|auto/.test(ov)) return (dom.clientHeight!=dom.scrollHeight);
  1912. dom = (dom.parentNode) ? dom.parentNode : false;
  1913. }
  1914. return false;
  1915. };
  1916.  
  1917. this.getViewport = function(me) {
  1918. var dom = (me&&me.parentNode) ? me.parentNode : false;
  1919. while (dom&&(dom.nodeType==1)&&!(/BODY|HTML/.test(dom.nodeName))) {
  1920. var dd = $(dom);
  1921. var ov = dd.css('overflowY')||dd.css('overflowX')||dd.css('overflow')||'';
  1922. if ((/scroll|auto/.test(ov))&&(dom.clientHeight!=dom.scrollHeight)) return dd;
  1923. if (dd.getNiceScroll().length>0) return dd;
  1924. dom = (dom.parentNode) ? dom.parentNode : false;
  1925. }
  1926. return false;
  1927. };
  1928.  
  1929. function execScrollWheel(e,hr) {
  1930. var px = 0;
  1931. var py = 0;
  1932. var rt = 1;
  1933. if ("wheelDeltaY" in e) {
  1934. rt = self.opt.mousescrollstep/(16*3);
  1935. px = Math.floor(e.wheelDeltaX*rt);
  1936. py = Math.floor(e.wheelDeltaY*rt);
  1937. } else {
  1938. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  1939. if (delta) {
  1940. (hr) ? px = Math.floor(delta*self.opt.mousescrollstep) : py = Math.floor(delta*self.opt.mousescrollstep);
  1941. }
  1942. }
  1943. if (px) {
  1944. if (self.scrollmom) {self.scrollmom.stop()}
  1945. self.lastdeltax+=px;
  1946. self.synched("mousewheelx",function(){var dt=self.lastdeltax;self.lastdeltax=0;if(!self.rail.drag){self.doScrollLeftBy(dt)}});
  1947. }
  1948. if (py) {
  1949. if (self.scrollmom) {self.scrollmom.stop()}
  1950. self.lastdeltay+=py;
  1951. self.synched("mousewheely",function(){var dt=self.lastdeltay;self.lastdeltay=0;if(!self.rail.drag){self.doScrollBy(dt)}});
  1952. }
  1953. };
  1954.  
  1955. this.onmousewheel = function(e) {
  1956. if (self.locked) return true;
  1957. if (!self.rail.scrollable) {
  1958. if (self.railh&&self.railh.scrollable) {
  1959. return self.onmousewheelhr(e);
  1960. } else {
  1961. return true;
  1962. }
  1963. }
  1964. if (self.opt.preservenativescrolling&&self.checkarea) {
  1965. self.checkarea = false;
  1966. self.nativescrollingarea = self.isScrollable(e);
  1967. }
  1968. if (self.nativescrollingarea) return true; // this isn't my business
  1969. if (self.locked) return self.cancelEvent(e);
  1970. if (self.rail.drag) return self.cancelEvent(e);
  1971.  
  1972. execScrollWheel(e,false);
  1973.  
  1974. return self.cancelEvent(e);
  1975. };
  1976.  
  1977. this.onmousewheelhr = function(e) {
  1978. if (self.locked||!self.railh.scrollable) return true;
  1979. if (self.opt.preservenativescrolling&&self.checkarea) {
  1980. self.checkarea = false;
  1981. self.nativescrollingarea = self.isScrollable(e);
  1982. }
  1983. if (self.nativescrollingarea) return true; // this isn't my business
  1984. if (self.locked) return self.cancelEvent(e);
  1985. if (self.rail.drag) return self.cancelEvent(e);
  1986.  
  1987. execScrollWheel(e,true);
  1988.  
  1989. return self.cancelEvent(e);
  1990. };
  1991.  
  1992. this.stop = function() {
  1993. self.cancelScroll();
  1994. if (self.scrollmon) self.scrollmon.stop();
  1995. self.cursorfreezed = false;
  1996. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  1997. self.noticeCursor();
  1998. return self;
  1999. };
  2000.  
  2001. this.getTransitionSpeed = function(dif) {
  2002. var sp = Math.round(self.opt.scrollspeed*10);
  2003. var ex = Math.min(sp,Math.round((dif / 20) * self.opt.scrollspeed));
  2004. return (ex>20) ? ex : 0;
  2005. }
  2006.  
  2007. if (!self.opt.smoothscroll) {
  2008. this.doScrollLeft = function(x,spd) { //direct
  2009. var y = self.getScrollTop();
  2010. self.doScrollPos(x,y,spd);
  2011. }
  2012. this.doScrollTop = function(y,spd) { //direct
  2013. var x = self.getScrollLeft();
  2014. self.doScrollPos(x,y,spd);
  2015. }
  2016. this.doScrollPos = function(x,y,spd) { //direct
  2017. var nx = (x>self.page.maxw) ? self.page.maxw : x;
  2018. if (nx<0) nx=0;
  2019. var ny = (y>self.page.maxh) ? self.page.maxh : y;
  2020. if (ny<0) ny=0;
  2021. self.synched('scroll',function(){
  2022. self.setScrollTop(ny);
  2023. self.setScrollLeft(nx);
  2024. });
  2025. }
  2026. this.cancelScroll = function() {}; // direct
  2027. }
  2028. else if (self.ishwscroll&&cap.hastransition&&self.opt.usetransition) {
  2029. this.prepareTransition = function(dif,istime) {
  2030. var ex = (istime) ? ((dif>20)?dif:0) : self.getTransitionSpeed(dif);
  2031. var trans = (ex) ? cap.prefixstyle+'transform '+ex+'ms ease-out' : '';
  2032. if (!self.lasttransitionstyle||self.lasttransitionstyle!=trans) {
  2033. self.lasttransitionstyle = trans;
  2034. self.doc.css(cap.transitionstyle,trans);
  2035. }
  2036. return ex;
  2037. };
  2038.  
  2039. this.doScrollLeft = function(x,spd) { //trans
  2040. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  2041. self.doScrollPos(x,y,spd);
  2042. }
  2043.  
  2044. this.doScrollTop = function(y,spd) { //trans
  2045. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  2046. self.doScrollPos(x,y,spd);
  2047. }
  2048.  
  2049. this.doScrollPos = function(x,y,spd) { //trans
  2050.  
  2051. var py = self.getScrollTop();
  2052. var px = self.getScrollLeft();
  2053.  
  2054. if (((self.newscrolly-py)*(y-py)<0)||((self.newscrollx-px)*(x-px)<0)) self.cancelScroll(); //inverted movement detection
  2055.  
  2056. if (self.opt.bouncescroll==false) {
  2057. if (y<0) y=0;
  2058. else if (y>self.page.maxh) y=self.page.maxh;
  2059. if (x<0) x=0;
  2060. else if (x>self.page.maxw) x=self.page.maxw;
  2061. }
  2062.  
  2063. if (x==self.newscrollx&&y==self.newscrolly) return false;
  2064.  
  2065. self.newscrolly = y;
  2066. self.newscrollx = x;
  2067.  
  2068. self.newscrollspeed = spd||false;
  2069.  
  2070. if (self.timer) return false;
  2071.  
  2072. self.timer = setTimeout(function(){
  2073.  
  2074. var top = self.getScrollTop();
  2075. var lft = self.getScrollLeft();
  2076.  
  2077. var dst = {};
  2078. dst.x = x-lft;
  2079. dst.y = y-top;
  2080. dst.px = lft;
  2081. dst.py = top;
  2082.  
  2083. var dd = Math.round(Math.sqrt(Math.pow(dst.x,2)+Math.pow(dst.y,2)));
  2084.  
  2085. var df = (self.newscrollspeed) ? self.newscrollspeed : dd;
  2086. var ms = self.prepareTransition(df);
  2087.  
  2088. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  2089.  
  2090. if (ms>0) {
  2091.  
  2092. if (!self.scrollrunning&&self.onscrollstart) {
  2093. var info = {"type":"scrollstart","current":{"x":lft,"y":top},"request":{"x":x,"y":y},"end":{"x":self.newscrollx,"y":self.newscrolly},"speed":ms};
  2094. self.onscrollstart.call(self,info);
  2095. }
  2096.  
  2097. if (cap.transitionend) {
  2098. if (!self.scrollendtrapped) {
  2099. self.scrollendtrapped = true;
  2100. self.bind(self.doc,cap.transitionend,self.onScrollEnd,false); //I have got to do something usefull!!
  2101. }
  2102. } else {
  2103. if (self.scrollendtrapped) clearTimeout(self.scrollendtrapped);
  2104. self.scrollendtrapped = setTimeout(self.onScrollEnd,ms); // simulate transitionend event
  2105. }
  2106.  
  2107. var py = top;
  2108. var px = lft;
  2109. self.timerscroll = {
  2110. bz: new BezierClass(py,self.newscrolly,ms,0,0,0.58,1),
  2111. bh: new BezierClass(px,self.newscrollx,ms,0,0,0.58,1)
  2112. };
  2113. if (!self.cursorfreezed) self.timerscroll.tm=setInterval(function(){self.showCursor(self.getScrollTop(),self.getScrollLeft())},60);
  2114.  
  2115. }
  2116.  
  2117. self.synched("doScroll-set",function(){
  2118. self.timer = 0;
  2119. if (self.scrollendtrapped) self.scrollrunning = true;
  2120. self.setScrollTop(self.newscrolly);
  2121. self.setScrollLeft(self.newscrollx);
  2122. if (!self.scrollendtrapped) self.onScrollEnd();
  2123. });
  2124.  
  2125.  
  2126. },50);
  2127.  
  2128. };
  2129.  
  2130. this.cancelScroll = function() {
  2131. if (!self.scrollendtrapped) return true;
  2132. var py = self.getScrollTop();
  2133. var px = self.getScrollLeft();
  2134. self.scrollrunning = false;
  2135. if (!cap.transitionend) clearTimeout(cap.transitionend);
  2136. self.scrollendtrapped = false;
  2137. self._unbind(self.doc,cap.transitionend,self.onScrollEnd);
  2138. self.prepareTransition(0);
  2139. self.setScrollTop(py); // fire event onscroll
  2140. if (self.railh) self.setScrollLeft(px);
  2141. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  2142. self.timerscroll = false;
  2143.  
  2144. self.cursorfreezed = false;
  2145.  
  2146. //self.noticeCursor(false,py,px);
  2147. self.showCursor(py,px);
  2148. return self;
  2149. };
  2150. this.onScrollEnd = function() {
  2151. if (self.scrollendtrapped) self._unbind(self.doc,cap.transitionend,self.onScrollEnd);
  2152. self.scrollendtrapped = false;
  2153. self.prepareTransition(0);
  2154. if (self.timerscroll&&self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  2155. self.timerscroll = false;
  2156. var py = self.getScrollTop();
  2157. var px = self.getScrollLeft();
  2158. self.setScrollTop(py); // fire event onscroll
  2159. if (self.railh) self.setScrollLeft(px); // fire event onscroll left
  2160.  
  2161. self.noticeCursor(false,py,px);
  2162.  
  2163. self.cursorfreezed = false;
  2164.  
  2165. if (py<0) py=0
  2166. else if (py>self.page.maxh) py=self.page.maxh;
  2167. if (px<0) px=0
  2168. else if (px>self.page.maxw) px=self.page.maxw;
  2169. if((py!=self.newscrolly)||(px!=self.newscrollx)) return self.doScrollPos(px,py,self.opt.snapbackspeed);
  2170.  
  2171. if (self.onscrollend&&self.scrollrunning) {
  2172. var info = {"type":"scrollend","current":{"x":px,"y":py},"end":{"x":self.newscrollx,"y":self.newscrolly}};
  2173. self.onscrollend.call(self,info);
  2174. }
  2175. self.scrollrunning = false;
  2176.  
  2177. };
  2178.  
  2179. } else {
  2180.  
  2181. this.doScrollLeft = function(x) { //no-trans
  2182. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  2183. self.doScrollPos(x,y);
  2184. }
  2185.  
  2186. this.doScrollTop = function(y) { //no-trans
  2187. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  2188. self.doScrollPos(x,y);
  2189. }
  2190.  
  2191. this.doScrollPos = function(x,y) { //no-trans
  2192. var y = ((typeof y == "undefined")||(y===false)) ? self.getScrollTop(true) : y;
  2193.  
  2194. if ((self.timer)&&(self.newscrolly==y)&&(self.newscrollx==x)) return true;
  2195.  
  2196. if (self.timer) clearAnimationFrame(self.timer);
  2197. self.timer = 0;
  2198.  
  2199. var py = self.getScrollTop();
  2200. var px = self.getScrollLeft();
  2201.  
  2202. if (((self.newscrolly-py)*(y-py)<0)||((self.newscrollx-px)*(x-px)<0)) self.cancelScroll(); //inverted movement detection
  2203.  
  2204. self.newscrolly = y;
  2205. self.newscrollx = x;
  2206.  
  2207. if (!self.bouncescroll||!self.rail.visibility) {
  2208. if (self.newscrolly<0) {
  2209. self.newscrolly = 0;
  2210. }
  2211. else if (self.newscrolly>self.page.maxh) {
  2212. self.newscrolly = self.page.maxh;
  2213. }
  2214. }
  2215. if (!self.bouncescroll||!self.railh.visibility) {
  2216. if (self.newscrollx<0) {
  2217. self.newscrollx = 0;
  2218. }
  2219. else if (self.newscrollx>self.page.maxw) {
  2220. self.newscrollx = self.page.maxw;
  2221. }
  2222. }
  2223.  
  2224. self.dst = {};
  2225. self.dst.x = x-px;
  2226. self.dst.y = y-py;
  2227. self.dst.px = px;
  2228. self.dst.py = py;
  2229.  
  2230. var dst = Math.round(Math.sqrt(Math.pow(self.dst.x,2)+Math.pow(self.dst.y,2)));
  2231.  
  2232. self.dst.ax = self.dst.x / dst;
  2233. self.dst.ay = self.dst.y / dst;
  2234.  
  2235. var pa = 0;
  2236. var pe = dst;
  2237.  
  2238. if (self.dst.x==0) {
  2239. pa = py;
  2240. pe = y;
  2241. self.dst.ay = 1;
  2242. self.dst.py = 0;
  2243. } else if (self.dst.y==0) {
  2244. pa = px;
  2245. pe = x;
  2246. self.dst.ax = 1;
  2247. self.dst.px = 0;
  2248. }
  2249.  
  2250. var ms = self.getTransitionSpeed(dst);
  2251. if (ms>0) {
  2252. self.bzscroll = (self.bzscroll) ? self.bzscroll.update(pe,ms) : new BezierClass(pa,pe,ms,0,1,0,1);
  2253. } else {
  2254. self.bzscroll = false;
  2255. }
  2256.  
  2257. if (self.timer) return;
  2258.  
  2259. if ((py==self.page.maxh&&y>=self.page.maxh)||(px==self.page.maxw&&x>=self.page.maxw)) self.checkContentSize();
  2260.  
  2261. var sync = 1;
  2262.  
  2263. function scrolling() {
  2264. if (self.cancelAnimationFrame) return true;
  2265.  
  2266. self.scrollrunning = true;
  2267.  
  2268. sync = 1-sync;
  2269. if (sync) return (self.timer = setAnimationFrame(scrolling)||1);
  2270.  
  2271. var done = 0;
  2272.  
  2273. var sc = sy = self.getScrollTop();
  2274. if (self.dst.ay) {
  2275. sc = (self.bzscroll) ? self.dst.py + (self.bzscroll.getNow()*self.dst.ay) : self.newscrolly;
  2276. var dr=sc-sy;
  2277. if ((dr<0&&sc<self.newscrolly)||(dr>0&&sc>self.newscrolly)) sc = self.newscrolly;
  2278. self.setScrollTop(sc);
  2279. if (sc == self.newscrolly) done=1;
  2280. } else {
  2281. done=1;
  2282. }
  2283.  
  2284. var scx = sx = self.getScrollLeft();
  2285. if (self.dst.ax) {
  2286. scx = (self.bzscroll) ? self.dst.px + (self.bzscroll.getNow()*self.dst.ax) : self.newscrollx;
  2287. var dr=scx-sx;
  2288. if ((dr<0&&scx<self.newscrollx)||(dr>0&&scx>self.newscrollx)) scx = self.newscrollx;
  2289. self.setScrollLeft(scx);
  2290. if (scx == self.newscrollx) done+=1;
  2291. } else {
  2292. done+=1;
  2293. }
  2294.  
  2295. if (done==2) {
  2296. self.timer = 0;
  2297. self.cursorfreezed = false;
  2298. self.bzscroll = false;
  2299. self.scrollrunning = false;
  2300. if (sc<0) sc=0;
  2301. else if (sc>self.page.maxh) sc=self.page.maxh;
  2302. if (scx<0) scx=0;
  2303. else if (scx>self.page.maxw) scx=self.page.maxw;
  2304. if ((scx!=self.newscrollx)||(sc!=self.newscrolly)) self.doScrollPos(scx,sc);
  2305. else {
  2306. if (self.onscrollend) {
  2307. var info = {"type":"scrollend","current":{"x":sx,"y":sy},"end":{"x":self.newscrollx,"y":self.newscrolly}};
  2308. self.onscrollend.call(self,info);
  2309. }
  2310. }
  2311. } else {
  2312. self.timer = setAnimationFrame(scrolling)||1;
  2313. }
  2314. };
  2315. self.cancelAnimationFrame=false;
  2316. self.timer = 1;
  2317.  
  2318. if (self.onscrollstart&&!self.scrollrunning) {
  2319. var info = {"type":"scrollstart","current":{"x":px,"y":py},"request":{"x":x,"y":y},"end":{"x":self.newscrollx,"y":self.newscrolly},"speed":ms};
  2320. self.onscrollstart.call(self,info);
  2321. }
  2322.  
  2323. scrolling();
  2324.  
  2325. if ((py==self.page.maxh&&y>=py)||(px==self.page.maxw&&x>=px)) self.checkContentSize();
  2326.  
  2327. self.noticeCursor();
  2328. };
  2329.  
  2330. this.cancelScroll = function() {
  2331. if (self.timer) clearAnimationFrame(self.timer);
  2332. self.timer = 0;
  2333. self.bzscroll = false;
  2334. self.scrollrunning = false;
  2335. return self;
  2336. };
  2337.  
  2338. }
  2339.  
  2340. this.doScrollBy = function(stp,relative) {
  2341. var ny = 0;
  2342. if (relative) {
  2343. ny = Math.floor((self.scroll.y-stp)*self.scrollratio.y)
  2344. } else {
  2345. var sy = (self.timer) ? self.newscrolly : self.getScrollTop(true);
  2346. ny = sy-stp;
  2347. }
  2348. if (self.bouncescroll) {
  2349. var haf = Math.round(self.view.h/2);
  2350. if (ny<-haf) ny=-haf
  2351. else if (ny>(self.page.maxh+haf)) ny = (self.page.maxh+haf);
  2352. }
  2353. self.cursorfreezed = false;
  2354.  
  2355. py = self.getScrollTop(true);
  2356. if (ny<0&&py<=0) return self.noticeCursor();
  2357. else if (ny>self.page.maxh&&py>=self.page.maxh) {
  2358. self.checkContentSize();
  2359. return self.noticeCursor();
  2360. }
  2361.  
  2362. self.doScrollTop(ny);
  2363. };
  2364.  
  2365. this.doScrollLeftBy = function(stp,relative) {
  2366. var nx = 0;
  2367. if (relative) {
  2368. nx = Math.floor((self.scroll.x-stp)*self.scrollratio.x)
  2369. } else {
  2370. var sx = (self.timer) ? self.newscrollx : self.getScrollLeft(true);
  2371. nx = sx-stp;
  2372. }
  2373. if (self.bouncescroll) {
  2374. var haf = Math.round(self.view.w/2);
  2375. if (nx<-haf) nx=-haf
  2376. else if (nx>(self.page.maxw+haf)) nx = (self.page.maxw+haf);
  2377. }
  2378. self.cursorfreezed = false;
  2379.  
  2380. px = self.getScrollLeft(true);
  2381. if (nx<0&&px<=0) return self.noticeCursor();
  2382. else if (nx>self.page.maxw&&px>=self.page.maxw) return self.noticeCursor();
  2383.  
  2384. self.doScrollLeft(nx);
  2385. };
  2386.  
  2387. this.doScrollTo = function(pos,relative) {
  2388. var ny = (relative) ? Math.round(pos*self.scrollratio.y) : pos;
  2389. if (ny<0) ny=0
  2390. else if (ny>self.page.maxh) ny = self.page.maxh;
  2391. self.cursorfreezed = false;
  2392. self.doScrollTop(pos);
  2393. };
  2394.  
  2395. this.checkContentSize = function() {
  2396. var pg = self.getContentSize();
  2397. if ((pg.h!=self.page.h)||(pg.w!=self.page.w)) self.resize(false,pg);
  2398. };
  2399.  
  2400. self.onscroll = function(e) {
  2401. if (self.rail.drag) return;
  2402. if (!self.cursorfreezed) {
  2403. self.synched('scroll',function(){
  2404. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  2405. if (self.railh) self.scroll.x = Math.round(self.getScrollLeft() * (1/self.scrollratio.x));
  2406. self.noticeCursor();
  2407. });
  2408. }
  2409. };
  2410. self.bind(self.docscroll,"scroll",self.onscroll);
  2411.  
  2412. this.doZoomIn = function(e) {
  2413. if (self.zoomactive) return;
  2414. self.zoomactive = true;
  2415.  
  2416. self.zoomrestore = {
  2417. style:{}
  2418. };
  2419. var lst = ['position','top','left','zIndex','backgroundColor','marginTop','marginBottom','marginLeft','marginRight'];
  2420. var win = self.win[0].style;
  2421. for(var a in lst) {
  2422. var pp = lst[a];
  2423. self.zoomrestore.style[pp] = (typeof win[pp]!='undefined') ? win[pp] : '';
  2424. }
  2425.  
  2426. self.zoomrestore.style.width = self.win.css('width');
  2427. self.zoomrestore.style.height = self.win.css('height');
  2428.  
  2429. self.zoomrestore.padding = {
  2430. w:self.win.outerWidth()-self.win.width(),
  2431. h:self.win.outerHeight()-self.win.height()
  2432. };
  2433.  
  2434. if (cap.isios4) {
  2435. self.zoomrestore.scrollTop = $(window).scrollTop();
  2436. $(window).scrollTop(0);
  2437. }
  2438.  
  2439. self.win.css({
  2440. "position":(cap.isios4)?"absolute":"fixed",
  2441. "top":0,
  2442. "left":0,
  2443. "z-index":self.opt.zindex+100,
  2444. "margin":"0px"
  2445. });
  2446. var bkg = self.win.css("backgroundColor");
  2447. if (bkg==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor","#fff");
  2448. self.rail.css({"z-index":self.opt.zindex+110});
  2449. self.zoom.css({"z-index":self.opt.zindex+112});
  2450. self.zoom.css('backgroundPosition','0px -18px');
  2451. self.resizeZoom();
  2452.  
  2453. if (self.onzoomin) self.onzoomin.call(self);
  2454.  
  2455. return self.cancelEvent(e);
  2456. };
  2457.  
  2458. this.doZoomOut = function(e) {
  2459. if (!self.zoomactive) return;
  2460. self.zoomactive = false;
  2461.  
  2462. self.win.css("margin","");
  2463. self.win.css(self.zoomrestore.style);
  2464.  
  2465. if (cap.isios4) {
  2466. $(window).scrollTop(self.zoomrestore.scrollTop);
  2467. }
  2468.  
  2469. self.rail.css({"z-index":(self.ispage)?self.opt.zindex:self.opt.zindex+2});
  2470. self.zoom.css({"z-index":self.opt.zindex});
  2471. self.zoomrestore = false;
  2472. self.zoom.css('backgroundPosition','0px 0px');
  2473. self.onResize();
  2474.  
  2475. if (self.onzoomout) self.onzoomout.call(self);
  2476.  
  2477. return self.cancelEvent(e);
  2478. };
  2479.  
  2480. this.doZoom = function(e) {
  2481. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  2482. };
  2483.  
  2484. this.resizeZoom = function() {
  2485. if (!self.zoomactive) return;
  2486.  
  2487. var py = self.getScrollTop(); //preserve scrolling position
  2488. self.win.css({
  2489. width:$(window).width()-self.zoomrestore.padding.w+"px",
  2490. height:$(window).height()-self.zoomrestore.padding.h+"px"
  2491. });
  2492. self.onResize();
  2493. self.setScrollTop(Math.min(self.page.maxh,py));
  2494. };
  2495.  
  2496. this.init();
  2497.  
  2498. $.nicescroll.push(this);
  2499.  
  2500. };
  2501.  
  2502. // Inspired by the work of Kin Blas
  2503. // http://webpro.host.adobe.com/people/jblas/momentum/includes/jquery.momentum.0.7.js
  2504.  
  2505.  
  2506. var ScrollMomentumClass2D = function(nc) {
  2507. var self = this;
  2508. this.nc = nc;
  2509.  
  2510. this.lastx = 0;
  2511. this.lasty = 0;
  2512. this.speedx = 0;
  2513. this.speedy = 0;
  2514. this.lasttime = 0;
  2515. this.steptime = 0;
  2516. this.snapx = false;
  2517. this.snapy = false;
  2518. this.demulx = 0;
  2519. this.demuly = 0;
  2520.  
  2521. this.lastscrollx = -1;
  2522. this.lastscrolly = -1;
  2523.  
  2524. this.chkx = 0;
  2525. this.chky = 0;
  2526.  
  2527. this.timer = 0;
  2528.  
  2529. this.time = function() {
  2530. return +new Date();//beautifull hack
  2531. };
  2532.  
  2533. this.reset = function(px,py) {
  2534. self.stop();
  2535. var now = self.time();
  2536. self.steptime = 0;
  2537. self.lasttime = now;
  2538. self.speedx = 0;
  2539. self.speedy = 0;
  2540. self.lastx = px;
  2541. self.lasty = py;
  2542. self.lastscrollx = -1;
  2543. self.lastscrolly = -1;
  2544. };
  2545.  
  2546. this.update = function(px,py) {
  2547. var now = self.time();
  2548. self.steptime = now - self.lasttime;
  2549. self.lasttime = now;
  2550. var dy = py - self.lasty;
  2551. var dx = px - self.lastx;
  2552. var sy = self.nc.getScrollTop();
  2553. var sx = self.nc.getScrollLeft();
  2554. var newy = sy + dy;
  2555. var newx = sx + dx;
  2556. self.snapx = (newx<0)||(newx>self.nc.page.maxw);
  2557. self.snapy = (newy<0)||(newy>self.nc.page.maxh);
  2558. self.speedx = dx;
  2559. self.speedy = dy;
  2560. self.lastx = px;
  2561. self.lasty = py;
  2562. };
  2563.  
  2564. this.stop = function() {
  2565. self.nc.unsynched("domomentum2d");
  2566. if (self.timer) clearTimeout(self.timer);
  2567. self.timer = 0;
  2568. self.lastscrollx = -1;
  2569. self.lastscrolly = -1;
  2570. };
  2571.  
  2572. this.doSnapy = function(nx,ny) {
  2573. var snap = false;
  2574.  
  2575. if (ny<0) {
  2576. ny=0;
  2577. snap=true;
  2578. }
  2579. else if (ny>self.nc.page.maxh) {
  2580. ny=self.nc.page.maxh;
  2581. snap=true;
  2582. }
  2583.  
  2584. if (nx<0) {
  2585. nx=0;
  2586. snap=true;
  2587. }
  2588. else if (nx>self.nc.page.maxw) {
  2589. nx=self.nc.page.maxw;
  2590. snap=true;
  2591. }
  2592.  
  2593. if (snap) self.nc.doScrollPos(nx,ny,self.nc.opt.snapbackspeed);
  2594. };
  2595.  
  2596. this.doMomentum = function(gp) {
  2597. var t = self.time();
  2598. var l = (gp) ? t+gp : self.lasttime;
  2599.  
  2600. var sl = self.nc.getScrollLeft();
  2601. var st = self.nc.getScrollTop();
  2602.  
  2603. var pageh = self.nc.page.maxh;
  2604. var pagew = self.nc.page.maxw;
  2605.  
  2606. self.speedx = (pagew>0) ? Math.min(60,self.speedx) : 0;
  2607. self.speedy = (pageh>0) ? Math.min(60,self.speedy) : 0;
  2608.  
  2609. var chk = l && (t - l) <= 50;
  2610.  
  2611. if ((st<0)||(st>pageh)||(sl<0)||(sl>pagew)) chk = false;
  2612.  
  2613. var sy = (self.speedy && chk) ? self.speedy : false;
  2614. var sx = (self.speedx && chk) ? self.speedx : false;
  2615.  
  2616. if (sy||sx) {
  2617. var tm = Math.max(16,self.steptime); //timeout granularity
  2618.  
  2619. if (tm>50) { // do smooth
  2620. var xm = tm/50;
  2621. self.speedx*=xm;
  2622. self.speedy*=xm;
  2623. tm = 50;
  2624. }
  2625.  
  2626. self.demulxy = 0;
  2627.  
  2628. self.lastscrollx = self.nc.getScrollLeft();
  2629. self.chkx = self.lastscrollx;
  2630. self.lastscrolly = self.nc.getScrollTop();
  2631. self.chky = self.lastscrolly;
  2632.  
  2633. var nx = self.lastscrollx;
  2634. var ny = self.lastscrolly;
  2635.  
  2636. var onscroll = function(){
  2637. var df = ((self.time()-t)>600) ? 0.04 : 0.02;
  2638.  
  2639. if (self.speedx) {
  2640. nx = Math.floor(self.lastscrollx - (self.speedx*(1-self.demulxy)));
  2641. self.lastscrollx = nx;
  2642. if ((nx<0)||(nx>pagew)) df=0.10;
  2643. }
  2644.  
  2645. if (self.speedy) {
  2646. ny = Math.floor(self.lastscrolly - (self.speedy*(1-self.demulxy)));
  2647. self.lastscrolly = ny;
  2648. if ((ny<0)||(ny>pageh)) df=0.10;
  2649. }
  2650.  
  2651. self.demulxy = Math.min(1,self.demulxy+df);
  2652.  
  2653. self.nc.synched("domomentum2d",function(){
  2654.  
  2655. if (self.speedx) {
  2656. var scx = self.nc.getScrollLeft();
  2657. if (scx!=self.chkx) self.stop();
  2658. self.chkx=nx;
  2659. self.nc.setScrollLeft(nx);
  2660. }
  2661.  
  2662. if (self.speedy) {
  2663. var scy = self.nc.getScrollTop();
  2664. if (scy!=self.chky) self.stop();
  2665. self.chky=ny;
  2666. self.nc.setScrollTop(ny);
  2667. }
  2668.  
  2669. if(!self.timer) {
  2670. self.nc.hideCursor();
  2671. self.doSnapy(nx,ny);
  2672. }
  2673.  
  2674. });
  2675.  
  2676. if (self.demulxy<1) {
  2677. self.timer = setTimeout(onscroll,tm);
  2678. } else {
  2679. self.stop();
  2680. self.nc.hideCursor();
  2681. self.doSnapy(nx,ny);
  2682. }
  2683. };
  2684.  
  2685. onscroll();
  2686.  
  2687. } else {
  2688. self.doSnapy(self.nc.getScrollLeft(),self.nc.getScrollTop());
  2689. }
  2690.  
  2691. }
  2692.  
  2693. };
  2694.  
  2695.  
  2696. // override jQuery scrollTop
  2697.  
  2698. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  2699.  
  2700. $.cssHooks["pageYOffset"] = {
  2701. get: function(elem,computed,extra) {
  2702. var nice = $.data(elem,'__nicescroll')||false;
  2703. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  2704. },
  2705. set: function(elem,value) {
  2706. var nice = $.data(elem,'__nicescroll')||false;
  2707. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem,value);
  2708. return this;
  2709. }
  2710. };
  2711.  
  2712. /*
  2713. $.fx.step["scrollTop"] = function(fx){
  2714. $.cssHooks["scrollTop"].set( fx.elem, fx.now + fx.unit );
  2715. };
  2716. */
  2717.  
  2718. jQuery.fn.scrollTop = function(value) {
  2719. if (typeof value == "undefined") {
  2720. var nice = (this[0]) ? $.data(this[0],'__nicescroll')||false : false;
  2721. return (nice&&nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  2722. } else {
  2723. return this.each(function() {
  2724. var nice = $.data(this,'__nicescroll')||false;
  2725. (nice&&nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this),value);
  2726. });
  2727. }
  2728. }
  2729.  
  2730. // override jQuery scrollLeft
  2731.  
  2732. var _scrollLeft = jQuery.fn.scrollLeft; // preserve original function
  2733.  
  2734. $.cssHooks.pageXOffset = {
  2735. get: function(elem,computed,extra) {
  2736. var nice = $.data(elem,'__nicescroll')||false;
  2737. return (nice&&nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(elem);
  2738. },
  2739. set: function(elem,value) {
  2740. var nice = $.data(elem,'__nicescroll')||false;
  2741. (nice&&nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call(elem,value);
  2742. return this;
  2743. }
  2744. };
  2745.  
  2746. /*
  2747. $.fx.step["scrollLeft"] = function(fx){
  2748. $.cssHooks["scrollLeft"].set( fx.elem, fx.now + fx.unit );
  2749. };
  2750. */
  2751.  
  2752. jQuery.fn.scrollLeft = function(value) {
  2753. if (typeof value == "undefined") {
  2754. var nice = (this[0]) ? $.data(this[0],'__nicescroll')||false : false;
  2755. return (nice&&nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(this);
  2756. } else {
  2757. return this.each(function() {
  2758. var nice = $.data(this,'__nicescroll')||false;
  2759. (nice&&nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call($(this),value);
  2760. });
  2761. }
  2762. }
  2763.  
  2764. var NiceScrollArray = function(doms) {
  2765. var self = this;
  2766. this.length = 0;
  2767. this.name = "nicescrollarray";
  2768.  
  2769. this.each = function(fn) {
  2770. for(var a=0;a<self.length;a++) fn.call(self[a]);
  2771. return self;
  2772. };
  2773.  
  2774. this.push = function(nice) {
  2775. self[self.length]=nice;
  2776. self.length++;
  2777. };
  2778.  
  2779. this.eq = function(idx) {
  2780. return self[idx];
  2781. };
  2782.  
  2783. if (doms) {
  2784. for(a=0;a<doms.length;a++) {
  2785. var nice = $.data(doms[a],'__nicescroll')||false;
  2786. if (nice) {
  2787. this[this.length]=nice;
  2788. this.length++;
  2789. }
  2790. };
  2791. }
  2792.  
  2793. return this;
  2794. };
  2795.  
  2796. function mplex(el,lst,fn) {
  2797. for(var a=0;a<lst.length;a++) fn(el,lst[a]);
  2798. };
  2799. mplex(
  2800. NiceScrollArray.prototype,
  2801. ['show','hide','toggle','onResize','resize','remove','stop','doScrollPos'],
  2802. function(e,n) {
  2803. e[n] = function(){
  2804. var args = arguments;
  2805. return this.each(function(){
  2806. this[n].apply(this,args);
  2807. });
  2808. };
  2809. }
  2810. );
  2811.  
  2812. jQuery.fn.getNiceScroll = function(index) {
  2813. if (typeof index == "undefined") {
  2814. return new NiceScrollArray(this);
  2815. } else {
  2816. var nice = $.data(this[index],'__nicescroll')||false;
  2817. return nice;
  2818. }
  2819. };
  2820.  
  2821. jQuery.extend(jQuery.expr[':'], {
  2822. nicescroll: function(a) {
  2823. return ($.data(a,'__nicescroll'))?true:false;
  2824. }
  2825. });
  2826.  
  2827. $.fn.niceScroll = function(wrapper,opt) {
  2828. if (typeof opt=="undefined") {
  2829. if ((typeof wrapper=="object")&&!("jquery" in wrapper)) {
  2830. opt = wrapper;
  2831. wrapper = false;
  2832. }
  2833. }
  2834. var ret = new NiceScrollArray();
  2835. if (typeof opt=="undefined") opt = {};
  2836.  
  2837. if (wrapper||false) {
  2838. opt.doc = $(wrapper);
  2839. opt.win = $(this);
  2840. }
  2841. var docundef = !("doc" in opt);
  2842. if (!docundef&&!("win" in opt)) opt.win = $(this);
  2843.  
  2844. this.each(function() {
  2845. var nice = $(this).data('__nicescroll')||false;
  2846. if (!nice) {
  2847. opt.doc = (docundef) ? $(this) : opt.doc;
  2848. nice = new NiceScrollClass(opt,$(this));
  2849. $(this).data('__nicescroll',nice);
  2850. }
  2851. ret.push(nice);
  2852. });
  2853. return (ret.length==1) ? ret[0] : ret;
  2854. };
  2855.  
  2856. window.NiceScroll = {
  2857. getjQuery:function(){return jQuery}
  2858. };
  2859.  
  2860. if (!$.nicescroll) {
  2861. $.nicescroll = new NiceScrollArray();
  2862. }
  2863.  
  2864. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement