One.
Two. Brief introduction
The code has comments, logic is simple, do not do more repeating.
/** Slider*/;(function($) {$.extend ($.fn, {slider:function(obj) { This. each (function() { //Current Zepto Object var$self = $ ( This); varDom = { "Wrap": $self. Find (". Slider-list")), "Item": $self. Find (". Slider-item")), "FirstItem": $self. Find (". Slider-item")). First (),"LastItem": $self. Find (". Slider-item")). Last (),"PageList":NULL, "Pageitem":NULL, }; varSettings = { "Len": Dom.item.length,"Width": Dom.wrap.width (),"StartX": 0, "Starty": 0, "Index": obj? (Obj.infinite? 1:0): 0, "Distance": 0, "Timer":NULL, "AutoScroll": obj? (Obj.autoscroll?)true:false) :false, "Infinite": obj? (Obj.infinite?)true:false) :false }; varFuns ={init:function() { if(Settings.len > 1) {Funs.initui (); Funs.bindevent (); if(settings.autoscroll) {Settings.timer= SetInterval (funs.autoanimate, 3000); }}, Initui:function() { if(settings.infinite) {varCLONEFISRT =Dom.firstItem.clone (); varClonelast =Dom.lastItem.clone (); Dom.wrap.append (CLONEFISRT); Clonelast.insertbefore (Dom.firstitem); } funs.creatpage (); Dom.item= $self. Find (". Slider-item")); Settings.count=dom.item.length; Settings.distance= SETTINGS.WIDTH/5; if(settings.infinite) {dom.wrap.css ("-webkit-transform", "Translate3d (" +-settings.width + "px,0,0)"); }}, Creatpage:function() { varPageList = ' <div class= ' slider-page ' ><ul> '; for(vari = 0; i < Settings.len; i++) { if(i = = 0) {pagelist+ = ' <li class= ' active ' ></li> '; } Else{pagelist+ = ' <li></li> '; }} pagelist+ = ' </ul></div> '; $self. Append (pagelist); Dom.pagelist= $self. Find (". Slider-page")); Dom.pageitem= Dom.pagelist.find ("li"); }, Bindevent:function() {$self. Off (). On ({"Touchstart":function(e) {e.stoppropagation (); //E.preventdefault ();clearinterval (Settings.timer); Settings.startx= E.touches? E.touches[0].pagex:e.originalevent.touches[0].pagex; Settings.starty= E.touches? E.touches[0].pagey:e.originalevent.touches[0].pagey; }, "Touchmove":function(e) {e.stoppropagation (); E.preventdefault (); varCurX = e.touches? E.touches[0].pagex:e.originalevent.touches[0].pagex; varCurY = e.touches? E.touches[0].pagey:e.originalevent.touches[0].pagey; varMoveX = CurX-Settings.startx; varMovey = CurY-Settings.starty; varx = 0; //Avoid disabling the drop-down event if(Math.Abs (Movey) >Math.Abs (MoveX)) {Window.event.returnValue=true; } Else{e.preventdefault (); } x=-settings.index * Settings.width +MoveX; Dom.wrap.css ("-webkit-transform", "Translate3d (" + x + "px,0,0)"); }, "Touchend":function(e) {e.stoppropagation (); //E.preventdefault (); varCurX = e.changedtouches? E.changedtouches[0].pagex:e.originalevent.changedtouches[0].pagex; varMoveX = CurX-Settings.startx; if(Math.Abs (MoveX) >=settings.distance) {Settings.index= Settings.index-math.abs (MoveX)/MoveX;//judge whether to the left or the right } if(Settings.index >= settings.count-1) {Settings.index= Settings.count-1; } if(Settings.index <= 0) {Settings.index= 0; } funs.animate (); if(settings.autoscroll) {Settings.timer= SetInterval (funs.autoanimate, 3000); } }, "Touchcancel":function(e) {dom.wrap.css ("-webkit-transform", "Translate3d (" +-settings.index * settings.width + "px,0,0)"); }, "Webkittransitionend":function(e) {e.stoppropagation (); E.preventdefault (); Dom.wrap.removeClass ("Transitionable"); if(settings.infinite) {if(Settings.index <= 0) {Settings.index= Settings.count-2; } if(Settings.index >= settings.count-1) {Settings.index= 1; } } varItemIndex = Settings.infinite? Settings.index-1: Settings.index; Dom.pageItem.removeClass ("Active"); Dom.pageItem.eq ("+ ItemIndex +" "). AddClass (" Active "); Dom.wrap.css ("-webkit-transform", "Translate3d (" +-settings.index * settings.width + "px,0,0)"); } }); $ (window). On ({"Resize":function() {funs.adjustpos (); }, "Orientationchange":function() {funs.adjustpos (); })}, Adjustpos:function() {settings.width=dom.wrap.width (); Settings.distance= SETTINGS.WIDTH/5; Dom.wrap.css ("-webkit-transform", "Translate3d (" +-settings.index * settings.width + "px,0,0)"); }, animate:function() { varPageIndex =NULL; if(settings.infinite) {if(Settings.index = =settings.count) {PageIndex= 0; } Else if(Settings.index = = 0) {PageIndex= 2; } Else{PageIndex= Settings.index-1; }} dom.wrap.addClass ("Transitionable"); Dom.wrap.css ("-webkit-transform", "Translate3d (" +-settings.index * settings.width + "px,0,0)"); }, Autoanimate:function() { if(Settings.width > 0) {Settings.index++; Funs.animate (); } } }; Funs.init (); }); return This; } });}) (Window.jquery|| Window. Zepto);
Mobile-side Carousel Diagram Plugin (supports Zepto and jquery)