<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
*{margin: 0; padding: 0;}
#outer{ width: 500px; text-align: center;
padding: 20px;margin: 50px auto;background-color: yellow;}
</style>
<title></title>
</head>
<body>
<div id="outer">
<img src="img/2.jpg"/ width="500px">
<button id="prev">前一张</button>
<button id="next">后一张</button>
</div>
</body>
<script type="text/javascript">
var prev=document.getElementById("prev")
var next=document.getElementById("next")
var img=document.getElementsByTagName("img")[0]
var imgarr=["img/1.jpg","img/2.jpg","img/3.jpg","img/5.jpg","img/6.jpg"]
var index=0
prev.onclick=function(){
index--
if(index<0){
index=imgarr.length-1
}
img.src=imgarr[index]
}
next.onclick=function(){
index++
if(index>imgarr.length-1){
index=0
}
img.src=imgarr[index]
}
</script>
</html>
原生js代码实现图片切换功能
最新推荐文章于 2023-04-03 21:30:47 发布