jq实现轮播

来源:互联网 发布:深圳华强北数据恢复 编辑:程序博客网 时间:2024/06/05 02:19
<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script src="js/jquery-2.2.3.js"></script>
<style type="text/css">
* {
padding: 0;
margin: 0;
}

.body {
position: relative;
width: 500px;
height: 300px;
margin: 60px auto;
}

.img-box {
width: 500px;
height: 300px;
overflow: hidden;
margin: 60px auto;
}

.img {
width: 500px;
height: 300px;
float: left;
}

.img img {
width: 100%;
height: 100%;
}

.tabs {
width: 200px;
margin: 0 auto;
position: relative;
top: -50px;
}

.tab {
float: left;
width: 30px;
height: 30px;
background: #FF06FF;
text-align: center;
line-height: 30px;
margin-left: 10px;
color: #fff;
}

.on {
background: #007AFF;
}

.btn{
width: 30px;
height: 50px;
background: rgba(0,0,0,0.7);
color: #FFFFFF;
text-align: center;
line-height: 50px;
float: left;
position: absolute;
top: 50%;
margin-top: -25px;
font-size: 30px;
}
.btn:hover{
background: #FF06FF;
}
.btn2{
right: 0;
}
</style>
</head>


<body>
<div class="body">
<div class="img-box">
<div class="img"><img src="images/chun.jpg" /></div>
<div class="img"><img src="images/xia.jpg" /></div>
<div class="img"><img src="images/qiu.jpg" /></div>
<div class="img"><img src="images/dong.jpg" /></div>
</div>
<div class="btn-box">
<div class="btn btn1"><</div>
<div class="btn btn2">></div>
</div>
<div class="tabs">
<div class="tab on">1</div>
<div class="tab">2</div>
<div class="tab">3</div>
<div class="tab">4</div>
</div>
</div>


<script type="text/javascript">
$(".img").eq(0).show().siblings().hide();
var i = 0;
var timer
showTime();


$(".tab").hover(function() {
clearInterval(timer);
i = $(this).index();
show();
}, function() {
showTime();


})

$(".btn1").click(function(){
clearInterval(timer);
if(i == 0){
i = 4;
}
i--;
show();
showTime();
});

$(".btn2").click(function(){
clearInterval(timer);
if(i == 3){
i = -1;
}
i++;
show();
showTime();
});


function show() { //封装显示隐藏函数
$(".img").eq(i).fadeIn(200).siblings().fadeOut(200);
$(".tab").eq(i).addClass("on").siblings().removeClass("on");
};


function showTime() {
timer = setInterval(function() {
i++;
if(i == 4) {
i = 0;
}
show();
}, 2000);
}
</script>
</body>


</html>
1 0
原创粉丝点击