scrollbar.min.js 4.1 KB

1
  1. define(function(t,r,o){"use strict";var i=t("../util"),e=t("../animate"),s=40,n=10,l=50,a=i.prefixStyle("transform"),c=i.vendor?["-",i.vendor,"-transform"].join(""):"transform",d=i.prefixStyle("transition"),u=(i.prefixStyle("borderRadius"),i.prefixStyle("transitionDuration"),function(t){this.userConfig=i.mix({MIN_BAR_SCROLLED_SIZE:n,MIN_BAR_SIZE:l,MAX_BOUNCE_DISTANCE:s,spacing:5},t),this.init(t.xscroll)});return i.mix(u.prototype,{init:function(t){var r=this;r.xscroll=t,r.type=r.userConfig.type,r.isY="y"==r.type,r.scrollTopOrLeft=r.isY?"scrollTop":"scrollLeft"},destroy:function(){var t=this;i.remove(t.scrollbar),t.xscroll.off("scroll",t._scrollHandler,t),t.xscroll.off("scrollend",t._scrollEndHandler,t)},render:function(){var t=this,r=t.xscroll,o=(r.boundry,t.xscroll.userConfig.indicatorInsets),e=r.userConfig.gpuAcceleration?" translateZ(0) ":"",s=e?c+":"+e+";":"",n="opacity:0;position:absolute;z-index:999;overflow:hidden;-webkit-border-radius:3px;-moz-border-radius:3px;-o-border-radius:3px;"+s;o._xright=o.right+o.spacing,o._xbottom=o.bottom+o.spacing;var l=t.isY?i.substitute("width:{width}px;bottom:{_xbottom}px;top:{top}px;right:{right}px;",o)+n:i.substitute("height:{width}px;left:{left}px;right:{_xright}px;bottom:{bottom}px;",o)+n;t.scrollbar||(t.scrollbar=document.createElement("div"),t.indicate=document.createElement("div"),r.renderTo.appendChild(t.scrollbar),t.scrollbar.appendChild(t.indicate)),t.scrollbar.style.cssText=l;var a=t.isY?"width:100%;":"height:100%;";t.indicate.style.cssText=a+"position:absolute;background:rgba(0,0,0,0.3);-webkit-border-radius:3px;-moz-border-radius:3px;-o-border-radius:3px;",t._update(),t.hide(0),t._bindEvt()},_update:function(t,r,o,i){var e=this,t=void 0===t?e.isY?e.xscroll.getScrollTop():e.xscroll.getScrollLeft():t,s=e.computeScrollBar(t),n=e.isY?"height":"width";e.indicate.style[n]=Math.round(s.size)+"px",r&&o?e.scrollTo(s.pos,r,o,i):e.moveTo(s.pos)},computeScrollBar:function(t){var r=this,o=(r.isY?"y":"x",r.userConfig.spacing),i=r.xscroll,e=i.boundry,s=r.userConfig,t=r.isY?Math.round(t)+e._xtop:Math.round(t)+e._xleft,n=s.MIN_BAR_SCROLLED_SIZE,l=s.MIN_BAR_SIZE,a=s.MAX_BOUNCE_DISTANCE;r.containerSize=r.isY?i.containerHeight+e._xtop+e._xbottom:r.xscroll.containerWidth+e._xright+e._xleft,r.size=r.isY?e.cfg.height:e.cfg.width,r.indicateSize=r.isY?e.cfg.height-2*o:e.cfg.width-2*o;var c=r.indicateSize,d=r.containerSize,u=c*t/d,p=Math.round(c*r.size/d),x=r.isY?i.getBoundryOutTop():i.getBoundryOutLeft(),h=r.isY?i.getBoundryOutBottom():i.getBoundryOutRight(),f=l-p>0?l-p:0;if(p=p<l?l:p,u=(c-f)*t/d,x>=0){var _=x/a;_=_>1?1:_,u=-_*(p-n)}if(h>=0){var _=h/a;_=_>1?1:_,u=_*(p-n)+c-p}return r.barPos=Math.round(u),{size:Math.round(p),pos:r.barPos}},scrollTo:function(t,r,o,i){var s=this;s.show();var n=s.xscroll.userConfig.gpuAcceleration?" translateZ(0) ":"",l={css:{transform:s.isY?"translateY("+t+"px)"+n:"translateX("+t+"px)"+n},duration:r,easing:o,useTransition:s.xscroll.userConfig.useTransition,end:i};s.__timer=s.__timer||new e(s.indicate,l),s.__timer.stop(),s.__timer.reset(l),s.__timer.run()},moveTo:function(t){var r=this;r.show();var o=r.xscroll.userConfig.gpuAcceleration?" translateZ(0) ":"";r.isY?r.indicate.style[a]="translateY("+t+"px) "+o:r.indicate.style[a]="translateX("+t+"px) "+o,r.indicate.style[d]=""},_scrollHandler:function(t){var r=this;return r._update(t[r.scrollTopOrLeft]),r},isBoundryOut:function(){var t=this;return t.isY?t.xscroll.isBoundryOutTop()||t.xscroll.isBoundryOutBottom():t.xscroll.isBoundryOutLeft()||t.xscroll.isBoundryOutRight()},_scrollEndHandler:function(t){var r=this;return r.isBoundryOut()||(r._update(t[r.scrollTopOrLeft]),r.hide()),r},_bindEvt:function(){var t=this;t.__isEvtBind||(t.__isEvtBind=!0,t.xscroll.on("scroll",t._scrollHandler,t),t.xscroll.on("scrollend",t._scrollEndHandler,t))},reset:function(){var t=this;t.pos=0,t._update()},hide:function(t,r,o){var i=this,t=t>=0?t:300,o=o>=0?o:100;i.scrollbar.style.opacity=0,i.scrollbar.style[d]=["opacity ",t,"ms "," ease-out ",o,"ms"].join("")},show:function(){var t=this;t.scrollbar.style.opacity=1,t.scrollbar.style[d]=""}}),"object"==typeof o&&o.exports?void(o.exports=u):u});