jquery之无缝轮播

html文件 (图片可更改)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/style.css"/>
		<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div class="box">
			<ul class="showList" id="list">
				<li><img src="img/01.jpg"/></li>
				<li><img src="img/02.jpg"/></li>
				<li><img src="img/03.jpg"/></li>
				<li><img src="img/04.jpg"/></li>
				<li><img src="img/05.jpg"/></li>
				
			</ul>
			<!-- 角标 -->
			<ul class="iconList">
				<li>1</li>
				<li>2</li>
				<li>3</li>
				<li>4</li>
				<li>5</li>
			</ul>
			<!-- 左右按钮 -->
			<div id="btns">
				<span class="prev">&lt;</span>
				<span class="next">&gt;</span>
			</div>
		</div>
	</body>
</html>

css文件

* {
	padding: 0;
	margin: 0;
}

a {
	text-decoration: noen;
}

li {
	list-style: none;
}

.box {
	width: 500px;
	height: 300px;
	margin: 100px auto;
	position: relative;
	overflow: hidden;
}

.showList {
	position: absolute;
}

.box .showList li {
	width: 600px;
	height: 300px;
	float: left;
}

.box .showList li img {
	width: 600px;
	height: 300px;
}

/*角标 */
.box .iconList {
	height: 20px;
	z-index: 99;
	position: absolute;
	bottom: 20px;
	left: 190px;
}

.box .iconList li {
	width: 20px;
	height: 20px;
	float: left;
	background: #fff;
	text-align: center;
	border: 2px solid #ccc;
	border-radius: 50%;
	margin: 5px 2px;
	cursor: pointer;
}

.box .iconList li.select {
	border-color: #fff;
	background: red;
}

/*左右按钮*/ 
.box #btns span{
	display: none;
	position: absolute;
	width: 50px;
	height: 80px;
	background: rgba(0,0,0,0.3);
	color: #fff;
	top:37%;
	font-size: 40px;
	text-align: center;
	line-height:80px;
	cursor: pointer;
} 
#btns .next{
	right:10px;
}
#btns .prev{
	left: 10px;
}

js文件

$(function(){
	var firstImg = $('#list li').first().clone();
	$('#list').append(firstImg).width($('#list li').length*$('#list img').width());
	var count=0;
	var timer;
	autoTime();
	
	// 下一张
	$('.next').click(function(){
	    count++;
	    moveImg(count);
	})
	 
	// 上一张
	$('.prev').click(function(){
	    count--;
	    moveImg(count);
	})
	
	//定时器
	 function autoTime(){
	    timer = setInterval(function(){
	        count++;
	        moveImg(count);
	    }, 3000);
	 
	}
	//封装move函数
	function moveImg(num){
	 
	    // 如果是最后一张图片我们怎么办
	    if(count==$('#list li').length){
	        count = 1;
	        $('#list').css({left:0});
	    }
	 
	    // 是第一张
	    if(count==-1){
	        count=$('#list li').length-2;
	        $('#list').css({left:($('#list li').length-1)*-600});
	    }
	 
	    // 移动图片
	    $('#list').stop().animate({left:count*-600},800);
	 
	    // 角标
	    if(count==($('#list li').length-1)){
	        $('.iconList li').eq(0).addClass('select').siblings().removeClass('select');
	    }else{
	        $('.iconList li').eq(count).addClass('select').siblings().removeClass('select');
	    }
	}
	 
	//移入box清楚定时器,左右按钮显示出来
	$('.box').mouseover(function(){
	    $('.prev').show();
	    $('.next').show();
	    clearInterval(timer);
	}).mouseout(function(){
	    $('.prev').hide();
	    $('.next').hide();
	    autoTime();
	})
	//点击角标
	$('.iconList li').click(function(){
	    count = $(this).index();   
	    moveImg(count);
	})
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值