<!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>
* {
margin: 0;
padding: 0;
}
#outer {
width: 500px;
margin: auto;
text-align: center;
background-color: pink;
}
img {
width: 100%;
height: 300px;
}
</style>
</head>
<body>
<div id="outer">
<p id="info"></p>
<img src="./index/1.webp" alt="">
<button id="prev">上一张</button>
<button id="next">下一张</button>
</div>
<script>
var imgArr = ['./index/1.webp', './index/2.webp', './index/3.webp', './index/4.webp', './index/5.webp', './index/6.webp']
var prev = document.getElementById('prev')
var next = document.getElementById('next')
var img = document.getElementsByTagName('img')[0]
var index = 0
var info = document.getElementById('info')
info.innerHTML = '一共有 '+(imgArr.length)+' 张图片,这是第 1 张图片'
prev.onclick = function () {
index--
if (index < 0) {
index = imgArr.length - 1
img.alt = '图片'+(index+1)+''
}
img.src = imgArr[index]
var info = document.getElementById('info')
info.innerHTML = '一共有 ' + (imgArr.length) + ' 张图片,这是第 ' + (index+1) + ' 张图片'
}
next.onclick = function () {
index++
if (index > imgArr.length - 1) {
index = 0
img.alt = '图片'+(index+1)+''
}
img.src = imgArr[index]
var info = document.getElementById('info')
info.innerHTML = '一共有 ' + (imgArr.length) + ' 张图片,这是第 ' + (index+1) + ' 张图片'
}
</script>
</body>
</html>