1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. <style>
    8. * {
    9. margin: 0;
    10. padding: 0;
    11. }
    12. #outer {
    13. width: 500px;
    14. margin: auto;
    15. text-align: center;
    16. background-color: pink;
    17. }
    18. img {
    19. width: 100%;
    20. height: 300px;
    21. }
    22. </style>
    23. </head>
    24. <body>
    25. <div id="outer">
    26. <p id="info"></p>
    27. <img src="./index/1.webp" alt="">
    28. <button id="prev">上一张</button>
    29. <button id="next">下一张</button>
    30. </div>
    31. <script>
    32. var imgArr = ['./index/1.webp', './index/2.webp', './index/3.webp', './index/4.webp', './index/5.webp', './index/6.webp']
    33. var prev = document.getElementById('prev')
    34. var next = document.getElementById('next')
    35. var img = document.getElementsByTagName('img')[0]
    36. var index = 0
    37. var info = document.getElementById('info')
    38. info.innerHTML = '一共有 '+(imgArr.length)+' 张图片,这是第 1 张图片'
    39. prev.onclick = function () {
    40. index--
    41. if (index < 0) {
    42. index = imgArr.length - 1
    43. img.alt = '图片'+(index+1)+''
    44. }
    45. img.src = imgArr[index]
    46. var info = document.getElementById('info')
    47. info.innerHTML = '一共有 ' + (imgArr.length) + ' 张图片,这是第 ' + (index+1) + ' 张图片'
    48. }
    49. next.onclick = function () {
    50. index++
    51. if (index > imgArr.length - 1) {
    52. index = 0
    53. img.alt = '图片'+(index+1)+''
    54. }
    55. img.src = imgArr[index]
    56. var info = document.getElementById('info')
    57. info.innerHTML = '一共有 ' + (imgArr.length) + ' 张图片,这是第 ' + (index+1) + ' 张图片'
    58. }
    59. </script>
    60. </body>
    61. </html>