forked from ne0x/jQuery-slimScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.slimscroll.min.js
8 lines (8 loc) · 4.96 KB
/
jquery.slimscroll.min.js
1
2
3
4
5
6
7
8
/*! Copyright (c) 2011 Piotr Rochala (http://rocha.la)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version: 1.3.2
*
*/
(function(a){jQuery.fn.extend({slimScroll:function(b){b=b||{};var c={width:"auto",height:"250px",size:"7px",color:"#000",position:"right",distance:"1px",start:"top",opacity:0.4,alwaysVisible:false,disableFadeOut:false,railVisible:false,railColor:"#333",railOpacity:0.2,railDraggable:true,railClass:"slimScrollRail",barClass:"slimScrollBar",wrapperClass:"slimScrollDiv",allowPageScroll:false,wheelStep:20,touchScrollStep:200,borderRadius:"7px",railBorderRadius:"7px"};var d=a.extend(c,b);this.each(function(){var y,s,l,q,B,u,p,k,m="<div></div>",v=30,r=false;var C=a(this);if(C.parent().hasClass(d.wrapperClass)){var h=C.scrollTop();w=C.parent().find("."+d.barClass);e=C.parent().find("."+d.railClass);D();if(a.isPlainObject(b)){if("height" in b&&b.height=="auto"){C.parent().css("height","auto");C.css("height","auto");var o=C.parent().parent().height();C.parent().css("height",o);C.css("height",o)}if("scrollTo" in b){h=parseInt(d.scrollTo)}else{if("scrollBy" in b){h+=parseInt(d.scrollBy)}else{if("destroy" in b){j.call(this);w.remove();e.remove();C.unwrap();return}}}x(h,false,true)}return}d.height=(b.height=="auto")?C.parent().height():b.height;var i=a(m).addClass(d.wrapperClass).css({position:"relative",overflow:"hidden",width:d.width,height:d.height});C.css({overflow:"hidden",width:d.width,height:d.height});var e=a(m).addClass(d.railClass).css({width:d.size,height:"100%",position:"absolute",top:0,display:(d.alwaysVisible&&d.railVisible)?"block":"none","border-radius":d.railBorderRadius,background:d.railColor,opacity:d.railOpacity,zIndex:90});var w=a(m).addClass(d.barClass).css({background:d.color,width:d.size,position:"absolute",top:0,opacity:d.opacity,display:d.alwaysVisible?"block":"none","border-radius":d.borderRadius,BorderRadius:d.borderRadius,MozBorderRadius:d.borderRadius,WebkitBorderRadius:d.borderRadius,zIndex:99});var f=(d.position=="right")?{right:d.distance}:{left:d.distance};e.css(f);w.css(f);C.wrap(i);C.parent().append(w);C.parent().append(e);if(d.railDraggable){w.bind("mousedown",function(E){var F=a(document);l=true;t=parseFloat(w.css("top"));pageY=E.pageY;F.bind("mousemove.slimscroll",function(G){currTop=t+G.pageY-pageY;w.css("top",currTop);x(0,w.position().top,false)});F.bind("mouseup.slimscroll",function(G){l=false;n();F.unbind(".slimscroll")});return false}).bind("selectstart.slimscroll",function(E){E.stopPropagation();E.preventDefault();return false})}e.hover(function(){g()},function(){n()});w.hover(function(){s=true},function(){s=false});C.hover(function(){y=true;g();n()},function(){y=false;n()});C.bind("touchstart",function(F,E){if(F.originalEvent.touches.length){B=F.originalEvent.touches[0].pageY}});C.bind("touchmove",function(F){if(!r){F.originalEvent.preventDefault()}if(F.originalEvent.touches.length){var E=(B-F.originalEvent.touches[0].pageY)/d.touchScrollStep;x(E,true);B=F.originalEvent.touches[0].pageY}});D();if(d.start==="bottom"){w.css({top:C.outerHeight()-w.outerHeight()});x(0,true)}else{if(d.start!=="top"){x(a(d.start).position().top,null,true);if(!d.alwaysVisible){w.hide()}}}z.call(this);function A(F){if(!y){return}var F=F||window.event;var G=0;if(F.wheelDelta){G=-F.wheelDelta/120}if(F.detail){G=F.detail/3}var E=F.target||F.srcTarget||F.srcElement;if(a(E).closest("."+d.wrapperClass).is(C.parent())){x(G,true)}if(F.preventDefault&&!r){F.preventDefault()}if(!r){F.returnValue=false}}function x(J,G,E){r=false;var I=J;var H=C.outerHeight()-w.outerHeight();if(G){I=parseInt(w.css("top"))+J*parseInt(d.wheelStep)/100*w.outerHeight();I=Math.min(Math.max(I,0),H);I=(J>0)?Math.ceil(I):Math.floor(I);w.css({top:I+"px"})}p=parseInt(w.css("top"))/(C.outerHeight()-w.outerHeight());I=p*(C[0].scrollHeight-C.outerHeight());if(E){I=J;var F=I/C[0].scrollHeight*C.outerHeight();F=Math.min(Math.max(F,0),H);w.css({top:F+"px"})}C.scrollTop(I);C.trigger("slimscrolling",~~I);g();n()}function z(){if(this.addEventListener){this.addEventListener("DOMMouseScroll",A,false);this.addEventListener("mousewheel",A,false)}else{this.attachEvent("onmousewheel",A)}}function j(){if(this.removeEventListener){this.removeEventListener("DOMMouseScroll",A);this.removeEventListener("mousewheel",A)}else{this.detachEvent("onmousewheel",A)}}function D(){u=Math.max((C.outerHeight()/C[0].scrollHeight)*C.outerHeight(),v);w.css({height:u+"px"});var E=u==C.outerHeight()?"none":"block";w.css({display:E})}function g(){D();clearTimeout(q);if(p==~~p){r=d.allowPageScroll;if(k!=p){var E=(~~p==0)?"top":"bottom";C.trigger("slimscroll",E)}}else{r=false}k=p;if(u>=C.outerHeight()){r=true;return}w.stop(true,true).fadeIn("fast");if(d.railVisible){e.stop(true,true).fadeIn("fast")}}function n(){if(!d.alwaysVisible){q=setTimeout(function(){if(!(d.disableFadeOut&&y)&&!s&&!l){w.fadeOut("slow");e.fadeOut("slow")}},1000)}}});return this}});jQuery.fn.extend({slimscroll:jQuery.fn.slimScroll})})(jQuery);