Implementation of jQuery image carousel _ jquery-js tutorial

Source: Internet
Author: User
I have encountered jQuery to implement automatic list scrolling. This image carousel has the same principle and only has some minor differences, the sequence number of the current image must be displayed in the lower right corner of the image as follows:

Let's take a look at the html code and the corresponding css code:

The Code is as follows:











  • 1

  • 2

  • 3

  • 4

  • 5




The Code is as follows:


# ScrollPics {
Height: 150px;
Width: 100%;
Margin-bottom: 10px;
Overflow: hidden;
Position: relative;
}
. Num {
Position: absolute;
Right: 5px;
Bottom: 5px;
}
# ScrollPics. num li {
Float: left;
Color: # FF7300;
Text-align: center;
Line-height: 16px;
Width: 16px;
Height: 16px;
Cursor: pointer;
Overflow: hidden;
Margin: 3px 1px;
Border: 1px solid # FF7300;
Background-color: # fff;
}
# ScrollPics. num li. on {
Color: # fff;
Line-height: 21px;
Width: 21px;
Height: 21px;
Font-size: 16px;
Margin: 0 1px;
Border: 0;
Background-color: # FF7300;
Font-weight: bold;
}

Use absolute positioning to set the position of num in the list, and set the relevant style for li. on indicates that the style category of li in the number list corresponding to the image is displayed.

The following is the js Code:

The Code is as follows:


// Rolling Advertisement
Var len = $ (". num> li"). length;
Var index = 0; // image serial number
Var adTimer;
$ (". Num li"). mouseover (function (){
Index = $ (". num li"). index (this); // you can call this operation to retrieve the index of li.
ShowImg (index );
}). Eq (0). mouseover ();
// Slide in to stop the animation and slide out to start the animation.
$ ('# ScrollPics'). hover (function (){
ClearInterval (adTimer );
}, Function (){
AdTimer = setInterval (function (){
ShowImg (index)
Index ++;
If (index = len) {// go to the first image after the last image
Index = 0;
}
},3000 );
}). Trigger ("mouseleave ");

Function showImg (index ){
Var adHeight = $ ("# scrollPics> ul> li: first"). height ();
$ (". Slider"). stop (true, false). animate ({
"MarginTop":-adHeight * index + "px" // you can change the value of the marginTop attribute to achieve carousel effect.
},1000 );
$ (". Num li"). removeClass ("on ")
. Eq (index). addClass ("on ");
}

Related Article

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.