12345678910111213141516171819202122232425262728293031323334353637 |
- /* jquery.nicescroll
- -- versione 2.0.0
- -- copyright 2011 InuYaksa*2011
- -- licensed under the MIT
- --
- -- http://areaaperta.com/nicescroll
- -- https://github.com/inuyaksa/jquery.nicescroll
- --
- */
- (function(e){var i=false,j=false,k=function(){var d=document.getElementsByTagName("script"),d=d[d.length-1].src.split("?")[0];return d.split("/").length>0?d.split("/").slice(0,-1).join("/")+"/":""}(),l=function(d){var a=this;this.opt={doc:e("body"),win:false,zindex:9E3,cursoropacitymin:0,cursoropacitymax:1,cursorcolor:"#424242",cursorwidth:"5px",cursorborder:"1px solid #fff",cursorborderradius:"4px",scrollspeed:60,mousescrollstep:48,touchbehavior:false,hwacceleration:true,boxzoom:false,dblclickzoom:true,
- gesturezoom:true};if(d)for(var g in a.opt)d[g]!==void 0&&(a.opt[g]=d[g]);this.id=a.opt.doc[0].id||"";this.doc=a.opt.doc;this.ispage=a.doc[0].nodeName=="BODY"||a.doc[0].nodeName=="HTML";this.haswrapper=a.opt.win!==false;this.win=a.opt.win||(this.ispage?e(window):this.doc);this.docscroll=this.ispage?e(window):this.win;this.isiframe=this.doc[0].nodeName=="IFRAME"&&this.win[0].nodeName=="IFRAME";this.doc[0].nodeName=="IFRAME"&&this.doc.load(function(){var b="contentDocument"in this?this.contentDocument:
- this.contentWindow.document;if(a.isiframe)a.docscroll=e(b.body);a.onResize();e(b.body).css({"overflow-y":"hidden"});e(b).scroll(a.onscroll);e(b).mouseup(function(){a.rail.drag=false});a.bind(b,"mousewheel",a.onmousewheel);e(b).keydown(a.onkeypress);if(a.cantouch||a.opt.touchbehavior)a.bind(b,"mousedown",function(b){a.rail.drag={x:b.pageX,y:b.pageY,sx:a.scroll.x,sy:a.scroll.y,st:a.getScrollTop()};return a.cancelEvent(b)}),a.bind(b,"mouseup",function(b){a.rail.drag=false;return a.cancelEvent(b)}),a.bind(b,
- "mousemove",function(b){if(a.rail.drag)return a.doScrollTo(a.rail.drag.st-(b.pageY-a.rail.drag.y),true),a.cancelEvent(b)});a.zoom&&(a.bind(b,"dblclick",a.doZoom),a.cantouch&&a.opt.gesturezoom&&a.bind(b,"gesturechange",function(b){b.scale>1.5&&a.doZoomIn(b);b.scale<0.8&&a.doZoomOut(b);return a.cancelEvent(b)}))});this.page=this.view=false;this.scroll={x:0,y:0};this.scrollratio={x:0,y:0};this.cursorheight=20;this.scrollvaluemax=0;do this.id="ascrail"+Math.round(Math.random()*99999);while(document.getElementById(this.id));
- this.hasmousefocus=this.hasfocus=this.zoomactive=this.zoom=this.cursorfreezed=this.cursor=this.rail=false;d=document.createElement("DIV");this.isieold=(this.isie=document.all&&!document.opera)&&!("msInterpolationMode"in d.style);this.cantouch="ontouchstart"in document.documentElement;if(a.opt.hwacceleration){if((this.trstyle=window.opera?"OTransform":document.all?"msTransform":d.style.webkitTransform!==void 0?"webkitTransform":d.style.MozTransform!==void 0?"MozTransform":false)&&d.style[this.trstyle]===
- void 0)this.trstyle=false;if(this.hastransform=this.trstyle!=false)d.style[this.trstyle]="translate3d(1px,2px,3px)",this.hastranslate3d=/translate3d/.test(d.style[this.trstyle])}else this.hastransform=this.trstyle=false;this.ishwscroll=a.hastransform&&a.opt.hwacceleration&&a.haswrapper;this.scrollTop=function(b){return b===void 0?a.getScrollTop():a.setScrollTop(b)};this.ishwscroll?(a.doc.translate={x:0,y:0},this.getScrollTop=function(){return a.doc.translate.y},this.setScrollTop=this.hastranslate3d?
- function(b){a.doc.css(a.trstyle,"translate3d(0px,"+b*-1+"px,0px)");a.doc.translate.y=b;document.createEvent?(b=document.createEvent("UIEvents"),b.initUIEvent("scroll",false,true,window,1),a.docscroll[0].dispatchEvent(b)):(b=document.createEventObject(),a.docscroll[0].fireEvent("onscroll"),b.cancelBubble=true)}:function(b){a.doc.css(a.trstyle,"translate(0px,"+b*-1+"px)");a.doc.translate.y=b;document.createEvent?(b=document.createEvent("UIEvents"),b.initUIEvent("scroll",false,true,window,1),a.docscroll[0].dispatchEvent(b)):
- (b=document.createEventObject(),a.docscroll[0].fireEvent("onscroll"),b.cancelBubble=true)}):(this.getScrollTop=function(){return a.docscroll.scrollTop()},this.setScrollTop=function(b){return a.docscroll.scrollTop(b)});this.getTarget=function(a){return!a?false:a.target?a.target:a.srcElement?a.srcElement:false};this.hasParent=function(a,c){if(!a)return false;for(var h=a.target||a.srcElement||a||false;h&&h.id!=c;)h=h.parentNode||false;return h!==false};this.updateScrollBar=function(){if(a.ishwscroll)a.rail.css({height:a.win.innerHeight()});
- else{var b=a.win.offset();b.top+=2;b.left+=a.win.outerWidth()-a.rail.width-5;a.rail.css({position:"absolute",top:b.top,left:b.left,height:a.win.outerHeight()});a.zoom&&a.zoom.css({position:"absolute",top:b.top+1,left:b.left-18})}};this.init=function(){if(!a.ispage||!a.cantouch&&!this.isieold){a.ispage?a.doc.css({"overflow-y":"hidden"}):a.docscroll.css({"overflow-y":"hidden"});var b=e(document.createElement("div"));b.attr("id",a.id);b.width=4+parseFloat(a.opt.cursorwidth);b.css({"padding-left":"4px",
- "padding-right":"1px",width:a.rail.width+"px","z-index":a.opt.zindex,opacity:a.cursoropacitymin});a.rail=b;var c=false;if(a.opt.boxzoom&&!a.ispage&&!a.isieold)c=document.createElement("div"),a.bind(c,"click",a.doZoom),a.zoom=e(c),a.zoom.css({cursor:"pointer","z-index":a.opt.zindex,backgroundImage:"url("+k+"zoomico.png)",height:18,width:18,backgroundPosition:"0px 0px"}),a.opt.dblclickzoom&&a.bind(a.win,"dblclick",a.doZoom),a.cantouch&&a.opt.gesturezoom&&a.bind(a.win,"gesturechange",function(b){b.scale>
- 1.5&&a.doZoomIn(b);b.scale<0.8&&a.doZoomOut(b);return a.cancelEvent(b)});a.ispage?(b.css({position:"fixed",top:"0px",right:"0px",height:"100%"}),a.doc.append(b)):a.ishwscroll?(a.win.css("position")=="static"&&a.win.css("position","relative"),a.zoom&&(a.zoom.css({position:"absolute",top:1,right:0,"margin-right":b.width+2}),a.win.append(a.zoom)),b.css({position:"absolute",top:0,right:0}),a.win.append(b)):(a.updateScrollBar(),e("body").append(b),a.zoom&&e("body").append(a.zoom));b=e(document.createElement("div"));
- b.css({position:"relative",top:0,left:0,width:a.opt.cursorwidth,height:"0px","background-color":a.opt.cursorcolor,border:a.opt.cursorborder,"-webkit-border-radius":a.opt.cursorborderradius,"-moz-border-radius":a.opt.cursorborderradius,"border-radius":a.opt.cursorborderradius});a.cursor=b;a.rail.append(b);a.win.resize(a.onResize);a.doc.resize(a.onResize);!a.ispage&&a.opt.boxzoom&&e(window).resize(a.resizeZoom);a.onResize();a.cantouch||a.opt.touchbehavior?(a.bind(a.win,"mousedown",function(b){a.rail.drag=
- {x:b.pageX,y:b.pageY,sx:a.scroll.x,sy:a.scroll.y,st:a.getScrollTop()};return a.cancelEvent(b)}),a.bind(a.win,"mouseup",function(b){a.rail.drag=false;return a.cancelEvent(b)}),a.bind(a.rail,"mousedown",function(b){a.rail.drag={x:b.pageX,y:b.pageY,sx:a.scroll.x,sy:a.scroll.y,st:a.getScrollTop()};return a.cancelEvent(b)}),a.bind(a.rail,"mouseup",function(b){a.rail.drag=false;return a.cancelEvent(b)}),a.bind(document,"mousemove",function(b){if(a.rail.drag)return a.doScrollTo(a.rail.drag.st-(b.pageY-a.rail.drag.y),
- true),a.cancelEvent(b)})):(a.bind(a.rail,"mousedown",function(b){a.rail.drag={x:b.screenX,y:b.screenY,sx:a.scroll.x,sy:a.scroll.y};return a.cancelEvent(b)}),a.bind(a.rail,"mouseup",function(b){a.rail.drag=false;a.hideCursor();return a.cancelEvent(b)}),a.bind(document,"mousemove",function(b){if(a.rail.drag){a.scroll.y=a.rail.drag.sy+(b.screenY-a.rail.drag.y);if(a.scroll.y<0)a.scroll.y=0;var c=a.scrollvaluemax;if(a.scroll.y>c)a.scroll.y=c;a.showCursor();a.cursorfreezed=true;a.doScroll(Math.round(a.scroll.y*
- a.scrollratio.y));return a.cancelEvent(b)}}));a.bind(document,"mouseup",function(){a.rail.drag=false;a.hideCursor()});a.cantouch||(a.rail.mouseenter(function(){a.showCursor();a.rail.active=true}),a.rail.mouseleave(function(){a.rail.active=false;a.rail.drag||a.hideCursor()}),a.isiframe||a.bind(a.isie&&a.ispage?document:a.docscroll,"mousewheel",a.onmousewheel),a.bind(a.rail,"mousewheel",a.onmousewheel));!a.ispage&&!a.cantouch&&(a.win.attr("tabindex")||a.win.attr({tabindex:(new Date).getTime()}),a.win.focus(function(b){i=
- a.getTarget(b).id||true;a.hasfocus=true;a.noticeCursor()}),a.win.blur(function(){i=false;a.hasfocus=false}),a.win.mouseenter(function(b){j=a.getTarget(b).id||true;a.hasmousefocus=true;a.noticeCursor()}),a.win.mouseleave(function(){j=false;a.hasmousefocus=false}));a.onkeypress=function(b){var b=b?b:window.e,c=a.getTarget(b);if(!c||!/(INPUT|TEXTAREA|SELECT)/.test(c.nodeName))if(a.hasfocus||a.hasmousefocus&&!i||a.ispage&&!i&&!j){c=true;switch(b.keyCode){case 38:case 63233:a.doScrollBy(12);c=false;break;
- case 40:case 63235:a.doScrollBy(-12);c=false;break;case 33:case 63276:a.doScrollBy(a.view.h,true);c=false;break;case 34:case 63277:a.doScrollBy(-a.view.h,true);c=false;break;case 36:case 63273:a.doScrollTo(0,true);c=false;break;case 35:case 63275:a.doScrollTo(a.page.h,true);c=false;break;case 27:a.zoomactive&&(a.doZoom(),c=false)}if(!c)return a.cancelEvent(b)}};a.bind(document,"keydown",a.onkeypress)}};this.showCursor=function(){if(a.cursortimeout)clearTimeout(a.cursortimeout),a.cursortimeout=0;a.rail&&
- (a.rail.stop().css({opacity:a.opt.cursoropacitymax}),a.cursor.css({height:a.cursorheight,top:a.scroll.y}),a.zoom&&a.zoom.stop().css({opacity:a.opt.cursoropacitymax}))};this.hideCursor=function(b){if(!a.cursortimeout&&a.rail)a.cursortimeout=setTimeout(function(){a.rail.active||(e(a.rail).stop().animate({opacity:a.opt.cursoropacitymin}),a.zoom&&a.zoom.stop().animate({opacity:a.opt.cursoropacitymin}));a.cursortimeout=0},b||800)};this.noticeCursor=function(b){a.showCursor();a.hideCursor(b)};this.getContentSize=
- function(){var b=a.ispage?{w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)}:a.haswrapper?{w:a.doc.outerWidth()+parseInt(a.win.css("paddingLeft"))+parseInt(a.win.css("paddingRight")),h:a.doc.outerHeight()+parseInt(a.win.css("paddingTop"))+parseInt(a.win.css("paddingBottom"))}:{w:a.docscroll[0].scrollWidth,h:a.docscroll[0].scrollHeight};b.w-=1;b.h-=1;return b};this.onResize=function(){a.ispage||a.updateScrollBar();
- a.view={w:a.ispage?a.win.width():a.win.innerWidth(),h:a.ispage?a.win.height():a.win.innerHeight()};a.page=a.getContentSize();a.cursorheight=Math.min(a.view.h,Math.round(a.view.h*(a.view.h/a.page.h)));a.scrollvaluemax=a.view.h-a.cursorheight-2;a.scrollratio={x:0,y:(a.page.h-a.view.h)/a.scrollvaluemax};a.scroll.y=Math.round(a.getScrollTop()*(1/a.scrollratio.y));a.noticeCursor()};this.bind=function(b,c,d,e){var f=b.length?b[0]:b;f.addEventListener?(a.cantouch&&/mouseup|mousedown|mousemove/.test(c)&&
- f.addEventListener(c=="mousedown"?"touchstart":c=="mouseup"?"touchend":"touchmove",function(a){if(a.touches.length<2){var b=a.touches.length>0?a.touches[0]:a;b.original=a;d.call(this,b)}},e||false),f.addEventListener(c,d,e||false),c=="mousewheel"&&f.addEventListener("DOMMouseScroll",d,e||false)):f.attachEvent?f.attachEvent("on"+c,function(b){if(!d.call(f,b)||!e)return a.cancelEvent(b)}):f["on"+c]=function(b){if(!d.call(f,b)||!e)return a.cancelEvent(b)}};this.cancelEvent=function(b){b=a.cantouch?b?
- b.original:false:b?b:window.event||false;if(!b)return false;b.stopPropagation&&b.stopPropagation();b.preventDefault&&b.preventDefault();b.cancelBubble=true;b.cancel=true;return b.returnValue=false};this.onmousewheel=function(b){b=b?b:window.event;if(a.rail.drag)return a.cancelEvent(b);var c=0;(c=b.detail?b.detail*-1:b.wheelDelta/40)&&a.doScrollBy(c*a.opt.mousescrollstep,true);return a.cancelEvent(b)};this.stop=function(){a.timer&&clearInterval(a.timer);a.timer=0;a.cursorfreezed=false;a.scroll.y=Math.round(a.getScrollTop()*
- (1/a.scrollratio.y));a.noticeCursor()};this.doScroll=function(b){a.newscrolly=b;if(!a.timer)a.timer=setInterval(function(){var b=a.newscrolly-a.getScrollTop(),b=b>0?Math.ceil(b/4):Math.floor(b/4),b=a.getScrollTop()+b;a.setScrollTop(b);if(b==a.newscrolly)clearInterval(a.timer),a.timer=0,a.cursorfreezed=false},a.opt.scrollspeed),a.noticeCursor()};this.doScrollBy=function(b,c){c&&(b=Math.round(b*1/a.scrollratio.y));var d=a.scroll.y-b;d<0&&(d=0);var e=a.scrollvaluemax;d>e&&(d=e);a.cursorfreezed=false;
- a.doScroll(Math.floor(d*a.scrollratio.y))};this.doScrollTo=function(b,c){c&&(b=Math.round(b*1/a.scrollratio.y));ny=b;ny<0&&(ny=0);var d=a.scrollvaluemax;ny>d&&(ny=d);a.cursorfreezed=false;a.doScroll(Math.floor(ny*a.scrollratio.y))};a.onscroll=function(){var b=(new Date).getTime();if(!a.lastcontentcheck||a.lastcontentcheck<b)if(a.lastcontentcheck=b+500,a.getContentSize().h!=a.page.h)a.onResize();if(!a.rail.drag){if(!a.cursorfreezed)a.scroll.y=Math.round(a.getScrollTop()*(1/a.scrollratio.y));a.noticeCursor()}};
- a.docscroll.scroll(function(b){a.onscroll(b)});this.doZoomIn=function(b){if(!a.zoomactive){a.zoomrestore={style:{}};for(var c in a.win[0].style)a.zoomrestore.style[c]=a.win[0].style[c];a.zoomrestore.padding={w:a.win.outerWidth()-a.win.width(),h:a.win.outerHeight()-a.win.height()};a.win.css({position:"fixed",top:0,left:0,"z-index":a.opt.zindex+100});c=a.win.css("backgroundColor");(c==""||/transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(c))&&a.win.css("backgroundColor","#fff");a.rail.css({"z-index":a.opt.zindex+
- 110});a.zoom.css({"z-index":a.opt.zindex+112});a.zoomactive=true;a.zoom.css("backgroundPosition","0px -18px");a.resizeZoom();return a.cancelEvent(b)}};this.doZoomOut=function(b){if(a.zoomactive){var c=a.zoomrestore.style;a.win.css({position:c.position||"",top:c.top||"",left:c.left||"",width:c.width||"",height:c.height||"","z-index":c.zIndex||"",backgroundColor:c.backgroundColor||""});a.rail.css({"z-index":a.opt.zindex});a.zoom.css({"z-index":a.opt.zindex});a.zoomactive=false;a.zoomrestore=false;a.zoom.css("backgroundPosition",
- "0px 0px");a.win.resize();return a.cancelEvent(b)}};this.doZoom=function(b){return a.zoomactive?a.doZoomOut(b):a.doZoomIn(b)};this.resizeZoom=function(){if(a.zoomactive){var b=a.getScrollTop();a.win.css({width:e(window).width()-a.zoomrestore.padding.w+"px",height:e(window).height()-a.zoomrestore.padding.h+"px"});a.setScrollTop(b);a.win.resize()}};this.init()};e.fn.niceScroll=function(d,a){typeof d=="object"&&typeof a=="undefined"&&(a=d,d=false);var g=[];typeof a=="undefined"&&(a={});if(d)a.doc=e(d),
- a.win=e(this);var b=!("doc"in a);this.each(function(){a.doc=b?e(this):a.doc;g.push(new l(a))});return g.length==1?g[0]:g}})(jQuery);
|