当前位置:   article > 正文

前端小案例——轮播图(HTML+CSS+JS, 附源码)_前端轮播图

前端轮播图

一、前言

实现功能:

  • 显示多张图片,每张图片占据轮播图容器的一部分空间。
  • 实现向左和向右的切换按钮,可以点击按钮切换到上一张或下一张图片。
  • 在底部显示小圆点,表示当前显示的图片,点击小圆点可以跳转到对应的图片。

实现逻辑:

  1. 首先,使用querySelectorAll方法获取所有的轮播项元素,并将其保存在items变量中。

  2. 定义一个全局变量current,用于记录当前显示的轮播项的索引,默认为0。通过操作current变量来控制显示的轮播项。并通过调用相关函数来更新显示和样式

  3. 定义showSlide函数,用于显示当前的轮播项。通过遍历所有轮播项,设置其transform属性来实现水平滑动效果。同时,调用updateDots函数更新小圆点的样式。

  4. 定义prevSlide函数,用于切换到上一张轮播项。如果current大于0,则将current减1;否则,将current设置为最后一张轮播项的索引。然后,调用showSlide函数显示对应的轮播项。

  5. 定义nextSlide函数,用于切换到下一张轮播项。如果current小于轮播项的数量减1,则将current加1;否则,将current设置为0。然后,调用showSlide函数显示对应的轮播项。

  6. 使用setInterval方法定义一个定时器timer,每隔3秒自动调用一次nextSlide函数,实现自动播放功能。

  7. 定义pauseTimer函数,用于暂停定时器。通过调用clearInterval方法,清除定时器timer

  8. 定义resumeTimer函数,用于恢复定时器。重新设置定时器timer,调用nextSlide函数实现轮播功能。

  9. 添加鼠标悬停和离开事件监听器,当鼠标悬停在轮播图上时,调用pauseTimer函数暂停自动播放;当鼠标离开时,调用resumeTimer函数恢复自动播放。

  10. 使用querySelectorAll方法获取所有的小圆点元素,并将其保存在dots变量中。

  11. 定义updateDots函数,用于更新小圆点的样式。遍历所有小圆点元素,移除它们的active类名,然后根据current变量为当前轮播项对应的小圆点添加active类名。

  12. 定义jumpToSlide函数,用于跳转到指定的轮播项。将current变量设置为指定轮播项的索引,然后调用showSlideupdateDots函数,显示对应的轮播项并更新小圆点的样式。

二、项目运行效果 

三、全部代码

  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>轮播图</title>
  7. <style>
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. text-decoration: none;
  12. list-style: none;
  13. background-repeat: no-repeat;
  14. }
  15. .carousel {
  16. position: relative;
  17. width: 100%;
  18. overflow: hidden;
  19. }
  20. .carousel-inner {
  21. display: flex;
  22. width: 100%;
  23. transition: transform 0.6s ease-in-out;
  24. }
  25. .item {
  26. flex: 0 0 100%;
  27. height: 55vh;
  28. }
  29. .item img {
  30. width: 100%;
  31. height: 100%;
  32. object-fit: cover;
  33. }
  34. .carousel-control {
  35. position: absolute;
  36. top: 50%;
  37. transform: translateY(-50%);
  38. color: #fff;
  39. font-size: 80px;
  40. z-index: 10;
  41. cursor: pointer;
  42. }
  43. .left {
  44. left: 25px;
  45. }
  46. .right {
  47. right: 25px;
  48. }
  49. .dots {
  50. position: absolute;
  51. bottom: 20px;
  52. left: 50%;
  53. z-index: 15;
  54. width: 60%;
  55. padding-left: 0;
  56. margin-left: -30%;
  57. text-align: center;
  58. list-style: none;
  59. }
  60. .dots > li {
  61. display: inline-block;
  62. width: 10px;
  63. height: 10px;
  64. margin: 1px;
  65. cursor: pointer;
  66. background-color: rgba(0,0,0,0);
  67. border: 1px solid #fff;
  68. border-radius: 10px;
  69. }
  70. .dots .active {
  71. width: 12px;
  72. height: 12px;
  73. margin: 0;
  74. background-color: #fff;
  75. }
  76. </style>
  77. </head>
  78. <body>
  79. <div class="carousel" id="carousel">
  80. <div class="carousel-inner">
  81. <div class="item">
  82. <img src="1.png" style="background-color: pink;">
  83. </div>
  84. <div class="item">
  85. <img src="2.png" style="background-color: bisque;">
  86. </div>
  87. <div class="item">
  88. <img src="3.jpg" style="background-color: rgb(144, 255, 236);">
  89. </div>
  90. <div class="item">
  91. <img src="4.jpg" style="background-color: rgb(248, 99, 124);">
  92. </div>
  93. <div class="item">
  94. <img src="5.jpg" style="background-color: rgb(210, 161, 250);">
  95. </div>
  96. </div>
  97. <div class="carousel-control left" onclick="prevSlide()">&lsaquo;</div>
  98. <div class="carousel-control right" onclick="nextSlide()">&rsaquo;</div>
  99. <div class="dots">
  100. <li class="active" onclick="jumpToSlide(0)"></li>
  101. <li onclick="jumpToSlide(1)"></li>
  102. <li onclick="jumpToSlide(2)"></li>
  103. <li onclick="jumpToSlide(3)"></li>
  104. <li onclick="jumpToSlide(4)"></li>
  105. </div>
  106. </div>
  107. </body>
  108. <script>
  109. let items = document.querySelectorAll('.item');
  110. let current = 0;
  111. function showSlide() {
  112. items.forEach(item => {
  113. item.style.transform = `translateX(-${current * 100}%)`;
  114. });
  115. updateDots();
  116. }
  117. function prevSlide() {
  118. if (current > 0) {
  119. current--;
  120. } else {
  121. current = items.length - 1;
  122. }
  123. showSlide();
  124. }
  125. function nextSlide() {
  126. if (current < items.length - 1) {
  127. current++;
  128. } else {
  129. current = 0;
  130. }
  131. showSlide();
  132. }
  133. let timer = setInterval(nextSlide, 3000);
  134. function pauseTimer() {
  135. clearInterval(timer);
  136. }
  137. function resumeTimer() {
  138. timer = setInterval(nextSlide, 3000);
  139. }
  140. document.getElementById('carousel').addEventListener('mouseover', pauseTimer);
  141. document.getElementById('carousel').addEventListener('mouseout', resumeTimer);
  142. let dots = document.querySelectorAll('.dots li');
  143. function updateDots() {
  144. dots.forEach(dot => {
  145. dot.classList.remove('active');
  146. });
  147. dots[current].classList.add('active');
  148. }
  149. function jumpToSlide(index) {
  150. current = index;
  151. showSlide();
  152. updateDots();
  153. }
  154. </script>
  155. </html>

四、答疑解惑

        这是一个非常适合前端入门练习的小案例,各位小伙伴可以自行更改样式和图片,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主。

        还多请各位小伙伴多多点赞支持,你们的支持是我最大的动力。

博主VX:18884281851

谢谢各位的支持~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/797228
推荐阅读