0


JavaScript图片轮播

代码在文章最后面(含图片URL)

实现功能

  1. 按向左按钮图片显示上一张
  2. 按向右按钮图片显示下一张
  3. 每隔2000毫秒显示下一张图
  4. 底部三个圆点显示当前的图片的编号

实现流程

  1. **初始化图片数组 **创建一个包含图片URL的数组,轮播时会通过这个数组来切换图片。
  2. 创建当前索引变量 这个变量用来追踪当前显示的图片索引。
  3. **创建自动轮播图片函数 **该函数负责更改轮播图中显示的图片,并根据当前索引更新指示器的状态。
  4. **创建指示器的函数 **此函数在HTML中创建了与图片数量相同的指示器元素,并为第一个指示器添加了active-indicator类,以表示它是活动的。
  5. 下一张图片按钮 当用户点击“下一张”按钮时,这个函数会被触发。它将currentIndex加一,如果索引等于图片数组长度,则将索引重置为0,然后调用showImage函数来更新图片和指示器。
  6. **上一张图片按钮 **当用户点击“上一张”按钮时,这个函数会被触发。它将currentIndex减一,如果索引小于0,则将索引设置为图片数组的最后一个元素的索引,然后调用showImage函数来更新图片和指示器。
  7. **创建事件监听器 **将这些监听器绑定到“下一张”和“上一张”按钮上,当按钮被点击时,它们会调用相应的函数。
  8. **启动自动轮播 **设置了一个定时器,每隔2000毫秒(2秒)调用一次nextImage函数,从而实现图片的自动轮播。

ONGD:通过操作DOM元素,用CSS类来控制显示的图片和当前图片指示器的状态,实现了图片轮播。

完整代码

  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>Simple Carousel</title>
  7. <style>
  8. body {
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. height: 100vh;
  13. margin: 0;
  14. background-color: #f0f0f0;
  15. }
  16. .carousel {
  17. position: relative;
  18. width: 300px;
  19. height: 200px;
  20. border-radius: 8px;
  21. overflow: hidden;
  22. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  23. }
  24. .carousel img {
  25. width: 100%;
  26. height: auto;
  27. }
  28. .controls {
  29. position: absolute;
  30. top: 50%;
  31. transform: translateY(-50%);
  32. width: 100%;
  33. display: flex;
  34. justify-content: space-between;
  35. padding: 0 10px;
  36. }
  37. .control-button {
  38. background-color: white;
  39. color: #333;
  40. font-size: 20px;
  41. width: 30px;
  42. height: 30px;
  43. border-radius: 50%;
  44. display: flex;
  45. justify-content: center;
  46. align-items: center;
  47. cursor: pointer;
  48. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  49. }
  50. .indicator-container {
  51. position: absolute;
  52. bottom: 10px;
  53. left: 50%;
  54. transform: translateX(-50%);
  55. display: flex;
  56. }
  57. .indicator {
  58. background-color: #ccc;
  59. width: 10px;
  60. height: 10px;
  61. border-radius: 50%;
  62. margin: 0 4px;
  63. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  64. }
  65. .active-indicator {
  66. background-color: black;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <div class="carousel">
  72. <img src="" alt="Image" id="carousel-image">
  73. <div class="controls">
  74. <button class="control-button" id="prev-btn">👈</button>
  75. <button class="control-button" id="next-btn">👉</button>
  76. </div>
  77. <div class="indicator-container" id="indicators"></div>
  78. </div>
  79. <script>
  80. const images = [
  81. 'https://q9.itc.cn/q_70/images03/20240715/8319afaf350346a08a4d51b65638e39d.png',
  82. 'https://n.sinaimg.cn/sinacn20191106ac/42/w500h342/20191106/6b91-ihyxcrp9562392.jpg',
  83. 'https://n.sinaimg.cn/news/transform/20170412/D-Ac-fyecfam0465124.jpg'
  84. ];
  85. let currentIndex = 0;
  86. function showImage(index) {
  87. document.getElementById('carousel-image').src = images[index];
  88. const indicators = document.getElementById('indicators').children;
  89. for (let i = 0; i < indicators.length; i++) {
  90. indicators[i].classList.remove('active-indicator');
  91. }
  92. indicators[index].classList.add('active-indicator');
  93. }
  94. function createIndicators() {
  95. const indicatorContainer = document.getElementById('indicators');
  96. for (let i = 0; i < images.length; i++) {
  97. const indicator = document.createElement('div');
  98. indicator.classList.add('indicator');
  99. if (i === 0) {
  100. indicator.classList.add('active-indicator');
  101. }
  102. indicatorContainer.appendChild(indicator);
  103. }
  104. }
  105. function nextImage() {
  106. currentIndex++;
  107. if (currentIndex >= images.length) {
  108. currentIndex = 0;
  109. }
  110. showImage(currentIndex);
  111. }
  112. function prevImage() {
  113. currentIndex--;
  114. if (currentIndex < 0) {
  115. currentIndex = images.length - 1;
  116. }
  117. showImage(currentIndex);
  118. }
  119. document.getElementById('next-btn').addEventListener('click', nextImage);
  120. document.getElementById('prev-btn').addEventListener('click', prevImage);
  121. // Auto-play functionality
  122. setInterval(nextImage, 2000);
  123. createIndicators();
  124. showImage(currentIndex);
  125. </script>
  126. </body>
  127. </html>
标签: html javascript css

本文转载自: https://blog.csdn.net/w11111xxxl/article/details/140742524
版权归原作者 睿智的海鸥 所有, 如有侵权,请联系我们删除。

“JavaScript图片轮播”的评论:

还没有评论