7 lines
4.6 KiB
JavaScript
7 lines
4.6 KiB
JavaScript
/*! 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.8
|
|
*
|
|
*/ !function(e){e.fn.extend({slimScroll:function(i){var s=e.extend({width:"auto",height:"250px",size:"7px",color:"#000",position:"right",distance:"1px",start:"top",opacity:.4,alwaysVisible:!1,disableFadeOut:!1,railVisible:!1,railColor:"#333",railOpacity:.2,railDraggable:!0,scrollBehavior:"smooth",railClass:"slimScrollRail",barClass:"slimScrollBar",wrapperClass:"slimScrollDiv",allowPageScroll:!1,wheelStep:0,touchScrollStep:200,borderRadius:"7px",railBorderRadius:"7px"},i);return this.each(function(){function o(i){if(c){i=i||window.event;var o=0;i.wheelDelta&&(o=-i.wheelDelta/120),i.detail&&(o=i.detail/3),e(i.target||i.srcTarget||i.srcElement).closest("."+s.wrapperClass).is(v.parent())&&r(o,!0),i.preventDefault&&!b&&i.preventDefault(),b||(i.returnValue=!1)}}function r(e,i,o){b=!1;var r=v.outerHeight()-m.outerHeight();i&&(i=Math.min(Math.max(i=parseInt(m.css("top"))+e*parseInt(s.wheelStep)/100*m.outerHeight(),0),r),i=0<e?Math.ceil(i):Math.floor(i),m.css({top:i+"px"})),i=(f=parseInt(m.css("top"))/(v.outerHeight()-m.outerHeight()))*(v[0].scrollHeight-v.outerHeight()),o&&(e=Math.min(Math.max(e=(i=e)/v[0].scrollHeight*v.outerHeight(),0),r),m.css({top:e+"px"})),v.scrollTop(i),v.trigger("slimscrolling",~~i),a(),n()}function l(){g=Math.max(v.outerHeight()/v[0].scrollHeight*v.outerHeight(),30),m.css({height:g+"px"});var e=g==v.outerHeight()?"none":"block";m.css({display:e})}function a(){l(),clearTimeout(u),f==~~f?(b=s.allowPageScroll,$!=f&&v.trigger("slimscroll",0==~~f?"top":"bottom")):b=!1,$=f,g>=v.outerHeight()?b=!0:(m.stop(!0,!0).fadeIn("fast"),s.railVisible&&_.stop(!0,!0).fadeIn("fast"))}function n(){s.alwaysVisible||(u=setTimeout(function(){s.disableFadeOut&&c||h||d||(m.fadeOut("slow"),_.fadeOut("slow"))},1e3))}var c,h,d,u,p,g,f,$,b=!1,v=e(this);if(v.parent().hasClass(s.wrapperClass)){var w=v.scrollTop(),m=v.siblings("."+s.barClass),_=v.siblings("."+s.railClass);if(l(),e.isPlainObject(i)){if("height"in i&&"auto"==i.height){v.parent().css("height","auto"),v.css("height","auto");var y=v.parent().parent().height();v.parent().css("height",y),v.css("height",y)}else"height"in i&&(y=i.height,v.parent().css("height",y),v.css("height",y));if("scrollTo"in i)w=parseInt(s.scrollTo);else if("scrollBy"in i)w+=parseInt(s.scrollBy);else if("destroy"in i){m.remove(),_.remove(),v.unwrap();return}r(w,!1,!0)}}else if(!(e.isPlainObject(i)&&"destroy"in i)){s.height="auto"==s.height?v.parent().height():s.height,w=e("<div></div>").addClass(s.wrapperClass).css({position:"relative",overflow:"hidden",width:s.width,height:s.height}),v.css({overflow:"hidden",width:s.width,height:s.height});var _=e("<div></div>").addClass(s.railClass).css({width:s.size,height:"100%",position:"absolute",top:0,display:s.alwaysVisible&&s.railVisible?"block":"none","border-radius":s.railBorderRadius,background:s.railColor,opacity:s.railOpacity,zIndex:90}),m=e("<div></div>").addClass(s.barClass).css({background:s.color,width:s.size,position:"absolute",top:0,opacity:s.opacity,display:s.alwaysVisible?"block":"none","border-radius":s.borderRadius,BorderRadius:s.borderRadius,MozBorderRadius:s.borderRadius,WebkitBorderRadius:s.borderRadius,zIndex:99}),y="right"==s.position?{right:s.distance}:{left:s.distance};_.css(y),m.css(y),v.wrap(w),v.parent().append(m),v.parent().append(_),s.railDraggable&&m.bind("mousedown",function(i){var s=e(document);return d=!0,t=parseFloat(m.css("top")),pageY=i.pageY,s.bind("mousemove.slimscroll",function(e){currTop=t+e.pageY-pageY,m.css("top",currTop),r(0,m.position().top,!1)}),s.bind("mouseup.slimscroll",function(e){d=!1,n(),s.unbind(".slimscroll")}),!1}).bind("selectstart.slimscroll",function(e){return e.stopPropagation(),e.preventDefault(),!1}),_.hover(function(){a()},function(){n()}),m.hover(function(){h=!0},function(){h=!1}),v.hover(function(){c=!0,a(),n()},function(){c=!1,n()}),v.bind("touchstart",function(e,i){e.originalEvent.touches.length&&(p=e.originalEvent.touches[0].pageY)}),v.bind("touchmove",function(e){b||e.originalEvent.preventDefault(),e.originalEvent.touches.length&&(r((p-e.originalEvent.touches[0].pageY)/s.touchScrollStep,!0),p=e.originalEvent.touches[0].pageY)}),l(),"bottom"===s.start?(m.css({top:v.outerHeight()-m.outerHeight()}),r(0,!0)):"top"!==s.start&&(r(e(s.start).position().top,null,!0),s.alwaysVisible||m.hide()),window.addEventListener?(this.addEventListener("DOMMouseScroll",o,!1),this.addEventListener("mousewheel",o,!1)):document.attachEvent("onmousewheel",o)}}),this}}),e.fn.extend({slimscroll:e.fn.slimScroll})}(jQuery); |