上面的代码有bug
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
.lunboBlock {
width: 100%;
overflow: hidden;
position: relative;
}
.lunboBlock .imagesBlock ul{
padding: 0;
margin: 0;
position: relative;
width: 100%;
height: 400px;
}
.lunboBlock .imagesBlock ul li {
list-style-type: none;
position: absolute;
width: 1920px;
left: 50%;
margin-left: -960px;
}
.lunboBlock .dottedBlock {
position: absolute;
bottom: 30px;
width: 100px;
left: 50%;
margin-left: -50px;
}
.lunboBlock .dottedBlock ul,.lunboBlock .dottedBlock ul li {
list-style-type: none;
padding: 0;
margin: 0;
}
.lunboBlock .dottedBlock ul li {
margin-right: 3px;
cursor: pointer;
color: white;
font-size: 14px;
text-align: center;
line-height: 12px;
width: 10px;
height: 10px;
border: 2px solid white;
border-radius: 50%;
display: inline-block;
opacity: 0.4;
}
.lunboBlock .dottedBlock ul li.active {
background: white;
opacity: 1;
}
</style>
<script src="jquery-1.8.3.min.js"></script>
<script>
$(function(){
var current = 0,
$images_li = $('.lunboBlock .imagesBlock>ul>li'),
$dotted_li = $('.lunboBlock .dottedBlock>ul>li'),
length = $images_li.length;
$images_li.fadeOut(0).eq(current).fadeIn(0);
function show() {
if(current < length-1){
$images_li.fadeOut(1000);
current++;
$images_li.eq(current).fadeIn(1000);
$dotted_li.removeClass('active').eq(current).addClass('active');
}
else {
$images_li.fadeOut(1000);
current = 0;
$images_li.eq(current).fadeIn(1000);
$dotted_li.removeClass('active').eq(current).addClass('active');
}
}
actionId = setInterval(show, 2500);
$dotted_li.click(function(event) {
clearInterval(actionId);
$images_li.stop(true,true);
current = $(this).index()-1;
show();
actionId = setInterval(show, 2500);
});
});
</script>
</head>
<body>
<div class="lunboBlock">
<div class="imagesBlock">
<ul>
<li><img src="1.png" alt="首页轮播图"></li>
<li><img src="2.png" alt="首页轮播图"></li>
<li><img src="3.png" alt="首页轮播图"></li>
<li><img src="4.png" alt="首页轮播图"></li>
</ul>
</div>
<div class="dottedBlock">
<ul>
<li class="active"></li>
<li ></li>
<li ></li>
<li ></li>
</ul>
</div>
</div>
</body>
</html>
下面的代码通过siblings处理掉bug
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
.lunboBlock {
width: 100%;
overflow: hidden;
position: relative;
}
.lunboBlock .imagesBlock ul{
padding: 0;
margin: 0;
position: relative;
width: 100%;
height: 400px;
}
.lunboBlock .imagesBlock ul li {
list-style-type: none;
position: absolute;
width: 1920px;
left: 50%;
margin-left: -960px;
}
.lunboBlock .dottedBlock {
position: absolute;
bottom: 30px;
width: 80px;
left: 50%;
margin-left: -40px;
}
.lunboBlock .dottedBlock ul,.lunboBlock .dottedBlock ul li {
list-style-type: none;
padding: 0;
margin: 0;
}
.lunboBlock .dottedBlock ul li {
margin-right: 3px;
cursor: pointer;
color: white;
font-size: 14px;
text-align: center;
line-height: 12px;
width: 10px;
height: 10px;
border: 2px solid white;
border-radius: 50%;
display: inline-block;
opacity: 0.4;
}
.lunboBlock .dottedBlock ul li.active {
background: white;
opacity: 1;
}
</style>
<script src="jquery-1.8.3.min.js"></script>
<script>
$(function(){
var current = 0,
$images_li = $('.lunboBlock .imagesBlock>ul>li'),
$dotted_li = $('.lunboBlock .dottedBlock>ul>li'),
length = $images_li.length;
$images_li.fadeOut(0).eq(current).fadeIn(0);
function show() {
if(current < length-1){
current++;
$images_li.eq(current).fadeIn(1000).siblings().fadeOut(1000);
$dotted_li.removeClass('active').eq(current).addClass('active');
}
else {
current = 0;
$images_li.eq(current).fadeIn(1000).siblings().fadeOut(1000);
$dotted_li.removeClass('active').eq(current).addClass('active');
}
}
actionId = setInterval(show, 2500);
$dotted_li.click(function(event) {
clearInterval(actionId);
$images_li.stop(true,true);
current = $(this).index()-1;
show();
actionId = setInterval(show, 2500);
});
});
</script>
</head>
<body>
<div class="lunboBlock">
<div class="imagesBlock">
<ul>
<li><img src="1.png" alt="首页轮播图"></li>
<li><img src="2.png" alt="首页轮播图"></li>
<li><img src="3.png" alt="首页轮播图"></li>
<li><img src="4.png" alt="首页轮播图"></li>
</ul>
</div>
<div class="dottedBlock">
<ul>
<li class="active"></li>
<li ></li>
<li ></li>
<li ></li>
</ul>
</div>
</div>
</body>
</html>