修正:图片使用background-image: url();
.item:nth-child(1){ background-image: url(); }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播图</title>
<style>
body{
background-color: rgba(0, 110, 255, 0.1);
}
.contanier{
width: 500px;
height: 300px;
position: relative;
margin-left: 370px;
}
.item{
width: 500px;
height: 300px;
position: absolute;
opacity: 0;
}
img{
width: 500px;
height: 300px;
}
.active{
z-index: 1;
opacity: 1;
transition: opcacity 500ms linear;
}
/* 按钮 */
.pre{
content: '<';
display: block;
position: absolute;
top: 90px;
left: -50px;
height: 100px;
line-height: 100px;
text-align: center;
width: 60px;
font-size: 60px;
font-weight:400;
color:#ffffff;
cursor: pointer;
z-index: 6;
}
.next{
content: '>';
display: block;
position: absolute;
top: 90px;
right: -50px;
height: 100px;
line-height: 100px;
text-align: center;
width: 60px;
font-size: 60px;
font-weight:400;
color:#ffffff;
cursor: pointer;
z-index: 6;
}
</style>
</head>
<body>
<div class="contanier">
<div class="item active" data-index=1><img src="img/85-1.jpg" alt=""></div>
<div class="item" data-index=2><img src="img/小王.jpg" alt=""></div>
<div class="item" data-index=3><img src="img/85-2.jpg" alt=""></div>
<div class="item" data-index=4><img src="img/85-3.jpg" alt=""></div>
<div class="item" data-index=5><img src="img/85.jpg" alt=""></div>
<!-- 小按钮可以点击前后 -->
<div class="pre"><</div>
<div class="next">></div>
</div>
<script src="../jquery.js"></script>
<script>
//索引
function next(index){
index = Number(index);
if($(".item").length===index){
return 1;
}
return index+1;
}
//去下一页的方法
function goNext(){
//找到有active的元素
var active=$('.active');
//找到他的下标
var index=active.attr('data-index');
//删除他的active样式
active.removeClass('active');
//给下一个元素添加active样式
$(".item[data-index="+next(index)+"]").addClass('active');
}
//调用方法
$('.next').click(function(){
goNext();
});
//去上一页索引
function pre(index){
index = Number(index);
if(index===1){
return $('.item').length;
}
return index-1;
}
//去上一页的方法
function goPre(){
//找到有active的元素
var active=$('.active');
//找到他的下标
var index=active.attr('data-index');
//删除他的active样式
active.removeClass('active');
//给下一个元素添加active样式
$(".item[data-index="+pre(index)+"]").addClass('active');
}
//调用方法
$('.pre').click(function(){
goPre();
});
//周期轮播的方法
setInterval(goNext,3000);
</script>
</body>
</html>