来看其实现过程
创建如下html结构
- <div id="carousel">
- <div id="buttons">
- <a href="#" id="prev">prev</a>
- <a href="#" id="next">next</a>
- <div class="clear"></div>
- </div>
- <div class="clear"></div>
- <div id="slides">
- <ul>
- <li><img src="slide1.gif" width="240" height="240" alt="Slide 1"/></li>
- <li><img src="slide2.gif" width="240" height="240" alt="Slide 2"/></li>
- <li><img src="slide3.gif" width="240" height="240" alt="Slide 3"/></li>
- </ul>
- <div class="clear"></div>
- </div>
- </div>
不算复杂的结构。#buttons为按钮容器,#slides为图片容器。
来看css代码
- #carousel {
- width:255px;
- height:290px;
- margin:0 auto;
- }
- #slides {
- overflow:hidden;
- /* fix ie overflow issue */
- position:relative;
- width:250px;
- height:250px;
- border:1px solid #ccc;
- }
- /* remove the list styles, width : item width * total items */
- #slides ul {
- position:relative;
- left:0;
- top:0;
- list-style:none;
- margin:0;
- padding:0;
- width:750px;
- }
- /* width of the item, in this case I put 250x250x gif */
- #slides li {
- width:250px;
- height:250px;
- float:left;
- }
- #slides li img {
- padding:5px;
- }
- /* Styling for prev and next buttons */
- #buttons {
- padding:0 0 5px 0;
- float:right;
- }
- #buttons a {
- display:block;
- width:31px;
- height:32px;
- text-indent:-999em;
- float:left;
- outline:0;
- }
- a#prev {
- background:url(arrow.gif) 0 -31px no-repeat;
- }
- a#prev:hover {
- background:url(arrow.gif) 0 0 no-repeat;
- }
- a#next {
- background:url(arrow.gif) -32px -31px no-repeat;
- }
- a#next:hover {
- background:url(arrow.gif) -32px 0 no-repeat;
- }
- .clear {clear:both}
来看其javascript
- $(document).ready(function() {
- //图片切换的时间间隔
- var speed = 5000;
- var run = setInterval('rotate()', speed);
- //获取图片容器li的盒宽度(包含外边距)
- var item_width = $('#slides li').outerWidth();
- var left_value = item_width * (-1);
- //将最后一个li移到第一个li前面
- $('#slides li:first').before($('#slides li:last'));
- //设置整个图片容器的left值为负的li宽度
- $('#slides ul').css({'left' : left_value});
- //单击前一页触发的事件
- $('#prev').click(function() {
- //获取移动的值
- var left_indent = parseInt($('#slides ul').css('left')) + item_width;
- //滚动图片容器
- $('#slides ul').animate({'left' : left_indent}, 200,function(){
- //将最后一个li移到第一个li前面
- $('#slides li:first').before($('#slides li:last'));
- //设置ul的left值
- $('#slides ul').css({'left' : left_value});
- });
- //取消按钮默认动作
- return false;
- });
- //与上面类似
- $('#next').click(function() {
- //get the right position
- var left_indent = parseInt($('#slides ul').css('left')) - item_width;
- //slide the item
- $('#slides ul').animate({'left' : left_indent}, 200, function () {
- //move the first item and put it as last item
- $('#slides li:last').after($('#slides li:first'));
- //set the default item to correct position
- $('#slides ul').css({'left' : left_value});
- });
- //cancel the link behavior
- return false;
- });
- $('#slides').hover(
- function() {
- clearInterval(run);
- },
- function() {
- run = setInterval('rotate()', speed);
- }
- );
- });
- function rotate() {
- $('#next').click();
- }
已经做了注释,不再解释,有问题可给我留言。
原文:http://www.queness.com/post/923/create-a-simple-infinite-carousel-with-jquery

