1 line
9.4 KiB
JavaScript
1 line
9.4 KiB
JavaScript
!function(e){"function"==typeof define&&define.amd?define(["jquery"],function(t){return e(t,window,document)}):"object"==typeof exports?module.exports=e(require("jquery"),window,document):e(jQuery,window,document)}(function(s,o,i){"use strict";var h,c,n,a,p,d,e,u,g,v,l,r,f,S,m,T,y,b,w,x,$,H,C,O,A;function t(t,e){this.el=t,this.options=e,c=c||w(),this.$el=s(this.el),this.doc=s(this.options.documentContext||i),this.win=s(this.options.windowContext||o),this.body=this.doc.find("body"),this.$content=this.$el.children("."+this.options.contentClass),this.$content.attr("tabindex",this.options.tabIndex||0),this.content=this.$content[0],this.previousPosition=0,this.options.iOSNativeScrolling&&null!=this.el.style.WebkitOverflowScrolling?this.nativeScrolling():this.generate(),this.createEvents(),this.addEvents(),this.reset()}b={paneClass:"nano-pane",sliderClass:"nano-slider",contentClass:"nano-content",iOSNativeScrolling:!1,preventPageScrolling:!1,disableResize:!1,alwaysVisible:!1,flashDelay:1500,sliderMinHeight:20,sliderMaxHeight:null,documentContext:null,windowContext:null},S="scroll",e="mousedown",u="mouseenter",g="mousemove",l="mousewheel",v="mouseup",f="resize",p="drag",d="enter",T="up",n="DOMMouseScroll",a="down",m="touchmove",h="Microsoft Internet Explorer"===o.navigator.appName&&/msie 7./i.test(o.navigator.appVersion)&&o.ActiveXObject,c=null,H=o.requestAnimationFrame,y=o.cancelAnimationFrame,O=i.createElement("div").style,A=function(){for(var t,e=["t","webkitT","MozT","msT","OT"],i=t=0,s=e.length;t<s;i=++t)if(e[i],e[i]+"ransform"in O)return e[i].substr(0,e[i].length-1);return!1}(),C=function(t){return!1!==A&&(""===A?t:A+t.charAt(0).toUpperCase()+t.substr(1))}("transform"),x=!1!==C,w=function(){var t=i.createElement("div"),e=t.style;return e.position="absolute",e.width="100px",e.height="100px",e.overflow=S,e.top="-9999px",i.body.appendChild(t),e=t.offsetWidth-t.clientWidth,i.body.removeChild(t),e},$=function(){var t=o.navigator.userAgent,e=/(?=.+Mac OS X)(?=.+Firefox)/.test(t);return!!e&&(t=(t=/Firefox\/\d{2}\./.exec(t))&&t[0].replace(/\D+/g,""),e&&23<+t)},t.prototype.preventScrolling=function(t,e){this.isActive&&(t.type===n?(e===a&&0<t.originalEvent.detail||e===T&&t.originalEvent.detail<0)&&t.preventDefault():t.type===l&&t.originalEvent&&t.originalEvent.wheelDelta&&(e===a&&t.originalEvent.wheelDelta<0||e===T&&0<t.originalEvent.wheelDelta)&&t.preventDefault())},t.prototype.nativeScrolling=function(){this.$content.css({WebkitOverflowScrolling:"touch"}),this.iOSNativeScrolling=!0,this.isActive=!0},t.prototype.updateScrollValues=function(){var t=this.content;this.maxScrollTop=t.scrollHeight-t.clientHeight,this.prevScrollTop=this.contentScrollTop||0,this.contentScrollTop=t.scrollTop,t=this.contentScrollTop>this.previousPosition?"down":this.contentScrollTop<this.previousPosition?"up":"same",this.previousPosition=this.contentScrollTop,"same"!=t&&this.$el.trigger("update",{position:this.contentScrollTop,maximum:this.maxScrollTop,direction:t}),this.iOSNativeScrolling||(this.maxSliderTop=this.paneHeight-this.sliderHeight,this.sliderTop=0===this.maxScrollTop?0:this.contentScrollTop*this.maxSliderTop/this.maxScrollTop)},t.prototype.setOnScrollStyles=function(){var t,e;x?(t={})[C]="translate(0, "+this.sliderTop+"px)":t={top:this.sliderTop},H?(y&&this.scrollRAF&&y(this.scrollRAF),this.scrollRAF=H((e=this,function(){return e.scrollRAF=null,e.slider.css(t)}))):this.slider.css(t)},t.prototype.createEvents=function(){var e,i,s,o,n,l,r,h;this.events={down:function(t){return h.isBeingDragged=!0,h.offsetY=t.pageY-h.slider.offset().top,h.slider.is(t.target)||(h.offsetY=0),h.pane.addClass("active"),h.doc.bind(g,h.events[p]).bind(v,h.events.up),h.body.bind(u,h.events[d]),!1},drag:function(t){return r.sliderY=t.pageY-r.$el.offset().top-r.paneTop-(r.offsetY||.5*r.sliderHeight),r.scroll(),r.contentScrollTop>=r.maxScrollTop&&r.prevScrollTop!==r.maxScrollTop?r.$el.trigger("scrollend"):0===r.contentScrollTop&&0!==r.prevScrollTop&&r.$el.trigger("scrolltop"),!1},up:function(t){return l.isBeingDragged=!1,l.pane.removeClass("active"),l.doc.unbind(g,l.events[p]).unbind(v,l.events.up),l.body.unbind(u,l.events[d]),!1},resize:function(t){n.reset()},panedown:function(t){return o.sliderY=(t.offsetY||t.originalEvent.layerY)-.5*o.sliderHeight,o.scroll(),o.events.down(t),!1},scroll:function(t){s.updateScrollValues(),s.isBeingDragged||(s.iOSNativeScrolling||(s.sliderY=s.sliderTop,s.setOnScrollStyles()),null!=t&&(s.contentScrollTop>=s.maxScrollTop?(s.options.preventPageScrolling&&s.preventScrolling(t,a),s.prevScrollTop!==s.maxScrollTop&&s.$el.trigger("scrollend")):0===s.contentScrollTop&&(s.options.preventPageScrolling&&s.preventScrolling(t,T),0!==s.prevScrollTop&&s.$el.trigger("scrolltop"))))},wheel:function(t){if(null!=t)return(t=t.delta||t.wheelDelta||t.originalEvent&&t.originalEvent.wheelDelta||-t.detail||t.originalEvent&&-t.originalEvent.detail)&&(i.sliderY+=-t/3),i.scroll(),!1},enter:(e=i=s=o=n=l=r=h=this,function(t){if(e.isBeingDragged)return 1!==(t.buttons||t.which)?(t=e.events).up.apply(t,arguments):void 0})}},t.prototype.addEvents=function(){var t;this.removeEvents(),t=this.events,this.options.disableResize||this.win.bind(f,t[f]),this.iOSNativeScrolling||(this.slider.bind(e,t[a]),this.pane.bind(e,t.panedown).bind(l+" "+n,t.wheel)),this.$content.bind(S+" "+l+" "+n+" "+m,t[S])},t.prototype.removeEvents=function(){var t=this.events;this.win.unbind(f,t[f]),this.iOSNativeScrolling||(this.slider.unbind(),this.pane.unbind()),this.$content.unbind(S+" "+l+" "+n+" "+m,t[S])},t.prototype.generate=function(){var t,e=this.options,i=e.paneClass,s=e.sliderClass;e.contentClass;return(e=this.$el.children("."+i)).length||e.children("."+s).length||this.$el.append('<div class="'+i+'"><div class="'+s+'" /></div>'),this.pane=this.$el.children("."+i),this.slider=this.pane.find("."+s),0===c&&$()?t={right:-14,paddingRight:+o.getComputedStyle(this.content,null).getPropertyValue("padding-right").replace(/[^0-9.]+/g,"")+14}:c&&(t={right:-c},this.$el.addClass("has-scrollbar")),null!=t&&this.$content.css(t),this},t.prototype.restore=function(){this.stopped=!1,this.iOSNativeScrolling||this.pane.show(),this.addEvents()},t.prototype.reset=function(){var t,e,i,s,o,n,l,r;if(!this.iOSNativeScrolling)return this.$el.find("."+this.options.paneClass).length||this.generate().stop(),this.stopped&&this.restore(),i=(e=(t=this.content).style).overflowY,h&&this.$content.css({height:this.$content.height()}),l=t.scrollHeight+c,0<(n=parseInt(this.$el.css("max-height"),10))&&(this.$el.height(""),this.$el.height(t.scrollHeight>n?n:t.scrollHeight)),s=(n=this.pane.outerHeight(!1))+(o=parseInt(this.pane.css("top"),10))+parseInt(this.pane.css("bottom"),10),(r=Math.round(s/l*n))<this.options.sliderMinHeight?r=this.options.sliderMinHeight:null!=this.options.sliderMaxHeight&&r>this.options.sliderMaxHeight&&(r=this.options.sliderMaxHeight),i===S&&e.overflowX!==S&&(r+=c),this.maxSliderTop=s-r,this.contentHeight=l,this.paneHeight=n,this.paneOuterHeight=s,this.sliderHeight=r,this.paneTop=o,this.slider.height(r),this.events.scroll(),this.pane.show(),this.isActive=!0,t.scrollHeight===t.clientHeight||this.pane.outerHeight(!0)>=t.scrollHeight&&i!==S?(this.pane.hide(),this.isActive=!1):this.el.clientHeight===t.scrollHeight&&i===S?this.slider.hide():this.slider.show(),this.pane.css({opacity:this.options.alwaysVisible?1:"",visibility:this.options.alwaysVisible?"visible":""}),"static"!==(e=this.$content.css("position"))&&"relative"!==e||(l=parseInt(this.$content.css("right"),10))&&this.$content.css({right:"",marginRight:l}),this;this.contentHeight=this.content.scrollHeight},t.prototype.scroll=function(){if(this.isActive)return this.sliderY=Math.max(0,this.sliderY),this.sliderY=Math.min(this.maxSliderTop,this.sliderY),this.$content.scrollTop(this.maxScrollTop*this.sliderY/this.maxSliderTop),this.iOSNativeScrolling||(this.updateScrollValues(),this.setOnScrollStyles()),this},t.prototype.scrollBottom=function(t){if(this.isActive)return this.$content.scrollTop(this.contentHeight-this.$content.height()-t).trigger(l),this.stop().restore(),this},t.prototype.scrollTop=function(t){if(this.isActive)return this.$content.scrollTop(+t).trigger(l),this.stop().restore(),this},t.prototype.scrollTo=function(t){if(this.isActive)return this.scrollTop(this.$el.find(t).get(0).offsetTop),this},t.prototype.stop=function(){return y&&this.scrollRAF&&(y(this.scrollRAF),this.scrollRAF=null),this.stopped=!0,this.removeEvents(),this.iOSNativeScrolling||this.pane.hide(),this},t.prototype.destroy=function(){return this.stopped||this.stop(),!this.iOSNativeScrolling&&this.pane.length&&this.pane.remove(),h&&this.$content.height(""),this.$content.removeAttr("tabindex"),this.$el.hasClass("has-scrollbar")&&(this.$el.removeClass("has-scrollbar"),this.$content.css({right:""})),this},t.prototype.flash=function(){var t;if(!this.iOSNativeScrolling&&this.isActive)return this.reset(),this.pane.addClass("flashed"),setTimeout(function(){t.pane.removeClass("flashed")},(t=this).options.flashDelay),this},r=t,s.fn.nanoScroller=function(i){return this.each(function(){var t,e;if((e=this.nanoscroller)||(t=s.extend({},b,i),this.nanoscroller=e=new r(this,t)),i&&"object"==typeof i){if(s.extend(e.options,i),null!=i.scrollBottom)return e.scrollBottom(i.scrollBottom);if(null!=i.scrollTop)return e.scrollTop(i.scrollTop);if(i.scrollTo)return e.scrollTo(i.scrollTo);if("bottom"===i.scroll)return e.scrollBottom(0);if("top"===i.scroll)return e.scrollTop(0);if(i.scroll&&i.scroll instanceof s)return e.scrollTo(i.scroll);if(i.stop)return e.stop();if(i.destroy)return e.destroy();if(i.flash)return e.flash()}return e.reset()})},s.fn.nanoScroller.Constructor=r}); |