When a function is called, it is OK to pass four necessary arguments.
Parameter 1: the UL to move;
Parameter 2: Left button;
Parameter 3: Right button;
Parameter 4: Motion speed; (optional, default 3s)
The code is as follows:
HTML section:
<div class= "Div1" > <ul class= "ul1" > <li>1111111111</li> <li>2222222222</li> <li>3333333333</li> <li>4444444444</li> <li>5555555555</li> & lt;/ul></div><div> <input id= "btn1" type= "button" value= "←" > <input id= "btn2" type= "button" Value= "→" ></div>
CSS section:
* {margin:0; padding:0;} body {text-align:center;}. Div1 {overflow:hidden; width:350px; height:160px; margin:0 auto; border:3px solid #f00; position:relative;}. ul1 {position:absolute; left:0; top:0;}. Ul1 li {width:100px; height:150px; float:left; list-style:none;}
JS section:
var imgscroll = { //variables oUl : null, oLiWidth : 0, Olilength : 0, btnleft : null, btnright : null, iindex : 0, timer : null, speed : 0, //functions move : function () { //copies the Li this.oul.children (' Li ') inside the UL. Clone (). AppendTo (This.oul); this.oliwidth = this.oul.children (' Li '). Width (); this.olilength = This.oUl.children (' Li '). length; //Set ul width this.oul.width (this.oliwidth* This.olilength) var self = this; //left button This.btnLeft.bind (' click ', Function () { clearinterval (Self.timer); if ( SELF.IINDEX==SELF.OLILENGTH/2) { self.iindex = 0; Self.oUl.stop (true,true). CSS (' left ', ' 0 '); }; self.iindex++; self.oul.stop (true,true). Animate ({Left:-self.iIndex *self.oliwidth},self.automove ()); }); //right button this.btnright.bind (' Click ', function () { clearinterval (Self.timer); if (self.iindex==0) { self.iindex = self.olilength/2; self.oul.stop (true,true). CSS (' left ',-self.iindex*self.oliwidth); }; self.iindex--; Self.oUl.stop (true,true). Animate ({left:-self.iindex*self.oliwidth},self.automove ()); }); }, //Automatic Carousel automove : function () { var self = this; this.timer = setinterval (function () { if (SELF.IINDEX==SELF.OLILENGTH/2) { self.iindex = 0; self.oul.stop (true,true). CSS (' left ', ' 0 '); }; &Nbsp; self.iindex++; self.oul.animate ({left:-self.iindex*self.oliwidth}); }, Self.speed); }, //initialization (Motion UL, left button, right button) init : function (oul, btnleft, Btnright, speed) { this.oul = oul; this.btnleft = btnleft; this.btnRight = btnRight; this.speed = speed ? speed*1000 : 3000; this.move (); this.automove (); }};//calls Imgscroll.init ($ ('. Ul1 '), $ (' #btn1 '), $ (' #btn2 '), 2);
There is no right or can optimize the place, please correct me, thank you!
jquery Carousel Diagram Function encapsulation