This article from: http://www.cnblogs.com/babyzone2004/archive/2010/08/30/1812682.html
[Instance demo]
It is really convenient to operate Dom with jquery, and jquery provides a very user-friendly API to meet our various needs. The selector is particularly outstanding in this example-"jquery achieves image carousel effect. Greatly simplifies JSCode.
[Principles]
Here we will talk about the entire process:
1. Hide all images except the first one,
2. Obtain the alt information of the first image and display it in the Information column. Then, add a click event.
3. Add a listener for the four buttons, click the corresponding button, and use the fadeout and fadein methods to display the image.
4. Set setinterval and regularly execute the switching function.
[Code description]
Filter (": visible"): Get all visible elements
Unbind (): deletes the Bound event from the matched element.
Siblings: gets a set of all unique peer elements of each element in a matched element set.
For example, find all peer elements of each Div with the class name selected.
<P> Hello </P> <div> <span> hello again </span> </div> <p> and again </P>
Run $ ("Div"). siblings () and the result is
[<P> Hello </P>, <p> and again </P>]
【ProgramSource code]
HTML section:
<Body>
<Div id = "banner">
<Div id = "banner_bg"> </div> <! -- Title background -->
<Div id = "banner_info"> </div> <! -- Title -->
<Ul>
<Li class = "on"> 1 </LI>
<Li> 2 </LI>
<Li> 3 </LI>
<Li> 4 </LI>
</Ul>
<Div id = "banner_list">
<A href = "#" target = "_ blank"> </a>
<A href = "#" target = "_ blank"> </a>
<A href = "#" target = "_ blank"> </a>
<A href = "#" target = "_ blank"> </a>
</Div>
</Div>
</Body>
CSS section:
<Style type = "text/CSS">
# Banner {position: relative; width: 478px; Height: 286px; Border: 1px solid #666; overflow: hidden ;}
# Banner_list IMG {border: 0px ;}
# Banner_bg {position: absolute; bottom: 0; Background-color: #000; Height: 30px; filter: alpha (opacity = 30); opacity: 0.3; Z-index: 1000;
Cursor: pointer; width: 478px ;}
# Banner_info {position: absolute; bottom: 0; left: 5px; Height: 22px; color: # FFF; Z-index: 1001; cursor: pointer}
# Banner_text {position: absolute; width: 120px; Z-index: 1002; Right: 3px; bottom: 3px ;}
# Banner ul {position: absolute; List-style-type: none; filter: alpha (opacity = 80); opacity: 0.8; Border: 1px solid # FFF; Z-index: 1002;
Margin: 0; padding: 0; bottom: 3px; Right: 5px ;}
# Banner ul Li {padding: 0px 8px; float: Left; display: block; color: # FFF; Border: # e5eaff 1px solid; Background: #6f4f67; cursor: pointer}
# Banner ul Li. On {Background: #900}
# Banner_list A {position: absolute ;}<! -- Make all four images overlap -->
</Style>
JS section:
<SCRIPT type = "text/JavaScript">
VaR t = n = 0, count;
$ (Document). Ready (function (){
Count = $ ("# banner_list a"). length;
$ ("# Banner_list A: Not (: First-child)"). Hide ();
$ ("# Banner_info" ).html ($ ("# banner_list A: First-Child"). Find ("IMG"). ATTR ('alt '));
$ ("# Banner_info "). click (function () {window. open ($ ("# banner_list A: First-Child "). ATTR ('href '), "_ blank ")});
$ ("# Banner li"). Click (function (){
VaR I = $ (this). Text ()-1; // obtain the value in the Li element, that is, 1, 2, 4
N = I;
If (I> = count) return;
$ ("# Banner_info" ).html ($ ("# banner_list a"). eq (I). Find ("IMG"). ATTR ('alt '));
$ ("# Banner_info "). unbind (). click (function () {window. open ($ ("# banner_list "). eq (I ). ATTR ('href '), "_ blank ")})
$ ("# Banner_list a"). Filter (": visible"). fadeout (500). Parent (). Children (). eq (I). fadein (1000 );
Document. getelementbyid ("banner"). style. Background = "";
$ (This). toggleclass ("On ");
$ (This). siblings (). removeattr ("class ");
});
T = setinterval ("showauto ()", 4000 );
$ ("# Banner"). Hover (function () {clearinterval (t)}, function () {T = setinterval ("showauto ()", 4000 );});
})
Function showauto ()
{
N = n> = (count-1 )? 0: ++ N;
$ ("# Banner li"). eq (N). Trigger ('click ');
}
</SCRIPT>
Attachment: source code download
Jquery achieves image carousel Effect
Oak hut
From nationWanhe City