代码

<!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">&lt</a>
<a href="javascript:;" class="right">&gt</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>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容