博客列表 >JS实现图片轮播

JS实现图片轮播

leverWang
leverWang原创
2020年08月30日 17:32:38758浏览

html

  1. <!-- @format -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>轮播图</title>
  8. <link rel="stylesheet" href="slider.css" />
  9. </head>
  10. <body>
  11. <div class="box">
  12. <img
  13. src="banner/banner1.jpg"
  14. alt=""
  15. data-index="1"
  16. class="slider active"
  17. />
  18. <img src="banner/banner2.jpg" alt="" data-index="2" class="slider" />
  19. <img src="banner/banner3.jpg" alt="" data-index="3" class="slider" />
  20. <img src="banner/banner4.jpg" alt="" data-index="4" class="slider" />
  21. <div class="point-list">
  22. <!-- <span class="point active" data-index="1"></span>
  23. <span class="point" data-index="2"></span>
  24. <span class="point" data-index="3"></span> -->
  25. </div>
  26. <span class="skip prev">&lt;</span>
  27. <span class="skip next">&gt;</span>
  28. </div>
  29. <script src="slider.js"></script>
  30. </body>
  31. </html>

slider.js

  1. /** @format */
  2. //获取所有图片
  3. var imgs = document.querySelectorAll(".box>img");
  4. //获取小圆点父节点
  5. var point_list = document.querySelector(".point-list");
  6. //按图片数量生成对应小圆点
  7. imgs.forEach(function (item, index) {
  8. let span = document.createElement("span");
  9. if (index == 0) {
  10. span.classList.add("active");
  11. }
  12. span.classList.add("point");
  13. span.dataset.index = item.dataset.index;
  14. point_list.append(span);
  15. });
  16. var points = document.querySelectorAll(".point");
  17. //为小圆点添加点击事件
  18. point_list.addEventListener("click", function (event) {
  19. //判断事件节点类型,避免父节点触发事件
  20. if (event.target.nodeName.toLowerCase() == "span") {
  21. imgs.forEach(function (img) {
  22. img.classList.remove("active");
  23. if (img.dataset.index == event.target.dataset.index) {
  24. img.classList.add("active");
  25. setPointActive(img.dataset.index);
  26. }
  27. });
  28. }
  29. });
  30. function setPointActive(index) {
  31. points.forEach(function (point) {
  32. point.classList.remove("active");
  33. if (point.dataset.index == index) {
  34. point.classList.add("active");
  35. }
  36. });
  37. }
  38. //-------------------翻页功能------------------------
  39. //获取翻页按钮
  40. var skip = document.querySelectorAll(".skip");
  41. skip.item(0).addEventListener("click", skipImg);
  42. skip.item(1).addEventListener("click", skipImg);
  43. function skipImg(ev) {
  44. var currentImg = null;
  45. //获取当前显示的图片
  46. imgs.forEach(function (img) {
  47. if (img.classList.contains("active")) {
  48. currentImg = img;
  49. }
  50. });
  51. //判断prev是否被点击
  52. if (ev.target.classList.contains("prev")) {
  53. currentImg.classList.remove("active");
  54. //设置当前图片为前一张图片
  55. currentImg = currentImg.previousElementSibling;
  56. // 判断当前图片是否为空,节点类型是否符合
  57. if (currentImg !== null && currentImg.nodeName.toLowerCase() === "img") {
  58. currentImg.classList.add("active");
  59. } else {
  60. currentImg = imgs[imgs.length - 1];
  61. currentImg.classList.add("active");
  62. }
  63. }
  64. //判断next是否被点击
  65. if (ev.target.classList.contains("next")) {
  66. currentImg.classList.remove("active");
  67. currentImg = currentImg.nextElementSibling;
  68. if (currentImg !== null && currentImg.nodeName.toLowerCase() === "img") {
  69. currentImg.classList.add("active");
  70. } else {
  71. currentImg = imgs[0];
  72. currentImg.classList.add("active");
  73. }
  74. }
  75. setPointActive(currentImg.dataset.index);
  76. }
  77. //----------------------------自动播放实现-----------------------
  78. var timer = setInterval(autoPlay, 5000);
  79. var box = document.querySelector(".box");
  80. box.addEventListener("mouseover", function () {
  81. clearInterval(timer);
  82. });
  83. //鼠标移出后2秒开始播放下一张,并设置一个新的定时器
  84. box.addEventListener("mouseout", function () {
  85. setTimeout(function () {
  86. autoPlay();
  87. timer = setInterval(autoPlay, 5000);
  88. }, 2000);
  89. });
  90. function autoPlay() {
  91. var currentImg = null;
  92. imgs.forEach(function (img) {
  93. if (img.classList.contains("active")) {
  94. currentImg = img;
  95. }
  96. });
  97. currentImg.classList.remove("active");
  98. currentImg = currentImg.nextElementSibling;
  99. if (currentImg != null && currentImg.nodeName.toLowerCase() === "img") {
  100. currentImg.classList.add("active");
  101. } else {
  102. currentImg = imgs[0];
  103. currentImg.classList.add("active");
  104. }
  105. setPointActive(currentImg.dataset.index);
  106. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议