<!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>
<style>
*{
margin: 0;
padding: 0;
}
.one{
overflow: hidden;
position:relative;
width: 1280px;
height:200px;
margin: 200px auto 0;
background-color: red;
}
.one .imgs{
position:relative;
left: 0;
display: flex;
width: 100%;
height: 100%;
transition: .7s ease;
}
.imgs img{
display:inline-block;
width: 100%;
height: 100%;
object-fit: cover;
cursor: pointer;
}
.one a{
position:absolute;
top:50%;
display: block;
width: 40px;
color: white;;
background-color: rgba(0,0,0,.7);
line-height: 70px;
transform: translateY(-50%);
text-align: center;
text-decoration: none;
user-select: none;
font-size: 25px;
}
.left{
/* position: absolute; */
left:0;
}
.right{
/* position: absolute; */
right: 0;
}
</style>
</head>
<body>
<div class="one">
<div class="imgs">
<img src="1.jpg" alt="">
<img src="2.jpg" alt="">
<img src="3.jpg" alt="">
<img src="4.jpg" alt="">
<img src="5.jpg" alt="">
</div>
<div class="tab">
<a href="javascript:;" class="left"><</a>
<a href="javascript:;" class="right">></a>
</div>
</div>
<script>
let imgs=document.getElementsByClassName('imgs')[0];
let tab=document.getElementsByClassName('tab')[0];
let one=document.getElementsByClassName('one')[0];
let index=0;
console.log(index);
tab.firstElementChild.onclick=function(){
index++;
if(index>imgs.childElementCount){
imgs.style.left=0;
index=0;
imgs.style.transition="none";
}else{
imgs.style.left= - index * (one.offsetWidth/5) +"px";
}
// imgs.style.transition=".7s ease";
}
tab.lastElementChild.onclick=function(){
index++;
if(index===0){
imgs.style.left=0
index=0;
}else{
imgs.style.left=index * (one.offsetWidth/5) +"px";
}
}
</script>
</body>
</html>