-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.scroller.min.js
1 lines (1 loc) · 2.47 KB
/
jquery.scroller.min.js
1
(function(a){function b(a){var b=document.documentElement;for(var c=0;c<a.length;c++)if(a[c]in b.style)return a[c];return null}var c="ontouchstart"in window?!0:!1,d=b(["transform","MozTransform","webkitTransform","msTransform","OTransform"]),e=b(["transitionDuration","MozTransitionDuration","webkitTransitionDuration","msTransitionDuration","OTransitionDuration"]),f={box_element:".swipable-box",move_treshold:.15,tap_treshold:.05,fixed_stops:!0},g=function(b,c){this.$el=b,this.settings=a.extend(f,c),this.idx=0,this.x=0,this.init()};g.prototype={init:function(){this.$boxes=this.$el.find(this.settings.box_element),this.boxW=this.$boxes.outerWidth(),this.$el.on(c?"touchstart":"mousedown",a.proxy(this.start,this))},mX:function(a){return c?a.originalEvent.targetTouches[0].pageX:a.pageX},start:function(b){b.preventDefault(),this.startEl=b.target,this.startX=this.endX=this.mX(b),e&&(this.$el.get(0).style[e]="0s"),c?a(document).on({"touchmove.scroller":a.proxy(this.move,this),"touchend.scroller":a.proxy(this.end,this)}):a(document).on({"mousemove.scroller":a.proxy(this.move,this),"mouseup.scroller":a.proxy(this.end,this)})},move:function(a){var b;a.preventDefault(),this.startX&&(this.endX=this.mX(a),b=this.x+(this.endX-this.startX),d?this.$el.get(0).style[d]="translate3d("+b+"px,0px,0px)":this.$el.css({left:b+"px"}))},end:function(b){if(this.settings.fixed_stops&&this.startX!==null&&this.endX!==null){this.move_to_closest(),this.startEl=b.target;var c=Math.abs(this.startX-this.endX);this.startEl&&c<=this.settings.tap_treshold*this.boxW&&a(this.startEl).trigger("tap")}this.startX=this.endX=this.startEl=null,a(document).off(".scroller")},move_to_closest:function(){var a=this.startX-this.endX,b=Math.round(-1*this.x/this.boxW),c=this.settings.move_treshold*this.boxW;a>c&&b+1<this.$boxes.length?b++:-1*a>c&&b-1>=0&&b--,this.move_to(b)},move_to:function(b,c){var f=this.boxW*b;this.idx=b,this.x=-1*f,d&&e?(this.$el.get(0).style[e]=c?"0s":"0.5s",this.$el.get(0).style[d]="translate3d("+this.x+"px,0px,0px)"):(this.$el.stop(),c?this.$el.css({left:this.x}):this.$el.animate({left:this.x},800)),c||setTimeout(a.proxy(function(){this.$el.trigger("scrollstop",[b])},this),810)},next:function(){this.idx+1<this.$boxes.length&&this.move_to(this.idx+1)},previous:function(){this.idx-1>=0&&this.move_to(this.idx-1)}},a.fn.scroller=function(b){return a(this).data("plugin_scroller")?a(this).data("plugin_scroller"):this.each(function(){a.data(this,"plugin_scroller")||a(this).data("plugin_scroller",new g(a(this),b))})}})(jQuery)