0


JS实现轮播图(一看就懂逻辑清晰)

轮播图有很多种实现方法,这是其中一种最清晰的方法。思路很清晰,代码很简单,欢迎大佬多指教。

先来看下效果图,嫌麻烦就不用具体图片来实现了,主要是理清思路。(自动轮播,左右按钮切换图片,小圆点切换图片,鼠标移入暂停轮播,鼠标移出继续轮播)


HTML

首先是html内容,布局很简单,一个图片列表,一个点列表,两个按钮。注意data-index使用HTML5中的data-xx属性来嵌入自定义数据(下面JS代码会提到)。记得给默认显示的图片和对应的小圆点加上active类哦。

  1. <div class="wrap">
  2. <ul class="list">
  3. <li class="item active">0</li>
  4. <li class="item">1</li>
  5. <li class="item">2</li>
  6. <li class="item">3</li>
  7. <li class="item">4</li>
  8. </ul>
  9. <ul class="pointList">
  10. <li class="point active" data-index = 0></li>
  11. <li class="point" data-index = 1></li>
  12. <li class="point" data-index = 2></li>
  13. <li class="point" data-index = 3></li>
  14. <li class="point" data-index = 4></li>
  15. </ul>
  16. <button class="btn" id="leftBtn"> < </button>
  17. <button class="btn" id="rightBtn"> > </button>
  18. </div>

CSS

思路:父容器list相对定位,item绝对定位,实现让所有图片重叠在父容器内。利用z-index来设置图片高度,图片高度最高会显示在顶层上。那么整个容器中,左右切换按钮和小圆点始终要在最顶层,不能被图片覆盖,所以他们的z-index一定要比图片高。active是一个样式,给某张图片绑定这个样式就能在最上层显示。然后就是图片切换的渐变效果,opacity完全不透明到透明,再加个transition动画效果。最后就是cursor给小圆点添加“小手指”,其他就没什么好说的了。

  1. <style>
  2. .wrap {
  3. width: 800px;
  4. height: 400px;
  5. position: relative;
  6. }
  7. .list {
  8. width: 800px;
  9. height: 400px;
  10. position: relative;
  11. padding-left: 0px;
  12. }
  13. .item {
  14. width: 100%;
  15. height: 100%;
  16. list-style: none;
  17. position: absolute;
  18. left: 0;
  19. opacity: 0;
  20. transition: all .8s;
  21. }
  22. .item:nth-child(1) {
  23. background-color: skyblue;
  24. }
  25. .item:nth-child(2) {
  26. background-color: yellowgreen;
  27. }
  28. .item:nth-child(3) {
  29. background-color: rebeccapurple;
  30. }
  31. .item:nth-child(4) {
  32. background-color: pink;
  33. }
  34. .item:nth-child(5) {
  35. background-color: orange;
  36. }
  37. .item.active {
  38. z-index: 10;
  39. opacity: 1;
  40. }
  41. .btn {
  42. width: 60px;
  43. height: 100px;
  44. z-index: 100;
  45. top: 150px;
  46. position: absolute;
  47. }
  48. #leftBtn {
  49. left: 0px;
  50. }
  51. #rightBtn {
  52. right: 0px;
  53. }
  54. .pointList {
  55. list-style: none;
  56. padding-left: 0px;
  57. position: absolute;
  58. right: 20px;
  59. bottom: 20px;
  60. z-index: 200;
  61. }
  62. .point {
  63. width: 10px;
  64. height: 10px;
  65. background-color: antiquewhite;
  66. border-radius: 100%;
  67. float: left;
  68. margin-right: 8px;
  69. border-style: solid;
  70. border-width: 2px;
  71. border-color: slategray;
  72. cursor: pointer;
  73. }
  74. .point.active{
  75. background-color: cadetblue;
  76. }
  77. </style>

Javascript

Index可以说是整个代码里面的"核心",先封装一个清除active的方法,这里面要清除图片的active(显示在最上层),比如切换到下一张图上张图的active就要清除。还有point的active(图片对应小圆点改变样式)。然后goIndex这个方法就是给图片和对应的小圆点同时加上active,左右按钮绑定的方法就不说了。

