UNPKG

@webevt/jquery.nicescroll

Version:

Nicescroll is a jquery plugin, for nice customizabled scrollbars with a very similar ios/mobile style. It supports DIVs, IFrames and document page (body) scrollbars. Compatible with Firefox 4+, Chrome 5+, Safari 4+ (win/mac), Opera 10+, IE 6+ (all A-grade

1,459 lines (1,220 loc) 122 kB
/* jquery.nicescroll -- version 3.6.6 -- copyright 2015-11-17 InuYaksa*2015 -- licensed under the MIT -- -- http://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; var mousefocus = false; var tabindexcounter = 0; var ascrailcounter = 2000; var globalmaxzindex = 0; var $ = jQuery; // sandbox // http://stackoverflow.com/questions/2161159/get-script-path function getScriptPath() { var scripts = document.getElementsByTagName('script'); var path = scripts.length ? scripts[scripts.length - 1].src.split('?')[0] : ''; return (path.split('/').length > 0) ? path.split('/').slice(0, -1).join('/') + '/' : ''; } var vendors = ['webkit','ms','moz','o']; var setAnimationFrame = window.requestAnimationFrame || false; var clearAnimationFrame = window.cancelAnimationFrame || false; if (!setAnimationFrame) { // legacy detection for (var vx in vendors) { var v = vendors[vx]; if (!setAnimationFrame) setAnimationFrame = window[v + 'RequestAnimationFrame']; if (!clearAnimationFrame) clearAnimationFrame = window[v + 'CancelAnimationFrame'] || window[v + 'CancelRequestAnimationFrame']; } } var ClsMutationObserver = window.MutationObserver || window.WebKitMutationObserver || false; var _globaloptions = { zindex: "auto", cursoropacitymin: 0, cursoropacitymax: 1, cursorcolor: "#424242", cursorwidth: "5px", cursorborder: "1px solid #fff", cursorborderradius: "5px", scrollspeed: 60, mousescrollstep: 8 * 3, touchbehavior: false, 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 }; var browserdetected = false; var getBrowserDetection = function() { if (browserdetected) return browserdetected; var _el = document.createElement('DIV'), _style = _el.style, _agent = navigator.userAgent, _platform = navigator.platform, d = {}; d.haspointerlock = "pointerLockElement" in document || "webkitPointerLockElement" in document || "mozPointerLockElement" in document; d.isopera = ("opera" in window); // 12- d.isopera12 = (d.isopera && ("getUserMedia" in navigator)); d.isoperamini = (Object.prototype.toString.call(window.operamini) === "[object OperaMini]"); d.isie = (("all" in document) && ("attachEvent" in _el) && !d.isopera); //IE10- d.isieold = (d.isie && !("msInterpolationMode" in _style)); // IE6 and older d.isie7 = d.isie && !d.isieold && (!("documentMode" in document) || (document.documentMode == 7)); d.isie8 = d.isie && ("documentMode" in document) && (document.documentMode == 8); d.isie9 = d.isie && ("performance" in window) && (document.documentMode >= 9); d.isie10 = d.isie && ("performance" in window) && (document.documentMode == 10); d.isie11 = ("msRequestFullscreen" in _el) && (document.documentMode >= 11); // IE11+ d.isieedge = (navigator.userAgent.match(/Edge\/12\./)); d.isie9mobile = /iemobile.9/i.test(_agent); //wp 7.1 mango if (d.isie9mobile) d.isie9 = false; d.isie7mobile = (!d.isie9mobile && d.isie7) && /iemobile/i.test(_agent); //wp 7.0 d.ismozilla = ("MozAppearance" in _style); d.iswebkit = ("WebkitAppearance" in _style); d.ischrome = ("chrome" in window); d.ischrome22 = (d.ischrome && d.haspointerlock); d.ischrome26 = (d.ischrome && ("transition" in _style)); // issue with transform detection (maintain prefix) d.cantouch = ("ontouchstart" in document.documentElement) || ("ontouchstart" in window); // detection for Chrome Touch Emulation d.hasmstouch = (window.MSPointerEvent || false); // IE10 pointer events d.hasw3ctouch = (window.PointerEvent || false) && ((navigator.MaxTouchPoints > 0)||(navigator.msMaxTouchPoints > 0)); //IE11 pointer events, following W3C Pointer Events spec d.ismac = /^mac$/i.test(_platform); d.isios = (d.cantouch && /iphone|ipad|ipod/i.test(_platform)); d.isios4 = ((d.isios) && !("seal" in Object)); d.isios7 = ((d.isios)&&("webkitHidden" in document)); //iOS 7+ d.isandroid = (/android/i.test(_agent)); d.haseventlistener = ("addEventListener" in _el); d.trstyle = false; d.hastransform = false; d.hastranslate3d = false; d.transitionstyle = false; d.hastransition = false; d.transitionend = false; var a; var check = ['transform', 'msTransform', 'webkitTransform', 'MozTransform', 'OTransform']; for (a = 0; a < check.length; a++) { if (typeof _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 = false; d.prefixstyle = ''; d.transitionend = false; check = ['transition', 'webkitTransition', 'msTransition', 'MozTransition', 'OTransition', 'OTransition', 'KhtmlTransition']; var prefix = ['', '-webkit-', '-ms-', '-moz-', '-o-', '-o', '-khtml-']; var evs = ['transitionend', 'webkitTransitionEnd', 'msTransitionEnd', 'transitionend', 'otransitionend', 'oTransitionEnd', 'KhtmlTransitionEnd']; for (a = 0; a < check.length; a++) { if (check[a] in _style) { d.transitionstyle = check[a]; d.prefixstyle = prefix[a]; d.transitionend = evs[a]; break; } } if (d.ischrome26) { // always use prefix d.prefixstyle = prefix[1]; } d.hastransition = (d.transitionstyle); function detectCursorGrab() { var lst = ['-webkit-grab', '-moz-grab', 'grab']; if ((d.ischrome && !d.ischrome22) || d.isie) lst = []; // force setting for IE returns false positive and chrome cursor bug for (var a = 0; a < lst.length; a++) { var p = lst[a]; _style.cursor = p; if (_style.cursor == p) return p; } return 'url(//mail.google.com/mail/images/2/openhand.cur),n-resize'; // thank you google for custom cursor! } d.cursorgrabvalue = detectCursorGrab(); d.hasmousecapture = ("setCapture" in _el); d.hasMutationObserver = (ClsMutationObserver !== false); _el = null; //memory released browserdetected = d; return d; }; var NiceScrollClass = function(myopt, me) { var self = this; this.version = '3.6.6'; this.name = 'nicescroll'; this.me = me; this.opt = { doc: $("body"), win: false }; $.extend(this.opt, _globaloptions); // clone opts // Options for internal use this.opt.snapbackspeed = 80; if (myopt || false) { for (var a in self.opt) { if (typeof myopt[a] != "undefined") self.opt[a] = myopt[a]; } } this.doc = self.opt.doc; this.iddoc = (this.doc && this.doc[0]) ? this.doc[0].id || '' : ''; this.ispage = /^BODY|HTML/.test((self.opt.win) ? self.opt.win[0].nodeName : this.doc[0].nodeName); this.haswrapper = (self.opt.win !== false); this.win = self.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')); this.istextarea = (this.win[0].nodeName == 'TEXTAREA'); this.forcescreen = false; //force to use screen position on events this.canshowonmouseevent = (self.opt.autohidemode != "scroll"); // Events jump table this.onmousedown = false; this.onmouseup = false; this.onmousemove = false; this.onmousewheel = false; this.onkeypress = false; this.ongesturezoom = false; this.onclick = false; // Nicescroll custom events this.onscrollstart = false; this.onscrollend = false; this.onscrollcancel = false; this.onzoomin = false; this.onzoomout = false; // 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; this.isrtlmode = (this.opt.rtlmode == "auto") ? ((this.win[0] == window ? this.body : this.win).css("direction") == "rtl") : (this.opt.rtlmode === true); // this.checkrtlmode = false; this.scrollrunning = false; this.scrollmom = false; this.observer = false; // observer div changes this.observerremover = false; // observer on parent for remove detection this.observerbody = false; // observer on body for position change do { this.id = "ascrail" + (ascrailcounter++); } while (document.getElementById(this.id)); 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.wheelprevented = false; //prevent mousewheel event this.overflowx = self.opt.overflowx; this.overflowy = self.opt.overflowy; this.nativescrollingarea = false; this.checkarea = 0; this.events = []; // event list for unbind this.saved = {}; // style saved this.delaylist = {}; this.synclist = {}; this.lastdeltax = 0; this.lastdeltay = 0; this.detected = getBrowserDetection(); var cap = $.extend({}, this.detected); this.canhwscroll = (cap.hastransform && self.opt.hwacceleration); this.ishwscroll = (this.canhwscroll && self.haswrapper); this.hasreversehr = (this.isrtlmode&&!cap.iswebkit); //RTL mode with reverse horizontal axis this.istouchcapable = false; // desktop devices with touch screen support //## Check WebKit-based desktop with touch support //## + Firefox 18 nightly build (desktop) false positive (or desktop with touch support) if (cap.cantouch && !cap.isios && !cap.isandroid && (cap.iswebkit || cap.ismozilla)) { this.istouchcapable = true; cap.cantouch = false; // parse normal desktop events } //## disable MouseLock API on user request if (!self.opt.enablemouselockapi) { cap.hasmousecapture = false; cap.haspointerlock = false; } /* deprecated this.delayed = function(name, fn, tm, lazy) { }; */ this.debounced = function(name, fn, tm) { var dd = self.delaylist[name]; self.delaylist[name] = fn; if (!dd) { self.debouncedelayed = setTimeout(function() { if (!self) return; var fn = self.delaylist[name]; self.delaylist[name] = false; fn.call(self); }, tm); } }; var _onsync = false; this.synched = function(name, fn) { function requestSync() { if (_onsync) return; setAnimationFrame(function() { _onsync = false; for (var nn in self.synclist) { var fn = self.synclist[nn]; if (fn) fn.call(self); self.synclist[nn] = false; } }); _onsync = true; } self.synclist[name] = fn; requestSync(); return name; }; this.unsynched = function(name) { if (self.synclist[name]) self.synclist[name] = false; }; this.css = function(el, pars) { // save & set for (var n in pars) { self.saved.css.push([el, n, el.css(n)]); el.css(n, pars[n]); } }; this.scrollTop = function(val) { return (typeof val == "undefined") ? self.getScrollTop() : self.setScrollTop(val); }; this.scrollLeft = function(val) { return (typeof val == "undefined") ? self.getScrollLeft() : self.setScrollLeft(val); }; // derived by by Dan Pupius www.pupius.net var BezierClass = function(st, ed, spd, p1, p2, p3, p4) { 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 = (new Date()).getTime(); this.df = this.ed - this.st; }; BezierClass.prototype = { B2: function(t) { return 3 * t * t * (1 - t); }, B3: function(t) { return 3 * t * (1 - t) * (1 - t); }, B4: function(t) { return (1 - t) * (1 - t) * (1 - t); }, getNow: function() { var nw = (new Date()).getTime(); var pc = 1 - ((nw - this.ts) / this.spd); var bz = this.B2(pc) + this.B3(pc) + this.B4(pc); return (pc < 0) ? this.ed : this.st + Math.round(this.df * bz); }, update: function(ed, spd) { this.st = this.getNow(); this.ed = ed; this.spd = spd; this.ts = (new Date()).getTime(); this.df = this.ed - this.st; return this; } }; //derived from http://stackoverflow.com/questions/11236090/ function getMatrixValues() { var tr = self.doc.css(cap.trstyle); if (tr && (tr.substr(0, 6) == "matrix")) { return tr.replace(/^.*\((.*)\)$/g, "$1").replace(/px/g, '').split(/, +/); } return false; } if (this.ishwscroll) { // hw accelerated scroll 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(last) { if (!last) { var mtx = getMatrixValues(); if (mtx) return (mtx.length == 16) ? -mtx[13] : -mtx[5]; //matrix3d 16 on IE10 if (self.timerscroll && self.timerscroll.bz) return self.timerscroll.bz.getNow(); } return self.doc.translate.y; }; this.getScrollLeft = function(last) { if (!last) { var mtx = getMatrixValues(); if (mtx) return (mtx.length == 16) ? -mtx[12] : -mtx[4]; //matrix3d 16 on IE10 if (self.timerscroll && self.timerscroll.bh) return self.timerscroll.bh.getNow(); } return self.doc.translate.x; }; this.notifyScrollEvent = function(el) { var e = document.createEvent("UIEvents"); e.initUIEvent("scroll", false, true, window, 1); e.niceevent = true; el.dispatchEvent(e); }; var cxscrollleft = (this.isrtlmode) ? 1 : -1; if (cap.hastranslate3d && self.opt.enabletranslate3d) { this.setScrollTop = function(val, silent) { self.doc.translate.y = val; self.doc.translate.ty = (val * -1) + "px"; self.doc.css(cap.trstyle, "translate3d(" + self.doc.translate.tx + "," + self.doc.translate.ty + ",0px)"); if (!silent) self.notifyScrollEvent(self.win[0]); }; this.setScrollLeft = function(val, silent) { self.doc.translate.x = val; self.doc.translate.tx = (val * cxscrollleft) + "px"; self.doc.css(cap.trstyle, "translate3d(" + self.doc.translate.tx + "," + self.doc.translate.ty + ",0px)"); if (!silent) self.notifyScrollEvent(self.win[0]); }; } else { this.setScrollTop = function(val, silent) { self.doc.translate.y = val; self.doc.translate.ty = (val * -1) + "px"; self.doc.css(cap.trstyle, "translate(" + self.doc.translate.tx + "," + self.doc.translate.ty + ")"); if (!silent) self.notifyScrollEvent(self.win[0]); }; this.setScrollLeft = function(val, silent) { self.doc.translate.x = val; self.doc.translate.tx = (val * cxscrollleft) + "px"; self.doc.css(cap.trstyle, "translate(" + self.doc.translate.tx + "," + self.doc.translate.ty + ")"); if (!silent) self.notifyScrollEvent(self.win[0]); }; } } else { // native scroll this.getScrollTop = function() { return self.docscroll.scrollTop(); }; this.setScrollTop = function(val) { return setTimeout(function() {self.docscroll.scrollTop(val)}, 1); }; this.getScrollLeft = function() { if (self.detected.ismozilla && self.isrtlmode) return Math.abs(self.docscroll.scrollLeft()); return self.docscroll.scrollLeft(); }; this.setScrollLeft = function(val) { return setTimeout(function() {self.docscroll.scrollLeft((self.detected.ismozilla && self.isrtlmode) ? -val : val)}, 1); }; } this.getTarget = function(e) { if (!e) return false; if (e.target) return e.target; if (e.srcElement) return e.srcElement; return false; }; this.hasParent = function(e, id) { if (!e) return false; var el = e.target || e.srcElement || e || false; while (el && el.id != id) { el = el.parentNode || false; } return (el !== false); }; 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 }; function getWidthToPixel(dom, prop, chkheight) { var wd = dom.css(prop); var px = parseFloat(wd); if (isNaN(px)) { px = _convertBorderWidth[wd] || 0; var brd = (px == 3) ? ((chkheight) ? (self.win.outerHeight() - self.win.innerHeight()) : (self.win.outerWidth() - self.win.innerWidth())) : 1; //DON'T TRUST CSS if (self.isie8 && px) px += 1; return (brd) ? px : 0; } return px; } this.getDocumentScrollOffset = function() { return {top:window.pageYOffset||document.documentElement.scrollTop, left:window.pageXOffset||document.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,// + self.viewport.scrollTop(), left: ww.left - vp.left // + self.viewport.scrollLeft() }; }; this.updateScrollBar = function(len) { if (self.ishwscroll) { self.rail.css({ //** height: self.win.innerHeight() - (self.opt.railpadding.top + self.opt.railpadding.bottom) }); if (self.railh) self.railh.css({ //** width: self.win.innerWidth() - (self.opt.railpadding.left + self.opt.railpadding.right) }); } else { var wpos = self.getOffset(); var pos = { top: wpos.top, left: wpos.left - (self.opt.railpadding.left + self.opt.railpadding.right) }; pos.top += getWidthToPixel(self.win, 'border-top-width', true); pos.left += (self.rail.align) ? self.win.outerWidth() - getWidthToPixel(self.win, 'border-right-width') - self.rail.width : getWidthToPixel(self.win, 'border-left-width'); var off = self.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()) - (self.opt.railpadding.top + self.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.railh && !self.railslocked) { var pos = { top: wpos.top, left: wpos.left }; var off = self.opt.railhoffset; if (!!off) { if (!!off.top) pos.top += off.top; if (!!off.left) pos.left += off.left; } 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); var x = pos.left + getWidthToPixel(self.win, 'border-left-width'); self.railh.css({ top: y - (self.opt.railpadding.top + self.opt.railpadding.bottom), left: x, width: self.railh.width }); } } }; this.doRailClick = function(e, dbl, hr) { var fn, pg, cur, pos; if (self.railslocked) return; self.cancelEvent(e); 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); fn(cur); } 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);// (cur >= pos) ? fn(pg): fn(-pg); } }; self.hasanimationframe = (setAnimationFrame); self.hascancelanimationframe = (clearAnimationFrame); if (!self.hasanimationframe) { setAnimationFrame = function(fn) { return setTimeout(fn, 15 - Math.floor((+new Date()) / 1000) % 16); }; // 1000/60)}; clearAnimationFrame = clearInterval; } else if (!self.hascancelanimationframe) clearAnimationFrame = function() { self.cancelAnimationFrame = true; }; this.init = function() { self.saved.css = []; if (cap.isie7mobile) return true; // SORRY, DO NOT WORK! if (cap.isoperamini) return true; // SORRY, DO NOT WORK! if (cap.hasmstouch) self.css((self.ispage) ? $("html") : self.win, { '-ms-touch-action': 'none' }); self.zindex = "auto"; if (!self.ispage && self.opt.zindex == "auto") { self.zindex = getZIndex() || "auto"; } else { self.zindex = self.opt.zindex; } if (!self.ispage && self.zindex != "auto") { if (self.zindex > globalmaxzindex) globalmaxzindex = self.zindex; } if (self.isie && self.zindex == 0 && self.opt.zindex == "auto") { // fix IE auto == 0 self.zindex = "auto"; } if (!self.ispage || (!cap.cantouch && !cap.isieold && !cap.isie9mobile)) { var cont = self.docscroll; if (self.ispage) cont = (self.haswrapper) ? self.win : self.doc; if (!cap.isie9mobile) self.css(cont, { 'overflow-y': 'hidden' }); if (self.ispage && cap.isie7) { if (self.doc[0].nodeName == 'BODY') self.css($("html"), { 'overflow-y': 'hidden' }); //IE7 double scrollbar issue else if (self.doc[0].nodeName == 'HTML') self.css($("body"), { 'overflow-y': 'hidden' }); //IE7 double scrollbar issue } if (cap.isios && !self.ispage && !self.haswrapper) self.css($("body"), { "-webkit-overflow-scrolling": "touch" }); //force hw acceleration var cursor = $(document.createElement('div')); cursor.css({ position: "relative", top: 0, "float": "right", width: self.opt.cursorwidth, height: "0px", 'background-color': self.opt.cursorcolor, border: self.opt.cursorborder, 'background-clip': 'padding-box', '-webkit-border-radius': self.opt.cursorborderradius, '-moz-border-radius': self.opt.cursorborderradius, 'border-radius': self.opt.cursorborderradius }); cursor.hborder = parseFloat(cursor.outerHeight() - cursor.innerHeight()); cursor.addClass('nicescroll-cursors'); self.cursor = cursor; var rail = $(document.createElement('div')); rail.attr('id', self.id); rail.addClass('nicescroll-rails nicescroll-rails-vr'); var v, a, kp = ["left","right","top","bottom"]; //** for (var n in kp) { a = kp[n]; v = self.opt.railpadding[a]; (v) ? rail.css("padding-"+a,v+"px") : self.opt.railpadding[a] = 0; } rail.append(cursor); rail.width = Math.max(parseFloat(self.opt.cursorwidth), cursor.outerWidth()); rail.css({ width: rail.width + "px", 'zIndex': self.zindex, "background": self.opt.background, cursor: "default" }); rail.visibility = true; rail.scrollable = true; rail.align = (self.opt.railalign == "left") ? 0 : 1; self.rail = rail; self.rail.drag = false; var zoom = false; if (self.opt.boxzoom && !self.ispage && !cap.isieold) { zoom = document.createElement('div'); self.bind(zoom, "click", self.doZoom); self.bind(zoom, "mouseenter", function() { self.zoom.css('opacity', self.opt.cursoropacitymax); }); self.bind(zoom, "mouseleave", function() { self.zoom.css('opacity', self.opt.cursoropacitymin); }); self.zoom = $(zoom); self.zoom.css({ "cursor": "pointer", 'z-index': self.zindex, 'backgroundImage': 'url(' + self.opt.scriptpath + 'zoomico.png)', 'height': 18, 'width': 18, 'backgroundPosition': '0px 0px' }); if (self.opt.dblclickzoom) self.bind(self.win, "dblclick", self.doZoom); if (cap.cantouch && self.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 (self.opt.horizrailenabled) { self.css(cont, { 'overflow-x': 'hidden' }); var cursor = $(document.createElement('div')); cursor.css({ position: "absolute", top: 0, height: self.opt.cursorwidth, width: "0px", 'background-color': self.opt.cursorcolor, border: self.opt.cursorborder, 'background-clip': 'padding-box', '-webkit-border-radius': self.opt.cursorborderradius, '-moz-border-radius': self.opt.cursorborderradius, 'border-radius': self.opt.cursorborderradius }); if (cap.isieold) cursor.css({'overflow':'hidden'}); //IE6 horiz scrollbar issue cursor.wborder = parseFloat(cursor.outerWidth() - cursor.innerWidth()); cursor.addClass('nicescroll-cursors'); self.cursorh = cursor; railh = $(document.createElement('div')); railh.attr('id', self.id + '-hr'); railh.addClass('nicescroll-rails nicescroll-rails-hr'); railh.height = Math.max(parseFloat(self.opt.cursorwidth), cursor.outerHeight()); railh.css({ height: railh.height + "px", 'zIndex': self.zindex, "background": self.opt.background }); railh.append(cursor); railh.visibility = true; railh.scrollable = true; railh.align = (self.opt.railvalign == "top") ? 0 : 1; self.railh = railh; self.railh.drag = false; } // if (self.ispage) { rail.css({ position: "fixed", top: "0px", height: "100%" }); (rail.align) ? rail.css({ right: "0px" }): rail.css({ left: "0px" }); self.body.append(rail); if (self.railh) { railh.css({ position: "fixed", left: "0px", width: "100%" }); (railh.align) ? railh.css({ bottom: "0px" }): railh.css({ top: "0px" }); 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.align) ? rail.css({ right: 0 }): rail.css({ left: 0 }); bd.append(rail); if (railh) { railh.css({ position: "absolute", left: 0, bottom: 0 }); (railh.align) ? railh.css({ bottom: 0 }): railh.css({ top: 0 }); bd.append(railh); } } else { self.isfixed = (self.win.css("position") == "fixed"); var rlpos = (self.isfixed) ? "fixed" : "absolute"; if (!self.isfixed) self.viewport = self.getViewport(self.win[0]); if (self.viewport) { self.body = self.viewport; if ((/fixed|absolute/.test(self.viewport.css("position"))) == false) self.css(self.viewport, { "position": "relative" }); } 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 (cap.isie && self.opt.disableoutline) self.win.attr("hideFocus", "true"); // IE, prevent dotted rectangle on focused div if (cap.iswebkit && self.opt.disableoutline) self.win.css({"outline": "none"}); // Webkit outline //if (cap.isopera&&self.opt.disableoutline) self.win.css({"outline":"0"}); // Opera 12- to test [TODO] } if (self.opt.autohidemode === false) { self.autohidedom = false; self.rail.css({ opacity: self.opt.cursoropacitymax }); if (self.railh) self.railh.css({ opacity: self.opt.cursoropacitymax }); } else if ((self.opt.autohidemode === true) || (self.opt.autohidemode === "leave")) { self.autohidedom = $().add(self.rail); if (cap.isie8) self.autohidedom = self.autohidedom.add(self.cursor); if (self.railh) self.autohidedom = self.autohidedom.add(self.railh); if (self.railh && cap.isie8) self.autohidedom = self.autohidedom.add(self.cursorh); } else if (self.opt.autohidemode == "scroll") { self.autohidedom = $().add(self.rail); if (self.railh) self.autohidedom = self.autohidedom.add(self.railh); } else if (self.opt.autohidemode == "cursor") { self.autohidedom = $().add(self.cursor); if (self.railh) self.autohidedom = self.autohidedom.add(self.cursorh); } else if (self.opt.autohidemode == "hidden") { self.autohidedom = false; self.hide(); self.railslocked = false; } if (cap.isie9mobile) { self.scrollmom = new ScrollMomentumClass2D(self); self.onmangotouch = function() { var py = self.getScrollTop(); var px = self.getScrollLeft(); if ((py == self.scrollmom.lastscrolly) && (px == self.scrollmom.lastscrollx)) return true; var dfy = py - self.mangotouch.sy; var dfx = px - self.mangotouch.sx; var df = Math.round(Math.sqrt(Math.pow(dfx, 2) + Math.pow(dfy, 2))); if (df == 0) return; var dry = (dfy < 0) ? -1 : 1; var drx = (dfx < 0) ? -1 : 1; var tm = +new Date(); if (self.mangotouch.lazy) clearTimeout(self.mangotouch.lazy); if (((tm - self.mangotouch.tm) > 80) || (self.mangotouch.dry != dry) || (self.mangotouch.drx != drx)) { self.scrollmom.stop(); self.scrollmom.reset(px, py); self.mangotouch.sy = py; self.mangotouch.ly = py; self.mangotouch.sx = px; self.mangotouch.lx = px; self.mangotouch.dry = dry; self.mangotouch.drx = drx; self.mangotouch.tm = tm; } else { self.scrollmom.stop(); self.scrollmom.update(self.mangotouch.sx - dfx, self.mangotouch.sy - dfy); self.mangotouch.tm = tm; var ds = Math.max(Math.abs(self.mangotouch.ly - py), Math.abs(self.mangotouch.lx - px)); self.mangotouch.ly = py; self.mangotouch.lx = px; if (ds > 2) { self.mangotouch.lazy = setTimeout(function() { self.mangotouch.lazy = false; self.mangotouch.dry = 0; self.mangotouch.drx = 0; self.mangotouch.tm = 0; self.scrollmom.doMomentum(30); }, 100); } } }; var top = self.getScrollTop(); var lef = self.getScrollLeft(); self.mangotouch = { sy: top, ly: top, dry: 0, sx: lef, lx: lef, drx: 0, lazy: false, tm: 0 }; self.bind(self.docscroll, "scroll", self.onmangotouch); } else { if (cap.cantouch || self.istouchcapable || self.opt.touchbehavior || cap.hasmstouch) { self.scrollmom = new ScrollMomentumClass2D(self); self.ontouchstart = function(e) { if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false; self.hasmoving = false; if (!self.railslocked) { var tg; if (cap.hasmstouch) { tg = (e.target) ? e.target : false; while (tg) { var nc = $(tg).getNiceScroll(); if ((nc.length > 0) && (nc[0].me == self.me)) break; if (nc.length > 0) return false; if ((tg.nodeName == 'DIV') && (tg.id == self.id)) break; tg = (tg.parentNode) ? tg.parentNode : false; } } self.cancelScroll(); 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 (!("clientX" in e) && ("changedTouches" in e)) { e.clientX = e.changedTouches[0].clientX; e.clientY = e.changedTouches[0].clientY; } 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 }; if (self.ispage || !self.opt.directionlockdeadzone) { self.rail.drag.dl = "f"; } else { var view = { w: $(window).width(), h: $(window).height() }; var page = { w: Math.max(document.body.scrollWidth, document.documentElement.scrollWidth), h: Math.max(document.body.scrollHeight, document.documentElement.scrollHeight) }; var maxh = Math.max(0, page.h - view.h); var maxw = Math.max(0, page.w - view.w); if (!self.rail.scrollable && self.railh.scrollable) self.rail.drag.ck = (maxh > 0) ? "v" : false; else if (self.rail.scrollable && !self.railh.scrollable) self.rail.drag.ck = (maxw > 0) ? "h" : false; else self.rail.drag.ck = false; if (!self.rail.drag.ck) self.rail.drag.dl = "f"; } if (self.opt.touchbehavior && self.isiframe && cap.isie) { var wp = self.win.position(); self.rail.drag.x += wp.left; self.rail.drag.y += wp.top; } self.hasmoving = false; self.lastmouseup = false; self.scrollmom.reset(e.clientX, e.clientY); if (!cap.cantouch && !this.istouchcapable && !e.pointerType) { var ip = (tg) ? /INPUT|SELECT|TEXTAREA/i.test(tg.nodeName) : false; if (!ip) { if (!self.ispage && cap.hasmousecapture) tg.setCapture(); if (self.opt.touchbehavior) { 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'))) { pc = { "tg": tg, "click": false }; self.preventclick = pc; } } } }; self.ontouchend = function(e) { if (!self.rail.drag) return true; if (self.rail.drag.pt == 2) { if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false; self.scrollmom.doMomentum(); self.rail.drag = false; if (self.hasmoving) { self.lastmouseup = true; self.hideCursor(); if (cap.hasmousecapture) document.releaseCapture(); if (!cap.cantouch) return self.cancelEvent(e); } } else if (self.rail.drag.pt == 1) { return self.onmouseup(e); } }; var moveneedoffset = (self.opt.touchbehavior && self.isiframe && !cap.hasmousecapture); self.ontouchmove = function(e, byiframe) { if (!self.rail.drag) return false; if (e.targetTouches && self.opt.preventmultitouchscrolling) { if (e.targetTouches.length > 1) return false; // multitouch } if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false; if (self.rail.drag.pt == 2) { if (cap.cantouch && (cap.isios) && (typeof e.original == "undefined")) return true; // prevent ios "ghost" events by clickable elements self.hasmoving = true; if (self.preventclick && !self.preventclick.click) { self.preventclick.click = self.preventclick.tg.onclick || false; self.preventclick.tg.onclick = self.onpreventclick; } var ev = $.extend({ "original": e }, e); e = ev; if (("changedTouches" in e)) { e.clientX = e.changedTouches[0].clientX; e.clientY = e.changedTouches[0].clientY; } if (self.forcescreen) { var le = e; e = { "original": (e.original) ? e.original : e }; e.clientX = le.screenX; e.clientY = le.screenY; } var ofy,ofx; ofx = ofy = 0; if (moveneedoffset && !byiframe) { var wp = self.win.position(); ofx = -wp.left; ofy = -wp.top; } var fy = e.clientY + ofy; var my = (fy - self.rail.drag.y); var fx = e.clientX + ofx; var mx = (fx - self.rail.drag.x); var ny = self.rail.drag.st - my; if (self.ishwscroll && self.opt.bouncescroll) { if (ny < 0) { ny = Math.round(ny / 2); // fy = 0; } else if (ny > self.page.maxh) { ny = self.page.maxh + Math.round((ny - self.page.maxh) / 2); // fy = 0; } } else { if (ny < 0) { ny = 0; fy = 0; } if (ny > self.page.maxh) { ny = self.page.maxh; fy = 0; } } var nx; if (self.railh && self.railh.scrollable) { nx = (self.isrtlmode) ? mx - self.rail.drag.sl : self.rail.drag.sl - mx; if (self.ishwscroll && self.opt.bouncescroll) { if (nx < 0) { nx = Math.round(nx / 2); // fx = 0; } else if (nx > self.page.maxw) { nx = self.page.maxw + Math.round((nx - self.page.maxw) / 2); // fx = 0; } } else { if (nx < 0) { nx = 0; fx = 0; } if (nx > self.page.maxw) { nx = self.page.maxw; fx = 0; } } } var grabbed = false; if (self.rail.drag.dl) { grabbed = true; if (self.rail.drag.dl == "v") nx = self.rail.drag.sl; else if (self.rail.drag.dl == "h") ny = self.rail.drag.st; } else { var ay = Math.abs(my); var ax = Math.abs(mx); var dz = self.opt.directionlockdeadzone; if (self.rail.drag.ck == "v") { if (ay > dz && (ax <= (ay * 0.3))) { self.rail.drag = false; return true; } else if (ax > dz) { self.rail.drag.dl = "f"; $("body").scrollTop($("body").scrollTop()); // stop iOS native scrolling (when active javascript has blocked) } } else if (self.rail.drag.ck == "h") { if (ax > dz && (ay <= (ax * 0.3))) { self.rail.drag = false; return true; } else if (ay > dz) { self.rail.drag.dl = "f"; $("body").scrollLeft($("body").scrollLeft()); // stop iOS native scrolling (when active javascript has blocked) } } } self.synched("touchmove", function() { if (self.rail.drag && (self.rail.drag.pt == 2)) { if (self.prepareTransition) self.prepareTransition(0); 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) document.selection.clear(); } }); if (cap.ischrome && self.istouchcapable) grabbed = false; //chrome touch emulation doesn't like! if (grabbed) return self.cancelEvent(e); } else if (self.rail.drag.pt == 1) { // drag on cursor return self.onmousemove(e); } }; } self.onmousedown = function(e, hronly) { if (self.rail.drag && self.rail.drag.pt != 1) return; if (self.railslocked) return self.cancelEvent(e); self.cancelScroll(); self.rail.drag = { x: e.clientX, y: e.clientY, sx: self.scroll.x, sy: self.scroll.y, pt: 1, hr: (!!hronly) }; var tg = self.getTarget(e); if (!self.ispage && 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); }; self.onmouseup = function(e) { if (self.rail.drag) { if (self.rail.drag.pt != 1) return true; if (cap.hasmousecapture) document.releaseCapture(); if (self.isiframe && !cap.hasmousecapture) self.doc.css("pointer-events", self.saved.csspointerevents); self.rail.drag = false; //if (!self.rail.active) self.hideCursor(); if (self.hasmoving) self.triggerScrollEnd(); // TODO - check &&!self.scrollrunning return self.cancelEvent(e); } }; self.onmousemove = function(e) { if (self.rail.drag) { if (self.rail.drag.pt != 1) return; if (cap.ischrome && e.which == 0) return