<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="banner.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script src="banner.js"></script>
</head>
<body>
<div class="content">
<div class="list">
<img src="images/01.png" alt="">
<img src="images/02.png" alt="">
<img src="images/03.png" alt="">
<img src="images/04.png" alt="">
<img src="images/05.png" alt="">
</div>
<button id="left"></button>
<button id="right"></button>
<div id="btns">
<span class="current"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
</body>
</html>
*{margin: 0;padding: 0;}
.content{
position: relative;
width: 600px;
height: 400px;
margin-left: auto;
margin-right: auto;
border: 1px solid #333;
}
.list{
position: absolute;
width: 600px;
height: 400px;
}
.list img,#left{
position: absolute;
}
.list img:first-child{
z-index: 100;
}
#left,#right{
cursor: pointer;
top:50%;
transform: translateY(-50%);
z-index: 101;
width:40px;
height:70px;
background:url("images/icon-slides.png");
border:none;
position: absolute;
}
#left{
background-position-x:-86px
}
#left:hover{
background-position: 0;
}
#right:hover{background-position-x: -43px}
#right{
position: relative;
left: 560px;
background-position-x:-125px;
}
#btns{
z-index: 101;
transform: translateX(40%);
bottom: 20px;
position: relative;
top: 300px;
}
#btns>span{
cursor: pointer;
width:20px;
height:20px;
z-index: 101;
display: inline-block;
border-radius: 50%;
border:1px solid #fff;
background-color:rgba(44,44,44,.3);
}
#btns .current{
background:orangered;
}
$(document).ready(function(){
var index =0;
/* 1.点击左边的按钮,每次index++*/
$("#right").click(function(){
/**1.定义下标,每次点击的时候,让index++ */
index++;
if(index>4){
index =0
}
console.log(index)
imgToggle(index,1000)
})
/* 2.点击左边的按钮,每次index--*/
$("#left").click(function(){
index--;
if(index<0){
index=4;
}
console.log(index)
imgToggle(index,1000);
})
/*点击对应焦点,对应的图片显示 */
$("#btns span").click(function(){
var num = $(this).index();
console.log(num)
/*重置下标 */
index = num;
imgToggle(num,1000)
})
/*封装的思想:将公用的部分封装在一起 */
function imgToggle(index,speed){
/**让对应的下标的焦点添加class=current,让其兄弟元素class="" */
$("#btns span").eq(index).addClass("current").siblings().removeClass("current");
/**让对应的下标的图片显示,其他图片隐藏 */
$(".list img").eq(index).fadeIn(speed).siblings().fadeOut(speed);
}
function slide(){
time = setInterval(function(){
// 调用右键的函数
$("#next").click();
},2000)
}
slide();
})