jquery Carousel Diagram Function encapsulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.