JS 原生手写轮播图

不念不忘少年蓝@ 2023-10-11 22:08 97阅读 0赞

只是实现了原生JS实现轮播图的逻辑和具体实现代码,css样式并未进行美化

实现了以下功能

(1) 点击容器container下方小圆点可以进行切换

(2) 点击左右的按钮可以进行切换

(3) 自动轮播图片

(4) 鼠标放上去暂停定时器,鼠标移出重新开启定时器

具体代码实现如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. body {
  15. margin: 300px;
  16. }
  17. .container {
  18. width: 600px;
  19. height: 300px;
  20. position: relative;
  21. }
  22. .content-list {
  23. width: 600px;
  24. height: 300px;
  25. position: relative;
  26. transition: all 0.9s;
  27. }
  28. .item {
  29. width: 100%;
  30. height: 100%;
  31. list-style: none;
  32. position: absolute;
  33. left: 0;
  34. top: 0;
  35. opacity: 0;
  36. }
  37. .item:nth-child(1) {
  38. background-color: red;
  39. }
  40. .item:nth-child(2) {
  41. background-color: green;
  42. }
  43. .item:nth-child(3) {
  44. background-color: blue;
  45. }
  46. .item:nth-child(4) {
  47. background-color: black;
  48. }
  49. .active {
  50. opacity: 1;
  51. }
  52. .order {
  53. position: absolute;
  54. left: 50%;
  55. transform: translateX(-50%);
  56. bottom: 10px;
  57. display: flex;
  58. }
  59. .item1 {
  60. width: 20px;
  61. height: 20px;
  62. margin-right: 10px;
  63. border-radius: 50%;
  64. background-color: white;
  65. list-style: none;
  66. }
  67. .item1Active {
  68. background: purple;
  69. }
  70. button {
  71. width: 20px;
  72. height: 30px;
  73. }
  74. .showBtn {
  75. display: block;
  76. }
  77. .hideBtn {
  78. display: none;
  79. }
  80. #leftBtn {
  81. position: absolute;
  82. left: 10px;
  83. top: 50%;
  84. transform: translateY(-50%);
  85. }
  86. #rightBtn {
  87. position: absolute;
  88. right: 10px;
  89. top: 50%;
  90. transform: translateY(-50%);
  91. }
  92. </style>
  93. </head>
  94. <body>
  95. <div class="container">
  96. <ul class="content-list">
  97. <li class="item active"></li>
  98. <li class="item"></li>
  99. <li class="item"></li>
  100. <li class="item"></li>
  101. </ul>
  102. <ul class="order">
  103. <li class="item1 item1Active">1</li>
  104. <li class="item1">2</li>
  105. <li class="item1">3</li>
  106. <li class="item1">4</li>
  107. </ul>
  108. <button id="leftBtn" class="hideBtn"><</button>
  109. <button id="rightBtn" class="hideBtn">></button>
  110. </div>
  111. <script>
  112. let container = document.querySelector('.container');
  113. let items = document.querySelectorAll('.item');
  114. let item1s = document.querySelectorAll('.item1');
  115. let leftBtn = document.querySelector('#leftBtn');
  116. let rightBtn = document.querySelector('#rightBtn');
  117. // 当前索引index
  118. let index = 0;
  119. function changeIndex() {
  120. for (let i = 0; i < items.length; i++) {
  121. items[i].classList.remove('active');
  122. item1s[i].classList.remove('item1Active');
  123. }
  124. items[index].classList.add('active');
  125. item1s[index].classList.add('item1Active');
  126. }
  127. function goLeft() {
  128. if (index > 0) {
  129. index--;
  130. } else {
  131. index = 3;
  132. }
  133. changeIndex();
  134. }
  135. function goRight() {
  136. if (index < 3) {
  137. index++;
  138. } else {
  139. index = 0;
  140. }
  141. changeIndex();
  142. }
  143. // 给左按钮和右按钮绑定点击事件,达到轮播图切换的效果
  144. leftBtn.addEventListener('click', goLeft);
  145. rightBtn.addEventListener('click', goRight);
  146. // 给底部小圆点增加自定义属性index,标记为索引,方便为小圆点绑定点击事件
  147. for (let i = 0; i < item1s.length; i++) {
  148. item1s[i].addEventListener('click', function () {
  149. index = i;
  150. changeIndex();
  151. });
  152. }
  153. // 声明定时器,达到轮播图自动向后切换的效果
  154. let timer;
  155. timer = setInterval(() => {
  156. goRight();
  157. }, 2000);
  158. // 鼠标移到当前容器触发的事件,两个按钮显示,关闭定时器
  159. container.addEventListener('mouseover', function () {
  160. leftBtn.className = 'showBtn';
  161. rightBtn.className = 'showBtn';
  162. // 暂停定时器
  163. clearInterval(timer);
  164. });
  165. // 鼠标离开当前容器触发的事件,两个按钮隐藏,重新开启定时器
  166. container.addEventListener('mouseleave', function () {
  167. leftBtn.className = 'hideBtn';
  168. rightBtn.className = 'hideBtn';
  169. // 再次打开定时器
  170. timer = setInterval(() => {
  171. goRight();
  172. }, 2000);
  173. });
  174. </script>
  175. </body>
  176. </html>

达到的效果图如下

ab98735937c84614aaef7fd9dbcf7360.gif

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读

    相关 原生js层叠

    前言: 已经一个月没有怎么更博了,一直在学习一些其他得。之前在网上看到一个层叠样式得轮播图,也是看过一个博主得内容以后自己又理清楚了思路才写出来。 原文出处:https:

    相关 原生js实现

    轮播图基本上是前端所必须面临的一个功能。而且在网上可以找到各种各样的插件或者写法。 但是我个人觉得还是写一下比较好。这里用到的是原生JS、CSS3相结合的写法。 对IE 8

    相关 原生js实现

    原生js实现轮播图  很多网站上都有轮播图,但却很难找到一个系统讲解的,因此这里做一个简单的介绍,希望大家都能有所收获,如果有哪些不正确的地方,希望大家可以指出。  [