简单的轮播图
html代码
<!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{
width: 613px;
height: 230px;
margin: 0 auto;
position: relative;
}
.box img{
width: 100%;
height: 100%;
position: absolute;
top:0;
left: 0;
}
.ctrols{
width: 112px;
height: 16px;
position: absolute;
bottom: 26px;
right: 60px;
z-index: 999;
}
.ctrols span{
display: inline-block;
border: 2px solid #fff;
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 8px;
background-color: rgb(0,0,0,.2);
}
.box img:nth-child(1){
z-index: 1;
}
.ctrols span:nth-child(5){
margin-right: 0;
}
.ctrols span.on{
background-color: orange;
}
.arrow-left ,.arrow-right{
position: absolute;
top: 50%;
width: 30px;
height: 50px;
margin-top: -25px;
background-color:rgb(0,0,0,.2);
z-index: 10;
font-size: 30px;
color: #fff;
text-align: center;
line-height: 50px;
}
.arrow-left{
left: 0;
}
.arrow-right{
right: 0;
}
</style>
</head>
<body>
<div class="box">
<img src="images/img01.jpg"/>//所用图片大家可以随意改变
<img src="images/img02.jpg"/>
<img src="images/img03.jpg"/>
<img src="images/img04.jpg"/>
<img src="images/img05.jpg"/>
<div class="ctrols">
<span class="on"></span><span></span><span></span><span></span><span></span>
</div>
<div class="arrow">
<div class="arrow-left"><</div>
<div class="arrow-right">></div>
</div>
</div>
</body>
</html>
js代码
<script>
var spans = document.getElementsByTagName("span");
var imgs = document.getElementsByTagName("img");
var arrowLeft = document.getElementsByClassName("arrow-left")[0];
var arrowRight = document.getElementsByClassName("arrow-right")[0];
var step = 0;
for(var i=0; i<spans.length;i++){
spans[i].idx = i;
spans[i].onclick = function(){
step = this.idx;
clearInterval(timer);
changeImg(this.idx);
}
}
var timer = setInterval(function(){
step++;
if(step === 5){
step = 0;
}
changeImg(step);
},2000);
function changeImg(a){
for(var j=0;j<imgs.length;j++){
imgs[j].style.display = "none";
spans[j].className = "";
}
imgs[a].style.display = "block";
spans[a].className = "on";
}
arrowLeft.onclick = function(){
clearInterval(timer);
step--;
if(step<0){
step = 4;
}
changeImg(step);
}
arrowRight.onclick = function(){
clearInterval(timer);
step++;
if(step>4){
step = 0;
}
changeImg(step);
}
</script>