Jquery Nicescroll
Jquery Nicescroll
Jquery Nicescroll
nicescroll
-- version 3.7.6
-- copyright 2017-07-19 InuYaksa*2017
-- licensed under the MIT
--
-- https://nicescroll.areaaperta.com/
-- https://github.com/inuyaksa/jquery.nicescroll
--
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS.
module.exports = factory(require('jquery'));
} else {
// Browser globals.
factory(jQuery);
}
}(function (jQuery) {
"use strict";
// globals
var domfocus = false,
mousefocus = false,
tabindexcounter = 0,
ascrailcounter = 2000,
globalmaxzindex = 0;
// http://stackoverflow.com/questions/2161159/get-script-path
function getScriptPath() {
var scripts = _doc.currentScript || (function () { var s =
_doc.getElementsByTagName('script'); return (s.length) ? s[s.length - 1] :
false; })();
var path = scripts ? scripts.src.split('?')[0] : '';
return (path.split('/').length > 0) ? path.split('/').slice(0, -1).join('/') +
'/' : '';
}
// based on code by Paul Irish
https://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
var setAnimationFrame = _win.requestAnimationFrame ||
_win.webkitRequestAnimationFrame || _win.mozRequestAnimationFrame || false;
var clearAnimationFrame = _win.cancelAnimationFrame ||
_win.webkitCancelAnimationFrame || _win.mozCancelAnimationFrame || false;
if (!setAnimationFrame) {
var anilasttime = 0;
setAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - anilasttime));
var id = _win.setTimeout(function () { callback(currTime + timeToCall); },
timeToCall);
anilasttime = currTime + timeToCall;
return id;
};
clearAnimationFrame = function (id) {
_win.clearTimeout(id);
};
} else {
if (!_win.cancelAnimationFrame) clearAnimationFrame = function (id) { };
}
var _globaloptions = {
zindex: "auto",
cursoropacitymin: 0,
cursoropacitymax: 1,
cursorcolor: "#424242",
cursorwidth: "6px",
cursorborder: "1px solid #fff",
cursorborderradius: "5px",
scrollspeed: 40,
mousescrollstep: 9 * 3,
touchbehavior: false, // deprecated
emulatetouch: false, // replacing touchbehavior
hwacceleration: true,
usetransition: true,
boxzoom: false,
dblclickzoom: true,
gesturezoom: true,
grabcursorenabled: true,
autohidemode: true,
background: "",
iframeautoresize: true,
cursorminheight: 32,
preservenativescrolling: true,
railoffset: false,
railhoffset: false,
bouncescroll: true,
spacebarenabled: true,
railpadding: {
top: 0,
right: 0,
left: 0,
bottom: 0
},
disableoutline: true,
horizrailenabled: true,
railalign: "right",
railvalign: "bottom",
enabletranslate3d: true,
enablemousewheel: true,
enablekeyboard: true,
smoothscroll: true,
sensitiverail: true,
enablemouselockapi: true,
// cursormaxheight:false,
cursorfixedheight: false,
directionlockdeadzone: 6,
hidecursordelay: 400,
nativeparentscrolling: true,
enablescrollonselection: true,
overflowx: true,
overflowy: true,
cursordragspeed: 0.3,
rtlmode: "auto",
cursordragontouch: false,
oneaxismousemode: "auto",
scriptpath: getScriptPath(),
preventmultitouchscrolling: true,
disablemutationobserver: false,
enableobserver: true,
scrollbarid: false
};
d.ismac = /^mac$/i.test(_platform);
d.isandroid = (/android/i.test(_agent));
d.trstyle = false;
d.hastransform = false;
d.hastranslate3d = false;
d.transitionstyle = false;
d.hastransition = false;
d.transitionend = false;
d.trstyle = "transform";
d.hastransform = ("transform" in _style) || (function () {
var check = ['msTransform', 'webkitTransform', 'MozTransform', 'OTransform'];
for (var a = 0, c = check.length; a < c; a++) {
if (_style[check[a]] !== undefined) {
d.trstyle = check[a];
break;
}
}
d.hastransform = (!!d.trstyle);
})();
if (d.hastransform) {
_style[d.trstyle] = "translate3d(1px,2px,3px)";
d.hastranslate3d = /translate3d/.test(_style[d.trstyle]);
}
d.transitionstyle = "transition";
d.prefixstyle = '';
d.transitionend = "transitionend";
d.transitionend = false;
var check = ['webkitTransition', 'msTransition', 'MozTransition',
'OTransition', 'OTransition', 'KhtmlTransition'];
var prefix = ['-webkit-', '-ms-', '-moz-', '-o-', '-o', '-khtml-'];
var evs = ['webkitTransitionEnd', 'msTransitionEnd', 'transitionend',
'otransitionend', 'oTransitionEnd', 'KhtmlTransitionEnd'];
for (var a = 0, c = check.length; a < c; a++) {
if (check[a] in _style) {
d.transitionstyle = check[a];
d.prefixstyle = prefix[a];
d.transitionend = evs[a];
break;
}
}
if (d.ischrome26) d.prefixstyle = prefix[1]; // always use prefix
d.hastransition = (d.transitionstyle);
})();
function detectCursorGrab() {
var lst = ['grab', '-webkit-grab', '-moz-grab'];
if ((d.ischrome && !d.ischrome38) || d.isie) lst = []; // force setting for
IE returns false positive and chrome cursor bug
for (var a = 0, l = lst.length; a < l; a++) {
var p = lst[a];
_style.cursor = p;
if (_style.cursor == p) return p;
}
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!
}
d.cursorgrabvalue = detectCursorGrab();
browserdetected = d;
return d;
};
this.version = '3.7.6';
this.name = 'nicescroll';
this.me = me;
if (myopt || false) {
for (var a in opt) {
if (myopt[a] !== undefined) opt[a] = myopt[a];
}
}
this.doc = opt.doc;
this.iddoc = (this.doc && this.doc[0]) ? this.doc[0].id || '' : '';
this.ispage = /^BODY|HTML/.test((opt.win) ? opt.win[0].nodeName :
this.doc[0].nodeName);
this.haswrapper = (opt.win !== false);
this.win = opt.win || (this.ispage ? $window : this.doc);
this.docscroll = (this.ispage && !this.haswrapper) ? $window : this.win;
this.body = $body;
this.viewport = false;
this.isfixed = false;
this.iframe = false;
this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName ==
'IFRAME'));
// Let's start!
this.view = false;
this.page = false;
this.scroll = {
x: 0,
y: 0
};
this.scrollratio = {
x: 0,
y: 0
};
this.cursorheight = 20;
this.scrollvaluemax = 0;
// http://dev.w3.org/csswg/css-writing-modes-3/#logical-to-physical
// http://dev.w3.org/csswg/css-writing-modes-3/#svg-writing-mode
if (opt.rtlmode == "auto") {
var target = this.win[0] == _win ? this.body : this.win;
var writingMode = target.css("writing-mode") || target.css("-webkit-writing-
mode") || target.css("-ms-writing-mode") || target.css("-moz-writing-mode");
this.scrollrunning = false;
this.scrollmom = false;
this.rail = false;
this.cursor = false;
this.cursorfreezed = false;
this.selectiondrag = false;
this.zoom = false;
this.zoomactive = false;
this.hasfocus = false;
this.hasmousefocus = false;
//this.visibility = true;
this.railslocked = false; // locked by resize
this.locked = false; // prevent lost of locked status sets by user
this.hidden = false; // rails always hidden
this.cursoractive = true; // user can interact with cursors
this.overflowx = opt.overflowx;
this.overflowy = opt.overflowy;
this.nativescrollingarea = false;
this.checkarea = 0;
this.delaylist = {};
this.synclist = {};
this.lastdeltax = 0;
this.lastdeltay = 0;
this.detected = getBrowserDetection();
if (!this.isrtlmode) {
this.hasreversehr = false;
} else if (this.isvertical) { // RTL mode with reverse horizontal axis
this.hasreversehr = !(cap.iswebkit || cap.isie || cap.isie11);
} else {
this.hasreversehr = !(cap.iswebkit || (cap.isie && !cap.isie10 && !
cap.isie11));
}
this.st = st;
this.ed = ed;
this.spd = spd;
this.p1 = p1 || 0;
this.p2 = p2 || 1;
this.p3 = p3 || 0;
this.p4 = p4 || 1;
this.ts = now();
this.df = ed - st;
};
BezierClass.prototype = {
B2: function (t) {
return 3 * (1 - t) * (1 - t) * t;
},
B3: function (t) {
return 3 * (1 - t) * t * t;
},
B4: function (t) {
return t * t * t;
},
getPos: function () {
return (now() - this.ts) / this.spd;
},
getNow: function () {
var pc = (now() - this.ts) / this.spd;
var bz = this.B2(pc) + this.B3(pc) + this.B4(pc);
return (pc >= 1) ? this.ed : this.st + (this.df * bz) | 0;
},
update: function (ed, spd) {
this.st = this.getNow();
this.ed = ed;
this.spd = spd;
this.ts = now();
this.df = this.ed - this.st;
return this;
}
};
this.doc.translate = {
x: 0,
y: 0,
tx: "0px",
ty: "0px"
};
//this one can help to enable hw accel on ios6 http://indiegamr.com/ios6-
html-hardware-acceleration-changes-and-how-to-fix-them/
if (cap.hastranslate3d && cap.isios) this.doc.css("-webkit-backface-
visibility", "hidden"); // prevent flickering
http://stackoverflow.com/questions/3461441/
this.getScrollTop = function () {
return self.docscroll.scrollTop();
};
this.setScrollTop = function (val) {
self.docscroll.scrollTop(val);
};
this.getScrollLeft = function () {
var val;
if (!self.hasreversehr) {
val = self.docscroll.scrollLeft();
} else if (self.detected.ismozilla) {
val = self.page.maxw - Math.abs(self.docscroll.scrollLeft());
} else {
val = self.page.maxw - self.docscroll.scrollLeft();
}
return val;
};
this.setScrollLeft = function (val) {
return setTimeout(function () {
if (!self) return;
if (self.hasreversehr) {
if (self.detected.ismozilla) {
val = -(self.page.maxw - val);
} else {
val = self.page.maxw - val;
}
}
return self.docscroll.scrollLeft(val);
}, 1);
};
}
function getZIndex() {
var dom = self.win;
if ("zIndex" in dom) return dom.zIndex(); // use jQuery UI method when
available
while (dom.length > 0) {
if (dom[0].nodeType == 9) return false;
var zi = dom.css('zIndex');
if (!isNaN(zi) && zi !== 0) return parseInt(zi);
dom = dom.parent();
}
return false;
}
//inspired by http://forum.jquery.com/topic/width-includes-border-width-when-
set-to-thin-medium-thick-in-ie
var _convertBorderWidth = {
"thin": 1,
"medium": 3,
"thick": 5
};
this.getDocumentScrollOffset = function () {
return {
top: _win.pageYOffset || _doc.documentElement.scrollTop,
left: _win.pageXOffset || _doc.documentElement.scrollLeft
};
};
this.getOffset = function () {
if (self.isfixed) {
var ofs = self.win.offset(); // fix Chrome auto issue (when right/bottom
props only)
var scrl = self.getDocumentScrollOffset();
ofs.top -= scrl.top;
ofs.left -= scrl.left;
return ofs;
}
var ww = self.win.offset();
if (!self.viewport) return ww;
var vp = self.viewport.offset();
return {
top: ww.top - vp.top,
left: ww.left - vp.left
};
};
off = opt.railoffset;
if (off) {
if (off.top) pos.top += off.top;
if (off.left) pos.left += off.left;
}
if (!self.railslocked) self.rail.css({
top: pos.top,
left: pos.left,
height: ((len) ? len.h : self.win.innerHeight()) - (opt.railpadding.top +
opt.railpadding.bottom)
});
if (self.zoom) {
self.zoom.css({
top: pos.top + 1,
left: (self.rail.align == 1) ? pos.left - 20 : pos.left +
self.rail.width + 4
});
}
}
};
if (self.railslocked) return;
self.cancelEvent(e);
if (!("pageY" in e)) {
e.pageX = e.clientX + _doc.documentElement.scrollLeft;
e.pageY = e.clientY + _doc.documentElement.scrollTop;
}
if (dbl) {
fn = (hr) ? self.doScrollLeft : self.doScrollTop;
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);
self.unsynched("relativexy");
fn(cur|0);
} else {
fn = (hr) ? self.doScrollLeftBy : self.doScrollBy;
cur = (hr) ? self.scroll.x : self.scroll.y;
pos = (hr) ? e.pageX - self.railh.offset().left : e.pageY -
self.rail.offset().top;
pg = (hr) ? self.view.w : self.view.h;
fn((cur >= pos) ? pg : -pg);
}
};
self.newscrolly = self.newscrollx = 0;
self.hasborderbox = false;
this.init = function () {
self.saved.css = [];
if (self.ishwscroll) {
this.doc.css(cap.transitionstyle, cap.prefixstyle + 'transform 0ms ease-
out');
if (cap.transitionend) self.bind(self.doc, cap.transitionend,
self.onScrollTransitionEnd, false); //I have got to do something usefull!!
}
self.zindex = "auto";
if (!self.ispage && opt.zindex == "auto") {
self.zindex = getZIndex() || "auto";
} else {
self.zindex = opt.zindex;
}
if (!self.ispage || !cap.isieold) {
self.css(cont, _scrollyhidden);
self.cursor = cursor;
rail.append(cursor);
rail.visibility = true;
rail.scrollable = true;
self.rail = rail;
self.rail.drag = false;
self.zoom = $(zoom);
self.zoom.css({
cursor: "pointer",
zIndex: self.zindex,
backgroundImage: 'url(' + opt.scriptpath + 'zoomico.png)',
height: 18,
width: 18,
backgroundPosition: '0 0'
});
if (opt.dblclickzoom) self.bind(self.win, "dblclick", self.doZoom);
if (cap.cantouch && opt.gesturezoom) {
self.ongesturezoom = function (e) {
if (e.scale > 1.5) self.doZoomIn(e);
if (e.scale < 0.8) self.doZoomOut(e);
return self.cancelEvent(e);
};
self.bind(self.win, "gestureend", self.ongesturezoom);
}
}
// init HORIZ
self.railh = false;
var railh;
if (opt.horizrailenabled) {
self.css(cont, {
overflowX: 'hidden'
});
cursor = $(_doc.createElement('div'));
cursor.css({
position: "absolute",
top: 0,
height: opt.cursorwidth,
width: 0,
backgroundColor: opt.cursorcolor,
border: opt.cursorborder,
backgroundClip: 'padding-box',
'-webkit-border-radius': opt.cursorborderradius,
'-moz-border-radius': opt.cursorborderradius,
'border-radius': opt.cursorborderradius
});
cursor.addClass('nicescroll-cursors');
self.cursorh = cursor;
railh = $(_doc.createElement('div'));
railh.attr('id', self.id + '-hr');
railh.addClass('nicescroll-rails nicescroll-rails-hr');
railh.height = Math.max(parseFloat(opt.cursorwidth),
cursor.outerHeight());
railh.css({
height: railh.height + "px",
'zIndex': self.zindex,
"background": opt.background
});
railh.append(cursor);
railh.visibility = true;
railh.scrollable = true;
self.railh = railh;
self.railh.drag = false;
if (self.ispage) {
rail.css({
position: "fixed",
top: 0,
height: "100%"
});
self.body.append(rail);
if (self.railh) {
railh.css({
position: "fixed",
left: 0,
width: "100%"
});
self.body.append(railh);
}
} else {
if (self.ishwscroll) {
if (self.win.css('position') == 'static') self.css(self.win,
{ 'position': 'relative' });
var bd = (self.win[0].nodeName == 'HTML') ? self.body : self.win;
$(bd).scrollTop(0).scrollLeft(0); // fix rail position if content
already scrolled
if (self.zoom) {
self.zoom.css({
position: "absolute",
top: 1,
right: 0,
"margin-right": rail.width + 4
});
bd.append(self.zoom);
}
rail.css({
position: "absolute",
top: 0
});
rail.css((rail.align) ? { right: 0 } : { left: 0 });
bd.append(rail);
if (railh) {
railh.css({
position: "absolute",
left: 0,
bottom: 0
});
railh.css((railh.align) ? { bottom: 0 } : { top: 0 });
bd.append(railh);
}
} else {
self.isfixed = (self.win.css("position") == "fixed");
var rlpos = (self.isfixed) ? "fixed" : "absolute";
rail.css({
position: rlpos
});
if (self.zoom) self.zoom.css({
position: rlpos
});
self.updateScrollBar();
self.body.append(rail);
if (self.zoom) self.body.append(self.zoom);
if (self.railh) {
railh.css({
position: rlpos
});
self.body.append(railh);
}
}
if (cap.isios) self.css(self.win, {
'-webkit-tap-highlight-color': 'rgba(0,0,0,0)',
'-webkit-touch-callout': 'none'
}); // prevent grey layer on click
if (opt.disableoutline) {
if (cap.isie) self.win.attr("hideFocus", "true"); // IE, prevent dotted
rectangle on focused div
if (cap.iswebkit) self.win.css('outline', 'none'); // Webkit outline
}
self.hasmoving = false;
if (self.scrollmom.timer) {
self.triggerScrollEnd();
self.scrollmom.stop();
}
if (!self.railslocked) {
var tg = self.getTarget(e);
if (tg) {
var skp = (/INPUT/i.test(tg.nodeName)) && (/range/i.test(tg.type));
if (skp) return self.stopPropagation(e);
}
if (self.forcescreen) {
var le = e;
e = {
"original": (e.original) ? e.original : e
};
e.clientX = le.screenX;
e.clientY = le.screenY;
}
self.rail.drag = {
x: e.clientX,
y: e.clientY,
sx: self.scroll.x,
sy: self.scroll.y,
st: self.getScrollTop(),
sl: self.getScrollLeft(),
pt: 2,
dl: false,
tg: tg
};
if (self.ispage || !opt.directionlockdeadzone) {
self.rail.drag.dl = "f";
} else {
var view = {
w: $window.width(),
h: $window.height()
};
self.hasmoving = false;
self.lastmouseup = false;
self.scrollmom.reset(e.clientX, e.clientY);
if (tg&&ismouse) {
var ip = /INPUT|SELECT|BUTTON|TEXTAREA/i.test(tg.nodeName);
if (!ip) {
if (cap.hasmousecapture) tg.setCapture();
if (opt.emulatetouch) {
if (tg.onclick && !(tg._onclick || false)) { // intercept DOM0
onclick event
tg._onclick = tg.onclick;
tg.onclick = function (e) {
if (self.hasmoving) return false;
tg._onclick.call(this, e);
};
}
return self.cancelEvent(e);
}
return self.stopPropagation(e);
}
if (/SUBMIT|CANCEL|BUTTON/i.test($(tg).attr('type'))) {
self.preventclick = {
"tg": tg,
"click": false
};
}
}
}
};
if (self.rail.drag.pt == 2) {
//if (e.pointerType && e.pointerType != 2 && e.pointerType !=
"touch") return false;
if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType ===
e.MSPOINTER_TYPE_MOUSE)) return false;
self.rail.drag = false;
if (self.hasmoving) {
self.scrollmom.doMomentum();
self.lastmouseup = true;
self.hideCursor();
if (cap.hasmousecapture) _doc.releaseCapture();
if (ismouse) return self.cancelEvent(e);
}
}
else if (self.rail.drag.pt == 1) {
return self.onmouseup(e);
}
};
if (self.rail.drag.pt == 2) {
if (("changedTouches" in e)) {
e.clientX = e.changedTouches[0].clientX;
e.clientY = e.changedTouches[0].clientY;
}
var nx = self.getScrollLeft();
if (!self.hasmoving) {
var ay = Math.abs(my);
var ax = Math.abs(mx);
var dz = opt.directionlockdeadzone;
if (!self.rail.drag.ck) {
if (ay > dz && ax > dz) self.rail.drag.dl = "f";
else if (ay > dz) self.rail.drag.dl = (ax > locktollerance) ? "f"
: "v";
else if (ax > dz) self.rail.drag.dl = (ay > locktollerance) ? "f"
: "h";
}
else if (self.rail.drag.ck == "v") {
if (ax > dz && ay <= locktollerance) {
self.rail.drag = false;
}
else if (ay > dz) self.rail.drag.dl = "v";
}
else if (self.rail.drag.ck == "h") {
if (self.rail.drag.dl) {
if (self.rail.drag.dl == "v") nx = self.rail.drag.sl;
else if (self.rail.drag.dl == "h") ny = self.rail.drag.st;
}
self.synched("touchmove", function () {
if (self.rail.drag && (self.rail.drag.pt == 2)) {
if (self.prepareTransition) self.resetTransition();
if (self.rail.scrollable) self.setScrollTop(ny);
self.scrollmom.update(fx, fy);
if (self.railh && self.railh.scrollable) {
self.setScrollLeft(nx);
self.showCursor(ny, nx);
} else {
self.showCursor(ny);
}
if (cap.isie10) _doc.selection.clear();
}
});
return self.cancelEvent(e);
}
else if (self.rail.drag.pt == 1) { // drag on cursor
return self.onmousemove(e);
}
};
self.cursorfreezed = true;
if (self.rail.drag.hr) {
self.scroll.x = self.rail.drag.sx + (e.touches[0].clientX -
self.rail.drag.x);
if (self.scroll.x < 0) self.scroll.x = 0;
var mw = self.scrollvaluemaxw;
if (self.scroll.x > mw) self.scroll.x = mw;
} else {
self.scroll.y = self.rail.drag.sy + (e.touches[0].clientY -
self.rail.drag.y);
if (self.scroll.y < 0) self.scroll.y = 0;
var my = self.scrollvaluemax;
if (self.scroll.y > my) self.scroll.y = my;
}
self.synched('touchmove', function () {
if (self.rail.drag && (self.rail.drag.pt == 3)) {
self.showCursor();
if (self.rail.drag.hr) self.doScrollLeft(Math.round(self.scroll.x
* self.scrollratio.x), opt.cursordragspeed);
else self.doScrollTop(Math.round(self.scroll.y *
self.scrollratio.y), opt.cursordragspeed);
}
});
return self.cancelEvent(e);
}
};
if (cap.hasmousecapture) tg.setCapture();
if (self.isiframe && !cap.hasmousecapture) {
self.saved.csspointerevents = self.doc.css("pointer-events");
self.css(self.doc, {
"pointer-events": "none"
});
}
self.hasmoving = false;
return self.cancelEvent(e);
};
if (cap.hasmousecapture) _doc.releaseCapture();
if (self.isiframe && !cap.hasmousecapture) self.doc.css("pointer-
events", self.saved.csspointerevents);
self.rail.drag = false;
self.cursorfreezed = false;
if (self.hasmoving) self.triggerScrollEnd();
return self.cancelEvent(e);
}
};
self.cursorfreezed = true;
self.hasmoving = true;
if (self.rail.drag.hr) {
self.scroll.x = self.rail.drag.sx + (e.clientX - self.rail.drag.x);
if (self.scroll.x < 0) self.scroll.x = 0;
var mw = self.scrollvaluemaxw;
if (self.scroll.x > mw) self.scroll.x = mw;
} else {
self.scroll.y = self.rail.drag.sy + (e.clientY - self.rail.drag.y);
if (self.scroll.y < 0) self.scroll.y = 0;
var my = self.scrollvaluemax;
if (self.scroll.y > my) self.scroll.y = my;
}
self.synched('mousemove', function () {
if (self.cursorfreezed) {
self.showCursor();
if (self.rail.drag.hr) {
self.scrollLeft(Math.round(self.scroll.x * self.scrollratio.x));
} else {
self.scrollTop(Math.round(self.scroll.y * self.scrollratio.y));
}
}
});
return self.cancelEvent(e);
}
else {
self.checkarea = 0;
}
};
if (cap.cantouch || opt.emulatetouch) {
} else {
if (e) {
var ww = self.win.outerHeight();
var df = (e.pageY - self.selectiondrag.top);
if (df > 0 && df < ww) df = 0;
if (df >= ww) df -= ww;
self.selectiondrag.df = df;
}
if (self.selectiondrag.df === 0) return;
var rt = -(self.selectiondrag.df*2/6)|0;
self.doScrollBy(rt);
self.debounced("doselectionscroll", function () {
checkSelectionScroll();
}, 50);
};
if (cap.hasw3ctouch) { //IE11+
self.css((self.ispage) ? $("html") : self.win, { 'touch-action':
'none' });
self.css(self.rail, {
'touch-action': 'none'
});
self.css(self.cursor, {
'touch-action': 'none'
});
self.bind(self.win, "pointerdown", self.ontouchstart);
self.bind(_doc, "pointerup", self.ontouchend);
self.delegate(_doc, "pointermove", self.ontouchmove);
} else if (cap.hasmstouch) { //IE10
self.css((self.ispage) ? $("html") : self.win, { '-ms-touch-action':
'none' });
self.css(self.rail, {
'-ms-touch-action': 'none'
});
self.css(self.cursor, {
'-ms-touch-action': 'none'
});
self.bind(self.win, "MSPointerDown", self.ontouchstart);
self.bind(_doc, "MSPointerUp", self.ontouchend);
self.delegate(_doc, "MSPointerMove", self.ontouchmove);
self.bind(self.cursor, "MSGestureHold", function (e) {
e.preventDefault();
});
self.bind(self.cursor, "contextmenu", function (e) {
e.preventDefault();
});
} else if (cap.cantouch) { // smartphones/touch devices
self.bind(self.win, "touchstart", self.ontouchstart, false, true);
self.bind(_doc, "touchend", self.ontouchend, false, true);
self.bind(_doc, "touchcancel", self.ontouchend, false, true);
self.delegate(_doc, "touchmove", self.ontouchmove, false, true);
}
if (opt.emulatetouch) {
self.bind(self.win, "mousedown", self.ontouchstart, false, true);
self.bind(_doc, "mouseup", self.ontouchend, false, true);
self.bind(_doc, "mousemove", self.ontouchmove, false, true);
}
self.rail.css({
cursor: "default"
});
self.railh && self.railh.css({
cursor: "default"
});
if (opt.sensitiverail) {
self.bind(self.rail, "click", function (e) {
self.doRailClick(e, false, false);
});
self.bind(self.rail, "dblclick", function (e) {
self.doRailClick(e, true, false);
});
self.bind(self.cursor, "click", function (e) {
self.cancelEvent(e);
});
self.bind(self.cursor, "dblclick", function (e) {
self.cancelEvent(e);
});
}
if (self.railh) {
self.jqbind(self.railh, "mouseenter", function () {
if (!self.ispage && !self.win.is(":visible")) return false;
if (self.canshowonmouseevent) self.showCursor();
self.rail.active = true;
});
self.jqbind(self.railh, "mouseleave", function () {
self.rail.active = false;
if (!self.rail.drag) self.hideCursor();
});
if (opt.sensitiverail) {
self.bind(self.railh, "click", function (e) {
self.doRailClick(e, false, true);
});
self.bind(self.railh, "dblclick", function (e) {
self.doRailClick(e, true, true);
});
self.bind(self.cursorh, "click", function (e) {
self.cancelEvent(e);
});
self.bind(self.cursorh, "dblclick", function (e) {
self.cancelEvent(e);
});
}
if (self.railh) {
self.bind(self.cursorh, "mousedown", function (e) {
self.onmousedown(e, true);
});
self.bind(self.cursorh, "mouseup", self.onmouseup);
}
if (self.zoom) {
self.jqbind(self.zoom, "mouseenter", function () {
if (self.canshowonmouseevent) self.showCursor();
self.rail.active = true;
});
self.jqbind(self.zoom, "mouseleave", function () {
self.rail.active = false;
if (!self.rail.drag) self.hideCursor();
});
}
} else {
if (opt.cursordragontouch) {
self.bind(self.cursor, "mousedown", self.onmousedown);
self.bind(self.cursor, "mouseup", self.onmouseup);
self.cursorh && self.bind(self.cursorh, "mousedown", function (e) {
self.onmousedown(e, true);
});
self.cursorh && self.bind(self.cursorh, "mouseup", self.onmouseup);
} else {
self.bind(self.rail, "mousedown", function (e)
{ e.preventDefault(); }); // prevent text selection
self.railh && self.bind(self.railh, "mousedown", function (e)
{ e.preventDefault(); });
}
if (opt.enablemousewheel) {
if (!self.isiframe) self.mousewheel((cap.isie && self.ispage) ? _doc :
self.win, self.onmousewheel);
self.mousewheel(self.rail, self.onmousewheel);
if (self.railh) self.mousewheel(self.railh, self.onmousewheelhr);
}
//Thanks to http://www.quirksmode.org !!
self.onkeypress = function (e) {
if (self.railslocked && self.page.maxh === 0) return true;
e = e || _win.event;
var tg = self.getTarget(e);
if (tg && /INPUT|TEXTAREA|SELECT|OPTION/.test(tg.nodeName)) {
var tp = tg.getAttribute('type') || tg.type || false;
if ((!tp) || !(/submit|button|cancel/i.tp)) return true;
}
if (opt.enableobserver) {
//
self.lazyResize(30);
if (this.doc[0].nodeName == 'IFRAME') {
var oniframeload = function () {
self.iframexd = false;
var doc;
try {
doc = 'contentDocument' in this ? this.contentDocument :
this.contentWindow._doc;
var a = doc.domain;
} catch (e) {
self.iframexd = true;
doc = false;
}
if (self.iframexd) {
if ("console" in _win) console.log('NiceScroll error: policy restriced
iframe');
return true; //cross-domain - I can't manage this
}
self.forcescreen = true;
if (self.isiframe) {
self.iframe = {
"doc": $(doc),
"html": self.doc.contents().find('html')[0],
"body": self.doc.contents().find('body')[0]
};
self.getContentSize = function () {
return {
w: Math.max(self.iframe.html.scrollWidth,
self.iframe.body.scrollWidth),
h: Math.max(self.iframe.html.scrollHeight,
self.iframe.body.scrollHeight)
};
};
self.docscroll = $(self.iframe.body);
}
self.css($(self.iframe.body), _scrollyhidden);
if ('contentWindow' in this) {
self.bind(this.contentWindow, "scroll", self.onscroll); //IE8 & minor
} else {
self.bind(doc, "scroll", self.onscroll);
}
if (opt.enablemousewheel) {
self.mousewheel(doc, self.onmousewheel);
}
if (opt.enablekeyboard) self.bind(doc, (cap.isopera) ? "keypress" :
"keydown", self.onkeypress);
if (cap.cantouch) {
self.bind(doc, "touchstart", self.ontouchstart);
self.bind(doc, "touchmove", self.ontouchmove);
}
else if (opt.emulatetouch) {
self.bind(doc, "mousedown", self.ontouchstart);
self.bind(doc, "mousemove", function (e) {
return self.ontouchmove(e, true);
});
if (opt.grabcursorenabled && cap.cursorgrabvalue) self.css($(doc.body),
{
'cursor': cap.cursorgrabvalue
});
}
if (self.zoom) {
if (opt.dblclickzoom) self.bind(doc, 'dblclick', self.doZoom);
if (self.ongesturezoom) self.bind(doc, "gestureend",
self.ongesturezoom);
}
};
};
if (!self.rail.drag || self.rail.drag.pt != 1) {
if (py !== undefined && py !== false) {
self.scroll.y = (py / self.scrollratio.y) | 0;
}
if (px !== undefined) {
self.scroll.x = (px / self.scrollratio.x) | 0;
}
}
self.cursor.css({
height: self.cursorheight,
top: self.scroll.y
});
if (self.cursorh) {
var lx = (self.hasreversehr) ? self.scrollvaluemaxw - self.scroll.x :
self.scroll.x;
self.cursorh.css({
width: self.cursorwidth,
left: (!self.rail.align && self.rail.visibility) ? lx + self.rail.width :
lx
});
self.cursoractive = true;
}
if (self.zoom) self.zoom.stop().css({
opacity: opt.cursoropacitymax
});
};
this.getContentSize =
(self.ispage) ?
function () {
return {
w: Math.max(_doc.body.scrollWidth, _doc.documentElement.scrollWidth),
h: Math.max(_doc.body.scrollHeight, _doc.documentElement.scrollHeight)
};
} : (self.haswrapper) ?
function () {
return {
w: self.doc[0].offsetWidth,
h: self.doc[0].offsetHeight
};
} : function () {
return {
w: self.docscroll[0].scrollWidth,
h: self.docscroll[0].scrollHeight
};
};
self.view = {
w: (self.ispage) ? self.win.width() : self.win[0].clientWidth,
h: (self.ispage) ? self.win.height() : self.win[0].clientHeight
};
if (self.page.maxh === 0) {
self.hideRail();
self.scrollvaluemax = 0;
self.scroll.y = 0;
self.scrollratio.y = 0;
self.cursorheight = 0;
self.setScrollTop(0);
if (self.rail) self.rail.scrollable = false;
} else {
//self.page.maxh -= (opt.railpadding.top + opt.railpadding.bottom); momo
edit
self.rail.scrollable = true;
}
if (self.page.maxw === 0) {
self.hideRailHr();
self.scrollvaluemaxw = 0;
self.scroll.x = 0;
self.scrollratio.x = 0;
self.cursorwidth = 0;
self.setScrollLeft(0);
if (self.railh) {
self.railh.scrollable = false;
}
} else {
//self.page.maxw -= (opt.railpadding.left + opt.railpadding.right); momo
edit
if (self.railh) self.railh.scrollable = (opt.horizrailenabled);
}
if (!self.hidden) {
if (!self.rail.visibility) self.showRail();
if (self.railh && !self.railh.visibility) self.showRailHr();
}
if (self.railh) {
self.railh.width = (self.page.maxh > 0) ? (self.view.w - self.rail.width) :
self.view.w;
self.scrollvaluemaxw = self.railh.width - self.cursorwidth -
(opt.railpadding.left + opt.railpadding.right);
}
if (!self.ispage) self.updateScrollBar(self.view);
self.scrollratio = {
x: (self.page.maxw / self.scrollvaluemaxw),
y: (self.page.maxh / self.scrollvaluemax)
};
var sy = self.getScrollTop();
if (sy > self.page.maxh) {
self.doScrollTop(self.page.maxh);
} else {
self.scroll.y = (self.getScrollTop() / self.scrollratio.y) | 0;
self.scroll.x = (self.getScrollLeft() / self.scrollratio.x) | 0;
if (self.cursoractive) self.noticeCursor();
}
return self;
};
this.resize = self.onResize;
var hlazyresize = 0;
this.onscreenresize = function(e) {
clearTimeout(hlazyresize);
hlazyresize = setTimeout(function () {
if (self) {
if (hiderails) self.showRails();
self.resize();
}
hlazyresize=0;
}, 120);
};
clearTimeout(hlazyresize);
hlazyresize = setTimeout(function () {
self && self.resize();
hlazyresize=0;
}, tm);
return self;
};
// derived by MDN
https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/wheel
function _modernWheelEvent(dom, name, fn, bubble) {
self._bind(dom, name, function (e) {
e = e || _win.event;
var event = {
original: e,
target: e.target || e.srcElement,
type: "wheel",
deltaMode: e.type == "MozMousePixelScroll" ? 0 : 1,
deltaX: 0,
deltaZ: 0,
preventDefault: function () {
e.preventDefault ? e.preventDefault() : e.returnValue = false;
return false;
},
stopImmediatePropagation: function () {
(e.stopImmediatePropagation) ? e.stopImmediatePropagation() :
e.cancelBubble = true;
}
};
if (name == "mousewheel") {
e.wheelDeltaX && (event.deltaX = -1 / 40 * e.wheelDeltaX);
e.wheelDeltaY && (event.deltaY = -1 / 40 * e.wheelDeltaY);
!event.deltaY && !event.deltaX && (event.deltaY = -1 / 40 *
e.wheelDelta);
} else {
event.deltaY = e.detail;
}
this.jqbind = function (dom, name, fn) { // use jquery bind for non-native
events (mouseenter/mouseleave)
self.events.push({
e: dom,
n: name,
f: fn,
q: true
});
$(dom).on(name, fn);
};
// thanks to
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
try { var options = Object.defineProperty({}, "passive", { get: function () {
passiveSupported = !0; } }); _win.addEventListener("test", null, options); } catch
(err) { }
this.cancelEvent = function(e) {
if (e.cancelable) e.preventDefault();
e.stopImmediatePropagation();
if (e.preventManipulation) e.preventManipulation(); // IE10+
return false;
};
} else {
Event.prototype.preventDefault = function () {
this.returnValue = false;
};
Event.prototype.stopPropagation = function () {
this.cancelBubble = true;
};
_win.constructor.prototype.addEventListener =
_doc.constructor.prototype.addEventListener = Element.prototype.addEventListener =
function (type, listener, useCapture) {
this.attachEvent("on" + type, listener);
};
_win.constructor.prototype.removeEventListener =
_doc.constructor.prototype.removeEventListener =
Element.prototype.removeEventListener = function (type, listener, useCapture) {
this.detachEvent("on" + type, listener);
};
// Thanks to http://www.switchonthecode.com !!
this.cancelEvent = function (e) {
e = e || _win.event;
if (e) {
e.cancelBubble = true;
e.cancel = true;
e.returnValue = false;
}
return false;
};
if (!de) {
de = {
a: [],
l: [],
f: function (e) {
var lst = de.l, l = lst.length - 1;
var r = false;
for (var a = l; a >= 0; a--) {
r = lst[a].call(e.target, e);
if (r === false) return false;
}
return r;
}
};
delegatevents[name] = de;
if (self.ispage) {
de.a = [self.id].concat(de.a);
de.l = [fn].concat(de.l);
} else {
de.a.push(self.id);
de.l.push(fn);
}
};
self.events.push({
e: el,
n: name,
f: fn,
b: bubble,
q: false
});
this.unbindAll = function () {
for (var a = 0; a < self.events.length; a++) {
var r = self.events[a];
(r.q) ? r.e.unbind(r.n, r.f) : self._unbind(r.e, r.n, r.f, r.b);
}
};
this.showRails = function () {
return self.showRail().showRailHr();
};
this.showRail = function () {
if ((self.page.maxh !== 0) && (self.ispage || self.win.css('display') !=
'none')) {
//self.visibility = true;
self.rail.visibility = true;
self.rail.css('display', 'block');
}
return self;
};
this.showRailHr = function () {
if (self.railh) {
if ((self.page.maxw !== 0) && (self.ispage || self.win.css('display') !=
'none')) {
self.railh.visibility = true;
self.railh.css('display', 'block');
}
}
return self;
};
this.hideRails = function () {
return self.hideRail().hideRailHr();
};
this.hideRail = function () {
//self.visibility = false;
self.rail.visibility = false;
self.rail.css('display', 'none');
return self;
};
this.hideRailHr = function () {
if (self.railh) {
self.railh.visibility = false;
self.railh.css('display', 'none');
}
return self;
};
this.show = function () {
self.hidden = false;
self.railslocked = false;
return self.showRails();
};
this.hide = function () {
self.hidden = true;
self.railslocked = true;
return self.hideRails();
};
this.toggle = function () {
return (self.hidden) ? self.show() : self.hide();
};
this.remove = function () {
self.stop();
if (self.cursortimeout) clearTimeout(self.cursortimeout);
for (var n in self.delaylist) if (self.delaylist[n])
clearAnimationFrame(self.delaylist[n].h);
self.doZoomOut();
self.unbindAll();
if (cap.isie9) self.win[0].detachEvent("onpropertychange",
self.onAttributeChange); //IE9 DOMAttrModified bug
self.events = null;
if (self.cursor) {
self.cursor.remove();
}
if (self.cursorh) {
self.cursorh.remove();
}
if (self.rail) {
self.rail.remove();
}
if (self.railh) {
self.railh.remove();
}
if (self.zoom) {
self.zoom.remove();
}
for (var a = 0; a < self.saved.css.length; a++) {
var d = self.saved.css[a];
d[0].css(d[1], (d[2] === undefined) ? '' : d[2]);
}
self.saved = false;
self.me.data('__nicescroll', ''); //erase all traces
self = null;
};
if (self.onscrollstart) {
var info = {
type: "scrollstart",
current: {
x: cx,
y: cy
},
request: {
x: rx,
y: ry
},
end: {
x: self.newscrollx,
y: self.newscrolly
},
speed: ms
};
self.onscrollstart.call(self, info);
}
};
this.triggerScrollEnd = function () {
if (self.onscrollend) {
var px = self.getScrollLeft();
var py = self.getScrollTop();
var info = {
type: "scrollend",
current: {
x: px,
y: py
},
end: {
x: px,
y: py
}
};
self.onscrollend.call(self, info);
};
if (!self.scrollrunning) {
self.newscrolly = self.getScrollTop();
self.newscrollx = self.getScrollLeft();
scrolltmr = now();
}
px = px * scrollspd | 0;
py = py * scrollspd | 0;
if (px) {
if (iswheel) { // mouse-only
if (px < 0) { // fix apple magic mouse swipe back/forward
if (self.getScrollLeft() >= self.page.maxw) return true;
} else {
if (self.getScrollLeft() <= 0) return true;
}
}
self.lastdeltax -= px;
if (py) {
if (chk) {
if (opt.nativeparentscrolling && chkscroll && !self.ispage && !
self.zoomactive) return true;
var ny = self.view.h >> 1;
if (self.newscrolly < -ny) { self.newscrolly = -ny; py = -1; }
else if (self.newscrolly > self.page.maxh + ny) { self.newscrolly =
self.page.maxh + ny; py = 1; }
else py = 0;
}
self.lastdeltay -= py;
if (py || px) {
self.synched("relativexy", function () {
});
}
if (hr && opt.oneaxismousemode && (px === 0) && py) { // classic vertical-
only mousewheel + browser with x/y support
px = py;
py = 0;
if (chkscroll) {
var hrend = (px < 0) ? (self.getScrollLeft() >= self.page.maxw) :
(self.getScrollLeft() <= 0);
if (hrend) { // preserve vertical scrolling
py = px;
px = 0;
}
}
if (chk) {
if (chkscroll) hasparentscrollingphase = true;
} else {
hasparentscrollingphase = false;
e.stopImmediatePropagation();
return e.preventDefault();
}
var nw = now();
var chk = false;
if (opt.preservenativescrolling && ((self.checkarea + 600) < nw)) {
self.nativescrollingarea = self.isScrollable(e);
chk = true;
}
self.checkarea = nw;
if (self.nativescrollingarea) return true; // this isn't my business
var ret = execScrollWheel(e, false, chk);
if (ret) self.checkarea = 0;
return ret;
};
var nw = now();
var chk = false;
if (opt.preservenativescrolling && ((self.checkarea + 600) < nw)) {
self.nativescrollingarea = self.isScrollable(e);
chk = true;
}
self.checkarea = nw;
if (self.nativescrollingarea) return true; // this is not my business
if (self.railslocked) return self.cancelEvent(e);
this.stop = function () {
self.cancelScroll();
if (self.scrollmon) self.scrollmon.stop();
self.cursorfreezed = false;
self.scroll.y = Math.round(self.getScrollTop() * (1 / self.scrollratio.y));
self.noticeCursor();
return self;
};
};
if (!opt.smoothscroll) {
this.doScrollLeft = function (x, spd) { //direct
var y = self.getScrollTop();
self.doScrollPos(x, y, spd);
};
this.doScrollTop = function (y, spd) { //direct
var x = self.getScrollLeft();
self.doScrollPos(x, y, spd);
};
this.doScrollPos = function (x, y, spd) { //direct
var nx = (x > self.page.maxw) ? self.page.maxw : x;
if (nx < 0) nx = 0;
var ny = (y > self.page.maxh) ? self.page.maxh : y;
if (ny < 0) ny = 0;
self.synched('scroll', function () {
self.setScrollTop(ny);
self.setScrollLeft(nx);
});
};
this.cancelScroll = function () { }; // direct
this.resetTransition = function () {
lasttransitionstyle = '';
self.doc.css(cap.prefixstyle + 'transition-duration', '0ms');
};
this.cursorupdate = {
running: false,
start: function () {
var m = this;
if (m.running) return;
m.running = true;
setAnimationFrame(loop);
},
stop: function () {
this.running = false;
}
};
var py = self.getScrollTop();
var px = self.getScrollLeft();
if (!opt.bouncescroll) {
if (y < 0) y = 0;
else if (y > self.page.maxh) y = self.page.maxh;
if (x < 0) x = 0;
else if (x > self.page.maxw) x = self.page.maxw;
} else {
if (y < 0) y = y / 2 | 0;
else if (y > self.page.maxh) y = self.page.maxh + (y - self.page.maxh) /
2 | 0;
if (x < 0) x = x / 2 | 0;
else if (x > self.page.maxw) x = self.page.maxw + (x - self.page.maxw) /
2 | 0;
}
self.newscrolly = y;
self.newscrollx = x;
var ms = self.prepareTransition(dd);
if (!self.scrollrunning) {
self.scrollrunning = true;
self.triggerScrollStart(lft, top, x, y, ms);
self.cursorupdate.start();
}
self.scrollendtrapped = true;
if (!cap.transitionend) {
if (self.scrollendtrapped) clearTimeout(self.scrollendtrapped);
self.scrollendtrapped = setTimeout(self.onScrollTransitionEnd, ms); //
simulate transitionend event
}
self.setScrollTop(self.newscrolly);
self.setScrollLeft(self.newscrollx);
};
this.cancelScroll = function () {
if (!self.scrollendtrapped) return true;
var py = self.getScrollTop();
var px = self.getScrollLeft();
self.scrollrunning = false;
if (!cap.transitionend) clearTimeout(cap.transitionend);
self.scrollendtrapped = false;
self.resetTransition();
self.setScrollTop(py); // fire event onscroll
if (self.railh) self.setScrollLeft(px);
if (self.timerscroll && self.timerscroll.tm)
clearInterval(self.timerscroll.tm);
self.timerscroll = false;
self.cursorfreezed = false;
self.cursorupdate.stop();
self.showCursor(py, px);
return self;
};
this.onScrollTransitionEnd = function () {
if (!self.scrollendtrapped) return;
var py = self.getScrollTop();
var px = self.getScrollLeft();
if (py < 0) py = 0;
else if (py > self.page.maxh) py = self.page.maxh;
if (px < 0) px = 0;
else if (px > self.page.maxw) px = self.page.maxw;
if ((py != self.newscrolly) || (px != self.newscrollx)) return
self.doScrollPos(px, py, opt.snapbackspeed);
if (self.scrollrunning) self.triggerScrollEnd();
self.scrollrunning = false;
self.scrollendtrapped = false;
self.resetTransition();
self.timerscroll = false;
self.setScrollTop(py); // fire event onscroll
if (self.railh) self.setScrollLeft(px); // fire event onscroll left
self.cursorupdate.stop();
self.noticeCursor(false, py, px);
self.cursorfreezed = false;
};
} else {
var py = self.getScrollTop();
var px = self.getScrollLeft();
if (!self.bouncescroll || !self.rail.visibility) {
if (y < 0) {
y = 0;
clipped = true;
} else if (y > self.page.maxh) {
y = self.page.maxh;
clipped = true;
}
}
if (!self.bouncescroll || !self.railh.visibility) {
if (x < 0) {
x = 0;
clipped = true;
} else if (x > self.page.maxw) {
x = self.page.maxw;
clipped = true;
}
}
self.newscrolly = y;
self.newscrollx = x;
self.dst = {};
self.dst.x = x - px;
self.dst.y = y - py;
self.dst.px = px;
self.dst.py = py;
self.bzscroll = {};
if (!self.scrollrunning) return;
var x = self.bzscroll.y.getPos();
self.setScrollLeft(self.bzscroll.x.getNow());
self.setScrollTop(self.bzscroll.y.getNow());
if (x <= 1) {
self.timer = setAnimationFrame(loop);
} else {
self.scrollrunning = false;
self.timer = 0;
self.triggerScrollEnd();
}
};
if (!self.scrollrunning) {
self.triggerScrollStart(px, py, x, y, ms);
self.scrollrunning = true;
self.timer = setAnimationFrame(loop);
}
};
this.cancelScroll = function () {
if (self.timer) clearAnimationFrame(self.timer);
self.timer = 0;
self.bzscroll = false;
self.scrollrunning = false;
return self;
};
this.checkContentSize = function () {
var pg = self.getContentSize();
if ((pg.h != self.page.h) || (pg.w != self.page.w)) self.resize(false, pg);
};
self.zoomrestore = {
style: {}
};
var lst = ['position', 'top', 'left', 'zIndex', 'backgroundColor',
'marginTop', 'marginBottom', 'marginLeft', 'marginRight'];
var win = self.win[0].style;
for (var a in lst) {
var pp = lst[a];
self.zoomrestore.style[pp] = (win[pp] !== undefined) ? win[pp] : '';
}
self.zoomrestore.style.width = self.win.css('width');
self.zoomrestore.style.height = self.win.css('height');
self.zoomrestore.padding = {
w: self.win.outerWidth() - self.win.width(),
h: self.win.outerHeight() - self.win.height()
};
if (cap.isios4) {
self.zoomrestore.scrollTop = $window.scrollTop();
$window.scrollTop(0);
}
self.win.css({
position: (cap.isios4) ? "absolute" : "fixed",
top: 0,
left: 0,
zIndex: globalmaxzindex + 100,
margin: 0
});
var bkg = self.win.css("backgroundColor");
if ("" === bkg || /transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg))
self.win.css("backgroundColor", "#fff");
self.rail.css({
zIndex: globalmaxzindex + 101
});
self.zoom.css({
zIndex: globalmaxzindex + 102
});
self.zoom.css('backgroundPosition', '0 -18px');
self.resizeZoom();
if (self.onzoomin) self.onzoomin.call(self);
return self.cancelEvent(e);
};
self.win.css("margin", "");
self.win.css(self.zoomrestore.style);
if (cap.isios4) {
$window.scrollTop(self.zoomrestore.scrollTop);
}
self.rail.css({
"z-index": self.zindex
});
self.zoom.css({
"z-index": self.zindex
});
self.zoomrestore = false;
self.zoom.css('backgroundPosition', '0 0');
self.onResize();
if (self.onzoomout) self.onzoomout.call(self);
return self.cancelEvent(e);
};
this.resizeZoom = function () {
if (!self.zoomactive) return;
self.setScrollTop(Math.min(self.page.maxh, py));
};
this.init();
$.nicescroll.push(this);
};
this.lastx = 0;
this.lasty = 0;
this.speedx = 0;
this.speedy = 0;
this.lasttime = 0;
this.steptime = 0;
this.snapx = false;
this.snapy = false;
this.demulx = 0;
this.demuly = 0;
this.lastscrollx = -1;
this.lastscrolly = -1;
this.chkx = 0;
this.chky = 0;
this.timer = 0;
this.stop = function () {
self.nc.unsynched("domomentum2d");
if (self.timer) clearTimeout(self.timer);
self.timer = 0;
self.lastscrollx = -1;
self.lastscrolly = -1;
};
if (ny < 0) {
ny = 0;
snap = true;
} else if (ny > self.nc.page.maxh) {
ny = self.nc.page.maxh;
snap = true;
}
if (nx < 0) {
nx = 0;
snap = true;
} else if (nx > self.nc.page.maxw) {
nx = self.nc.page.maxw;
snap = true;
}
var sl = self.nc.getScrollLeft();
var st = self.nc.getScrollTop();
if ((st < 0) || (st > pageh) || (sl < 0) || (sl > pagew)) chk = false;
if (sy || sx) {
var tm = Math.max(16, self.steptime); //timeout granularity
self.demulxy = 0;
self.lastscrollx = self.nc.getScrollLeft();
self.chkx = self.lastscrollx;
self.lastscrolly = self.nc.getScrollTop();
self.chky = self.lastscrolly;
var nx = self.lastscrollx;
var ny = self.lastscrolly;
if (self.speedx) {
nx = Math.floor(self.lastscrollx - (self.speedx * (1 - self.demulxy)));
self.lastscrollx = nx;
if ((nx < 0) || (nx > pagew)) df = 0.10;
}
if (self.speedy) {
ny = Math.floor(self.lastscrolly - (self.speedy * (1 - self.demulxy)));
self.lastscrolly = ny;
if ((ny < 0) || (ny > pageh)) df = 0.10;
}
self.nc.synched("domomentum2d", function () {
if (self.speedx) {
var scx = self.nc.getScrollLeft();
// if (scx != self.chkx) self.stop();
self.chkx = nx;
self.nc.setScrollLeft(nx);
}
if (self.speedy) {
var scy = self.nc.getScrollTop();
// if (scy != self.chky) self.stop();
self.chky = ny;
self.nc.setScrollTop(ny);
}
if (!self.timer) {
self.nc.hideCursor();
self.doSnapy(nx, ny);
}
});
if (self.demulxy < 1) {
self.timer = setTimeout(onscroll, tm);
} else {
self.stop();
self.nc.hideCursor();
self.doSnapy(nx, ny);
}
};
onscroll();
} else {
self.doSnapy(self.nc.getScrollLeft(), self.nc.getScrollTop());
}
};
};
jQuery.cssHooks.pageYOffset = {
get: function (elem, computed, extra) {
var nice = $.data(elem, '__nicescroll') || false;
return (nice && nice.ishwscroll) ? nice.getScrollTop() :
_scrollTop.call(elem);
},
set: function (elem, value) {
var nice = $.data(elem, '__nicescroll') || false;
(nice && nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) :
_scrollTop.call(elem, value);
return this;
}
};
$.cssHooks.pageXOffset = {
get: function (elem, computed, extra) {
var nice = $.data(elem, '__nicescroll') || false;
return (nice && nice.ishwscroll) ? nice.getScrollLeft() :
_scrollLeft.call(elem);
},
set: function (elem, value) {
var nice = $.data(elem, '__nicescroll') || false;
(nice && nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) :
_scrollLeft.call(elem, value);
return this;
}
};
if (doms) {
for (var a = 0; a < doms.length; a++) {
var nice = $.data(doms[a], '__nicescroll') || false;
if (nice) {
this[this.length] = nice;
this.length++;
}
}
}
return this;
};
this.each(function () {
var $this = $(this);
if (wrapper || false) {
var wrp = $(wrapper);
opt.doc = (wrp.length > 1) ? $(wrapper, $this) : wrp;
opt.win = $this;
}
var docundef = !("doc" in opt);
if (!docundef && !("win" in opt)) opt.win = $this;
_win.NiceScroll = {
getjQuery: function () {
return jQuery;
}
};
if (!$.nicescroll) {
$.nicescroll = new NiceScrollArray();
$.nicescroll.options = _globaloptions;
}
}));