用getAttribute拿到刚才给li标签绑定的data-index属性,绑定图片index = pointindex,就能实现点击小圆点图片切换了。由于上面goIndex方法早已经绑定好了给图片添加active样式的时候也给小圆点添加的样式了,就可以实现图片切换小圆点跟随变化的效果。

  1. <script>
  2. var items = document.querySelectorAll(".item");//图片节点
  3. var points = document.querySelectorAll(".point")//点
  4. var left = document.getElementById("leftBtn");
  5. var right = document.getElementById("rightBtn");
  6. var all = document.querySelector(".wrap")
  7. var index = 0;
  8. var time = 0;//定时器跳转参数初始化
  9. //封装一个清除active方法
  10. var clearActive = function () {
  11. for (i = 0; i < items.length; i++) {
  12. items[i].className = 'item';
  13. }
  14. for (j = 0; j < points.length; j++) {
  15. points[j].className = 'point';
  16. }
  17. }
  18. //改变active方法
  19. var goIndex = function () {
  20. clearActive();
  21. items[index].className = 'item active';
  22. points[index].className = 'point active'
  23. }
  24. //左按钮事件
  25. var goLeft = function () {
  26. if (index == 0) {
  27. index = 4;
  28. } else {
  29. index--;
  30. }
  31. goIndex();
  32. }
  33. //右按钮事件
  34. var goRight = function () {
  35. if (index < 4) {
  36. index++;
  37. } else {
  38. index = 0;
  39. }
  40. goIndex();
  41. }
  42. //绑定点击事件监听
  43. left.addEventListener('click', function () {
  44. goLeft();
  45. time = 0;//计时器跳转清零
  46. })
  47. right.addEventListener('click', function () {
  48. goRight();
  49. time = 0;//计时器跳转清零
  50. })
  51. for(i = 0;i < points.length;i++){
  52. points[i].addEventListener('click',function(){
  53. var pointIndex = this.getAttribute('data-index')
  54. index = pointIndex;
  55. goIndex();
  56. time = 0;//计时器跳转清零
  57. })
  58. }
  59. //计时器轮播效果
  60. var timer;
  61. function play(){
  62. timer = setInterval(() => {
  63. time ++;
  64. if(time == 20 ){
  65. goRight();
  66. time = 0;
  67. }
  68. },100)
  69. }
  70. play();
  71. //移入清除计时器
  72. all.onmousemove = function(){
  73. clearInterval(timer)
  74. }
  75. //移出启动计时器
  76. all.onmouseleave = function(){
  77. play();
  78. }
  79. </script>

