JS轮播图

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>轮播图实现</title>
<style type="text/css">
/*标题样式*/
p {
text-align: center;
font-size: 25px;
color: cadetblue;
font-family: fantasy;
}

.imgBox {
border-top: 2px solid cadetblue;
50%;
height: 500px;
margin: 0 auto;
}

.imgBox img {
60%;
height: 300px;
margin: 0 auto;
padding-top: 30px;
}

.img1 {
display: block;
}

.img2 {
display: none;
}

.img3 {
display: none;
}
</style>
</head>

<body>
<p>图片轮播</p>
<div class="imgBox">
<img class="img-slide img1" src="../img/1.jpg" alt="1">
<img class="img-slide img2" src="../img/2.jpg" alt="2">
<img class="img-slide img3" src="../img/3.jpg" alt="3">
</div>
</body>
<script type="text/javascript">
var index = 0;
//改变图片
function ChangeImg() {
index++;
var a = document.getElementsByClassName("img-slide");
if(index >= a.length) index = 0;
for(var i = 0; i < a.length; i++) {
a[i].style.display = 'none';
}
a[index].style.display = 'block';
}
//设置定时器,每隔两秒切换一张图片
setInterval(ChangeImg, 2000);
</script>
</html>

原文地址:https://www.cnblogs.com/qq1561942060/p/13606171.html