效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
.box{
margin:0 auto;
width: 800px;
height: 650px;
background: #6274b6;
position: relative;
padding: 20px;
text-align: center;
}
</style>
<script>
window.onload=function(){
var prev=document.getElementById("prev");
var next=document.getElementById("next");
var img=document.getElementsByTagName("img")[0];
var imgArr=["../images/1.jpg","../images/2.jpg","../images/3.jpg","../images/4.jpg","../images/5.jpg"];
var index=0;
var info=document.getElementById("info");
info.innerHTML=(index+1)+" / "+imgArr.length;
prev.onclick=function(){
index--;
if(index<0){
index=imgArr.length -1;
}
img.src=imgArr[index];
info.innerHTML=(index+1)+" / "+imgArr.length;
} ;
next.onclick=function(){
index++;
if(index>imgArr.length -1){
index=0;
}
img.src=imgArr[index];
info.innerHTML=(index+1)+" / "+imgArr.length;
};
}
</script>
</head>
<body>
<div class="box">
<p id="info"></p>
<img src="../images/1.jpg" alt="yhy" width="800" height="500">
<button id="prev">PREV</button>
<button id="next">NEXT</button>
</div>
</body>
</html>