jQuery 轮播图(setInterval)

CSS样式:

.ad { 
	margin-bottom:10px;
	width:586px; 
	height:150px; 
	overflow:hidden;
	position:relative;
}
.slider,. .num{
	position:absolute;
}
 .slider li{ 
	list-style:none;
	display:inline;
}
.slider img{ 
	width:586px; 
	height:150px;
	display:block;
}
 .num{ 
	right:5px; 
	bottom:5px;
}
 .num li{
	float: left;
	color: #FF7300;
	text-align: center;
	line-height: 16px;
	width: 16px;
	height: 16px;
	font-family: Arial;
	font-size: 12px;
	cursor: pointer;
	overflow: hidden;
	margin: 3px 1px;
	border: 1px solid #FF7300;
	background-color: #fff;
}
.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;
}

HTML代码:



            <div class="ad">
                <ul class="slider">
                    <li>
                        <img src="images/ads/1.gif" /></li>
                    <li>
                        <img src="images/ads/2.gif" /></li>
                    <li>
                        <img src="images/ads/3.gif" /></li>
                    <li>
                        <img src="images/ads/4.gif" /></li>
                    <li>
                        <img src="images/ads/5.gif" /></li>
                </ul>
                <ul class="num">
                    <li class="on">1</li>
                    <li>2</li>
                    <li>3</li>
                    <li>4</li>
                    <li>5</li>
                </ul>
            </div>


jQuery代码:

//获取图片的高度
            var height = $(".content_right .ad").height();
            //获取图片的数量
            var imgcount = $(".slider li").length;
            // 设置索引值
            var index = 0;
            //设置定时器
            function time() {
                timer = setInterval(function () {
                    //引用数字随图片一起显示
                    changeImg(index);
                    index++;
                    //当索引值等于图片的数量是索引变成0
                    if (index == imgcount) {
                        index = 0;
                    }
                }, 1500);

            }
            time();
           //数字随图片一起显示
            function changeImg(index) {
                //改变图片的top值实现滚动
                $(".slider").stop(true, false).animate({ top: -height * index }, 1200);
                //先移除on再更趋添加on
                $(".num li").removeClass("on").eq(index).addClass("on");
            }
            //鼠标移到数字显示图片
            $(".num li").mouseover(function () {
                //索引等于当前索引
                index = $(this).index();
                //引用数字随图片一起显示
                changeImg(index);
            });
            //鼠标移入移出轮播图停止和启动
            $(".ad").hover(function () {
                clearInterval(timer);
            }, function () {
                time();
            });



猜你喜欢

转载自blog.csdn.net/mogul1/article/details/80839225