You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3714 lines
117 KiB

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