/*!Copyright(c)2011PiotrRochala(http://rocha.la)*DuallicensedundertheMIT(http://www.opensource.org/licenses/mit-license.php)*andGPL(http://www.opensource.org/licenses/gpl-license.php) licenses.**Version:1.3.8**/layui.define("jquery", function (exports) { var jQuery = layui.jquery, $ = layui.jquery;(function($) { $.fn.extend({ slimScroll: function(options) { var defaults = { //widthinpixelsofthevisiblescrollareawidth:'auto',// height in pixels of the visible scroll areaheight:'250px',// width in pixels of the scrollbar and railsize:'7px',// scrollbar color, accepts any hex/color valuecolor:'#000',// scrollbar position - left/rightposition:'right',// distance in pixels between the side edge and the scrollbardistance:'1px',// default scroll position on load - top / bottom / $('selector')start:'top',// sets scrollbar opacityopacity:.4,// enables always-on mode for the scrollbaralwaysVisible:false,// check if we should hide the scrollbar when user is hovering overdisableFadeOut:false,// sets visibility of the railrailVisible:false,// sets rail colorrailColor:'#333',// sets rail opacityrailOpacity:.2,// whether we should use jQuery UI Draggable to enable bar draggingrailDraggable:true,// defautlt CSS class of the slimscroll railrailClass:'slimScrollRail',// defautlt CSS class of the slimscroll barbarClass:'slimScrollBar',// defautlt CSS class of the slimscroll wrapperwrapperClass:'slimScrollDiv',// check if mousewheel should scroll the window if we reach top/bottomallowPageScroll:false,// scroll amount applied to each mouse wheel stepwheelStep:20,// scroll amount applied when user is using gesturestouchScrollStep:200,// sets border radiusborderRadius:'7px',// sets border radius of the railrailBorderRadius:'7px'};varo=$.extend(defaults,options);// do it for every element that matches selectorthis.each(function(){varisOverPanel,isOverBar,isDragg,queueHide,touchDif,barHeight,percentScroll,lastScroll,divS='<div></div>',minBarHeight=30,releaseScroll=false;// used in event handlers and for better minificationvarme=$(this);// ensure we are not binding it againif(me.parent().hasClass(o.wrapperClass)){// start from last bar positionvaroffset=me.scrollTop();// find bar and railbar=me.siblings('.'+o.barClass);rail=me.siblings('.'+o.railClass);getBarHeight();// check if we should scroll existing instanceif($.isPlainObject(options)){// Pass height: auto to an existing slimscroll object to force a resize after contents have changedif('height'inoptions&&options.height=='auto'){me.parent().css('height','auto');me.css('height','auto');varheight=me.parent().parent().height();me.parent().css('height',height);me.css('height',height);}elseif('height'inoptions){varh=options.height;me.parent().css('height',h);me.css('height',h);}if('scrollTo'inoptions){// jump to a static pointoffset=parseInt(o.scrollTo);}elseif('scrollBy'inoptions){// jump by value pixelsoffset+=parseInt(o.scrollBy);}elseif('destroy'inoptions){// remove slimscroll elementsbar.remove();rail.remove();me.unwrap();return;}// scroll content by the given offsetscrollContent(offset,false,true);}return;}elseif($.isPlainObject(options)){if('destroy'inoptions){return;}}// optionally set height to the parent's heighto.height=(o.height=='auto')?me.parent().height():o.height;// wrap contentvarwrapper=$(divS).addClass(o.wrapperClass).css({position:'relative',overflow:'hidden',width:o.width,height:o.height});// update style for the divme.css({overflow:'hidden',width:o.width,height:o.height});// create scrollbar railvarrail=$(divS).addClass(o.railClass).css({width:o.size,height:'100%',position:'absolute',top:0,display:(o.alwaysVisible&&o.railVisible)?'block':'none','border-radius':o.railBorderRadius,background:o.railColor,opacity:o.railOpacity,zIndex:90});// create scrollbarvarbar=$(divS).addClass(o.barClass).css({background:o.color,width:o.size,position:'absolute',top:0,opacity:o.opacity,display:o.alwaysVisible?'block':'none','border-radius':o.borderRadius,BorderRadius:o.borderRadius,MozBorderRadius:o.borderRadius,WebkitBorderRadius:o.borderRadius,zIndex:99});// set positionvarposCss=(o.position=='right')?{right:o.distance}:{left:o.distance};rail.css(posCss);bar.css(posCss);// wrap itme.wrap(wrapper);// append to parent divme.parent().append(bar);me.parent().append(rail);// make it draggable and no longer dependent on the jqueryUIif(o.railDraggable){bar.bind("mousedown",function(e){var$doc=$(document);isDragg=true;t=parseFloat(bar.css('top'));pageY=e.pageY;$doc.bind("mousemove.slimscroll",function(e){currTop=t+e.pageY-pageY;bar.css('top',currTop);scrollContent(0,bar.position().top,false);// scroll content});$doc.bind("mouseup.slimscroll",function(e){isDragg=false;hideBar();$doc.unbind('.slimscroll');});returnfalse;}).bind("selectstart.slimscroll",function(e){e.stopPropagation();e.preventDefault();returnfalse;});}// on rail overrail.hover(function(){showBar();},function(){hideBar();});// on bar overbar.hover(function(){isOverBar=true;},function(){isOverBar=false;});// show on parent mouseoverme.hover(function(){isOverPanel=true;showBar();hideBar();},function(){isOverPanel=false;hideBar();});// support for mobileme.bind('touchstart',function(e,b){if(e.originalEvent.touches.length){// record where touch startedtouchDif=e.originalEvent.touches[0].pageY;}});me.bind('touchmove',function(e){// prevent scrolling the page if necessaryif(!releaseScroll){e.originalEvent.preventDefault();}if(e.originalEvent.touches.length){// see how far user swipedvardiff=(touchDif-e.originalEvent.touches[0].pageY)/o.touchScrollStep;// scroll contentscrollContent(diff,true);touchDif=e.originalEvent.touches[0].pageY;}});// set up initial heightgetBarHeight();// check start positionif(o.start==='bottom'){// scroll content to bottombar.css({top:me.outerHeight()-bar.outerHeight()});scrollContent(0,true);}elseif(o.start!=='top'){// assume jQuery selectorscrollContent($(o.start).position().top,null,true);// make sure bar stays hiddenif(!o.alwaysVisible){bar.hide();}}// attach scroll eventsattachWheel(this);function_onWheel(e){// use mouse wheel only when mouse is overif(!isOverPanel){return;}vare=e||window.event;vardelta=0;if(e.wheelDelta){delta=-e.wheelDelta/120;}if(e.detail){delta=e.detail/3;}vartarget=e.target||e.srcTarget||e.srcElement;if($(target).closest('.'+o.wrapperClass).is(me.parent())){// scroll contentscrollContent(delta,true);}// stop window scrollif(e.preventDefault&&!releaseScroll){e.preventDefault();}if(!releaseScroll){e.returnValue=false;}}functionscrollContent(y,isWheel,isJump){releaseScroll=false;vardelta=y;varmaxTop=me.outerHeight()-bar.outerHeight();if(isWheel){// move bar with mouse wheeldelta=parseInt(bar.css('top'))+y*parseInt(o.wheelStep)/100*bar.outerHeight();// move bar, make sure it doesn't go outdelta=Math.min(Math.max(delta,0),maxTop);// if scrolling down, make sure a fractional change to the// scroll position isn't rounded away when the scrollbar's CSS is set// this flooring of delta would happened automatically when// bar.css is set below, but we floor here for claritydelta=(y>0)?Math.ceil(delta):Math.floor(delta);// scroll the scrollbarbar.css({top:delta+'px'});}// calculate actual scroll amountpercentScroll=parseInt(bar.css('top'))/(me.outerHeight()-bar.outerHeight());delta=percentScroll*(me[0].scrollHeight-me.outerHeight());if(isJump){delta=y;varoffsetTop=delta/me[0].scrollHeight*me.outerHeight();offsetTop=Math.min(Math.max(offsetTop,0),maxTop);bar.css({top:offsetTop+'px'});}// scroll contentme.scrollTop(delta);// fire scrolling eventme.trigger('slimscrolling',~~delta);// ensure bar is visibleshowBar();// trigger hide when scroll is stoppedhideBar();}functionattachWheel(target){if(window.addEventListener){target.addEventListener('DOMMouseScroll',_onWheel,false);target.addEventListener('mousewheel',_onWheel,false);}else{document.attachEvent("onmousewheel",_onWheel)}}functiongetBarHeight(){// calculate scrollbar height and make sure it is not too smallbarHeight=Math.max((me.outerHeight()/me[0].scrollHeight)*me.outerHeight(),minBarHeight);bar.css({height:barHeight+'px'});// hide scrollbar if content is not long enoughvardisplay=barHeight==me.outerHeight()?'none':'block';bar.css({display:display});}functionshowBar(){// recalculate bar heightgetBarHeight();clearTimeout(queueHide);// when bar reached top or bottomif(percentScroll==~~percentScroll){//release wheelreleaseScroll=o.allowPageScroll;// publish approporiate eventif(lastScroll!=percentScroll){varmsg=(~~percentScroll==0)?'top':'bottom';me.trigger('slimscroll',msg);}}else{releaseScroll=false;}lastScroll=percentScroll;// show only when requiredif(barHeight>=me.outerHeight()){//allow window scrollreleaseScroll=true;return;}bar.stop(true,true).fadeIn('fast');if(o.railVisible){rail.stop(true,true).fadeIn('fast');}}functionhideBar(){// only hide when options allow itif(!o.alwaysVisible){queueHide=setTimeout(function(){if(!(o.disableFadeOut&&isOverPanel)&&!isOverBar&&!isDragg){bar.fadeOut('slow');rail.fadeOut('slow');}},1000);}}});// maintain chainabilityreturnthis;}});$.fn.extend({slimscroll:$.fn.slimScroll});})(jQuery);exports('slimscroll');});