总结:这个简单的轮播图实现例子是第一次写最容易理解,逻辑最清晰的一种。下面我把完整的代码块放出来,直接复制粘贴就可以运行。


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .wrap {
  6. width: 800px;
  7. height: 400px;
  8. position: relative;
  9. }
  10. .list {
  11. width: 800px;
  12. height: 400px;
  13. position: relative;
  14. padding-left: 0px;
  15. }
  16. .item {
  17. width: 100%;
  18. height: 100%;
  19. list-style: none;
  20. position: absolute;
  21. left: 0;
  22. opacity: 0;
  23. transition: all .8s;
  24. }
  25. .item:nth-child(1) {
  26. background-color: skyblue;
  27. }
  28. .item:nth-child(2) {
  29. background-color: yellowgreen;
  30. }
  31. .item:nth-child(3) {
  32. background-color: rebeccapurple;
  33. }
  34. .item:nth-child(4) {
  35. background-color: pink;
  36. }
  37. .item:nth-child(5) {
  38. background-color: orange;
  39. }
  40. .item.active {
  41. z-index: 10;
  42. opacity: 1;
  43. }
  44. .btn {
  45. width: 60px;
  46. height: 100px;
  47. z-index: 100;
  48. top: 150px;
  49. position: absolute;
  50. }
  51. #leftBtn {
  52. left: 0px;
  53. }
  54. #rightBtn {
  55. right: 0px;
  56. }
  57. .pointList {
  58. list-style: none;
  59. padding-left: 0px;
  60. position: absolute;
  61. right: 20px;
  62. bottom: 20px;
  63. z-index: 200;
  64. }
  65. .point {
  66. width: 10px;
  67. height: 10px;
  68. background-color: antiquewhite;
  69. border-radius: 100%;
  70. float: left;
  71. margin-right: 8px;
  72. border-style: solid;
  73. border-width: 2px;
  74. border-color: slategray;
  75. cursor: pointer;
  76. }
  77. .point.active{
  78. background-color: cadetblue;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div class="wrap">
  84. <ul class="list">
  85. <li class="item active">0</li>
  86. <li class="item">1</li>
  87. <li class="item">2</li>
  88. <li class="item">3</li>
  89. <li class="item">4</li>
  90. </ul>
  91. <ul class="pointList">
  92. <li class="point active" data-index = 0></li>
  93. <li class="point" data-index = 1></li>
  94. <li class="point" data-index = 2></li>
  95. <li class="point" data-index = 3></li>
  96. <li class="point" data-index = 4></li>
  97. </ul>
  98. <button class="btn" id="leftBtn"> < </button>
  99. <button class="btn" id="rightBtn"> > </button>
  100. </div>
  101. <script>
  102. var items = document.querySelectorAll(".item");//图片
  103. var points = document.querySelectorAll(".point")//点
  104. var left = document.getElementById("leftBtn");
  105. var right = document.getElementById("rightBtn");
  106. var all = document.querySelector(".wrap")
  107. var index = 0;
  108. var time = 0;//定时器跳转参数初始化
  109. //清除active方法
  110. var clearActive = function () {
  111. for (i = 0; i < items.length; i++) {
  112. items[i].className = 'item';
  113. }
  114. for (j = 0; j < points.length; j++) {
  115. points[j].className = 'point';
  116. }
  117. }
  118. //改变active方法
  119. var goIndex = function () {
  120. clearActive();
  121. items[index].className = 'item active';
  122. points[index].className = 'point active'
  123. }
  124. //左按钮事件
  125. var goLeft = function () {
  126. if (index == 0) {
  127. index = 4;
  128. } else {
  129. index--;
  130. }
  131. goIndex();
  132. }
  133. //右按钮事件
  134. var goRight = function () {
  135. if (index < 4) {
  136. index++;
  137. } else {
  138. index = 0;
  139. }
  140. goIndex();
  141. }
  142. //绑定点击事件监听
  143. left.addEventListener('click', function () {
  144. goLeft();
  145. time = 0;//计时器跳转清零
  146. })
  147. right.addEventListener('click', function () {
  148. goRight();
  149. time = 0;//计时器跳转清零
  150. })
  151. for(i = 0;i < points.length;i++){
  152. points[i].addEventListener('click',function(){
  153. var pointIndex = this.getAttribute('data-index')
  154. index = pointIndex;
  155. goIndex();
  156. time = 0;//计时器跳转清零
  157. })
  158. }
  159. //计时器
  160. var timer;
  161. function play(){
  162. timer = setInterval(() => {
  163. time ++;
  164. if(time == 20 ){
  165. goRight();
  166. time = 0;
  167. }
  168. },100)
  169. }
  170. play();
  171. //移入清除计时器
  172. all.onmousemove = function(){
  173. clearInterval(timer)
  174. }
  175. //移出启动计时器
  176. all.onmouseleave = function(){
  177. play();
  178. }
  179. </script>
  180. </body>
  181. </html>

创作不易,觉得有帮助的话请留下一个赞谢谢~

标签: javascript 前端 css3

本文转载自: https://blog.csdn.net/qq_20495901/article/details/122936580
版权归原作者 Beannnnnnn 所有, 如有侵权,请联系我们删除。

“JS实现轮播图(一看就懂逻辑清晰)”的评论:

还没有评论