css样式
*{margin:0;padding:0}
.content{
cursor: pointer;
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,#prev,#next,#btns{
position: absolute;
}
#list img:not(:first-child){
display: none;
}
#prev,#next{
cursor: pointer;
top:50%;transform: translateY(-50%);
z-index: 100;width:40px;height:70px;
background:url("../images/icon-slides.png");
border:none;
}
#prev{
background-position-x:-86px
}
#prev:hover{
background-position: 0;
}
#next:hover{background-position-x: -43px}
#next{
right:0;
background-position-x:-125px;
}
#btns{z-index: 101;
transform: translateX(-50%);
bottom: 20px;left:50%;}
#btns .current{
background:orangered;
}
#btns>span{
cursor: pointer;
width:20px;
height:20px;
display: inline-block;
border-radius: 50%;
border:1px solid #fff;
background-color:rgba(44,44,44,.3);
}
<div class="content">
<div id="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="prev"></button>
<button id="next"></button>
<div id="btns">
<span class="current"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<script>
var prev=document.getElementById("prev");
var next=document.getElementById("next");
var lists=document.querySelectorAll("#list>img");
var icons=document.querySelectorAll("#btns>span")
var index=0; //index 记录下标
next.onclick=function(){
index++;
if(index>4){
index=0;
}
console.log(index)
for(let i=0;i<lists.length;i++){ //遍历图片
lists[i].style.display="none" //让所有图片都隐藏
}
lists[index].style.display="block" //只让当前下标下的图片显示
for(let i=0;i<icons.length;i++){ //遍历焦点
icons[i].classList.remove("current") //移去所有焦点
}
lists[index].classList.add("current") //只让当前下标下的焦点增加
}
prev.onclick=function(){
index--;
if(index<0){
index=4;
}
for(let i=0;i<lists.length;i++){
lists[i].style.display="none";
}
lists[index].style.display="block"
for(let i=0;i<icons.length;i++){
icons[i].classList.remove("current")
}
lists[index].classList.add("current")
}
/* 下标重置 */
for(let i=0;i<icons.length;i++){ //遍历焦点
icons[i].index=i;
icons[i].onclick=function(){ //给焦点一个点击事件
let currentIndex=this.index; //指向当前index
for(let i=0;i<lists.length;i++){
lists[i].style.display="none";
}
lists[currentIndex].style.display="block"
for(let i=0;i<icons.length;i++){
icons[i].classList.remove("current")
}
icons[currentIndex].classList.add("current")
}
}
</script>