2. Next, let's define a HTML structure for our carousel. What's it going to need?
•
We'll need a
container
element to hold both the carousel itself and some navigation links
•
We'll also need to include another div for the
carousel
•
Inside the
carousel
, we'll want to define some elements in a
list
that we can scroll between. Forthis we're going to use a <ul> list with <li> list elements
•
You can include anything you want inside these <li>s but for this article, we're going to useimages so lets also include some <img> elements
<div id="container"> <div id="carousel"> <ul> <li><img src="image1.jpg" /></li> <li><img src="image2.jpg" /></li> <li><img src="image3.jpg" /></li> <li><img src="image4.jpg" /></li> <li><img src="image5.jpg" /></li> <li><img src="image6.jpg" /></li> <li><img src="image7.jpg" /></li> <li><img src="image8.jpg" /></li> </ul> </div> <a id="navPrev" href="#">Next</a> <a id="navNext" href="#">Previous</a> </div>
The JavaScript (using jQuery)
3. Next, lets define two navigation buttons (a previous and next) that will allow you to move to both endsof the carousel.
<script type="text/javascript"> $(document).ready(function(){//Define the animation speed for the Carousel var speed = 600;$('#navPrev').click(function(){//As the rest of our carousel is hidden, lets move it's margin left untilit's in view//We use the jQuery animate() function give this movement a nice smooth feel$('#carousel ul').animate({marginLeft:'-280px'}, speed);});$('#navNext').click(function(){//And now lets move back to the start of the Carousel$('#carousel ul').animate({marginLeft:'1px'}, speed);});}); </script>
Add a Comment
Anon Rayleft a comment
earlybackpack73left a comment