<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.show{
width: 500px;
height: 300px;
border: 1px solid red;
position: relative;
}
span{
width: 15px;
height: 25px;
display: inline-block;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
}
.pre{
position: absolute;
left:0;
top: 135px;
}
.next{
position: absolute;
right: 0;
top: 135px;
}
.show img{
width: 500px;
height: 300px;
display: none;
}
</style>
</head>
<body>
<div class="show">
<img src="./img/1.jpeg" alt="">
<img src="./img/2.gif" alt="">
<img src="./img/3.jpg" alt="">
<img src="./img/4.jpg" alt="">
<span class="pre"> <</span>
<span class="next">></span>
</div>
<script>
// 通过class类名获取document元素数组,通过数组获取dom对象
var pre = document.getElementsByClassName('pre')[0]
var next = document.getElementsByClassName('next')[0]
// 通过标签名字获取dom元素【数组】
var imgs = document.getElementsByTagName('img')
function swiper(){
var index = 0;
imgs[0].style.display = 'block'
next.onclick = function(){
imgs[index].style.display = 'none'
++index
if(index >3){
index = 0;
}
imgs[index].style.display = 'block'
}
pre.onclick = function(){
imgs[index].style.display = 'none'
--index
if(index < 0){
index = 3 ;
}
imgs[index].style.display = 'block'
}
}
swiper()
</script>
</body>
</html>
以上的轮播图仅供参考