2 lines
3.1 KiB
JavaScript
2 lines
3.1 KiB
JavaScript
(function(n){function o(i,t){this.box=n(i),this.config=n.extend({},t||{}),this.width=this.config.width||this.box.children().eq(0).width(),this.size=this.config.size||this.box.children().length,this.loop=!1,this.auto=!1,this.auto_wait_time=this.config.auto_wait_time||3e3,this.scroll_time=this.config.scroll_time!==void 0?this.config.scroll_time>0?this.config.scroll_time:0:300,this.minleft=-this.width*(this.size-1),this.maxleft=0,this.now_left=0,this.point_x=null,this.point_y=null,this.move_left=!1,this.index=0,this.busy=!1,this.timer,this.init()}n.extend(o.prototype,{init:function(){this.bind_event(),this.init_loop(),this.auto_scroll()},bind_event:function(){var i=this;i.box.bind("touchstart",function(t){var e=t.touches?t.touches:t.originalEvent.targetTouches;e.length==1&&!i.busy&&(i.point_x=e[0].screenX,i.point_y=e[0].screenY)}).bind("touchmove",function(t){var e=t.touches?t.touches:t.originalEvent.targetTouches;if(e.length==1&&!i.busy)return i.move(e[0].screenX,e[0].screenY)}).bind("touchend",function(t){!i.busy&&i.move_end()})},init_loop:function(){this.box.children().length>1&&this.box.children().length==this.size&&this.loop?(this.now_left=-this.width,this.minleft=-this.width*this.size,this.maxleft=-this.width,this.box.prepend(this.box.children().eq(this.size-1).clone()).append(this.box.children().eq(1).clone()).css(this.get_style(2))):this.loop=!1},auto_scroll:function(){var i=this;!i.auto||(clearTimeout(i.timer),i.timer=setTimeout(function(){i.go_index(i.index+1)},i.auto_wait_time))},go_index:function(i){var t=this;t.busy||(clearTimeout(t.timer),t.busy=!0,t.loop?(i=i<0?-1:i,i=i>t.size?t.size:i):(i=i<0?0:i,i=i>=t.size?t.size-1:i),!t.loop&&t.now_left==-(t.width*i)||t.loop&&t.now_left==-t.width*(i+1)?t.complete(i):(i==-1||i==t.size?(t.index=i==-1?t.size-1:0,t.now_left=i==-1?0:-t.width*(t.size+1)):(t.index=i,t.now_left=-(t.width*(t.index+(t.loop?1:0)))),t.box.css(this.get_style(1)),setTimeout(function(){t.complete(i)},t.scroll_time)))},complete:function(i){var t=this;t.busy=!1,t.config.callback&&t.config.callback(t.index),i==-1?t.now_left=t.minleft:i==t.size&&(t.now_left=t.maxleft),t.auto_scroll()},next:function(){this.busy||this.go_index(this.index+1)},prev:function(){this.busy||this.go_index(this.index-1)},move:function(i,t){var e=i-(this.point_x===null?i:this.point_x),s=t-(this.point_y===null?t:this.point_y),f=this.now_left,h=!1,l=s/Math.sqrt(e*e+s*s);return this.now_left=f+e,this.move_left=e<0,(l>Math.sin(Math.PI/3)||l<-Math.sin(Math.PI/3))&&(h=!0),this.point_x=i,this.point_y=t,h},move_end:function(){var i=this.now_left%this.width,t;this.now_left<this.minleft?t=this.index+1:this.now_left>this.maxleft?t=this.index-1:i!=0?this.move_left?t=this.index+1:t=this.index-1:t=this.index,this.point_x=this.point_y=null,this.go_index(t)},get_style:function(i){var t=this.now_left,e=i==1?this.scroll_time:0;return{"-webkit-transition":"-webkit-transform "+e+"ms","-webkit-transform":"translate3d("+t+"px,0,0)","-webkit-backface-visibility":"hidden",transition:"transform "+e+"ms",transform:"translate3d("+t+"px,0,0)"}}}),n.mggScrollImg=function(i,t){var e=new o(i,t);return{next:function(){e.next()},prev:function(){e.prev()},go:function(s){e.go_index(parseInt(s)||0)}}}})(jQuery);
|