博客列表 >JS制作轮播图

JS制作轮播图

longlong
longlong原创
2020年08月18日 11:19:57966浏览

1. 轮播图

1.1 轮播图片如下:

1.2 代码如下:

  • html部分:
  1. <!-- 1. 先写好html,样式设置好 -->
  2. <div class="box">
  3. <img src="./banner/banner1.jpg" alt="图片无法正常加载..." class="slider active" data-index="1" />
  4. <img src="./banner/banner2.jpg" alt="图片无法正常加载..." class="slider" data-index="2" />
  5. <img src="./banner/banner3.jpg" alt="图片无法正常加载..." class="slider" data-index="3" />
  6. <img src="./banner/banner4.jpg" alt="图片无法正常加载..." class="slider" data-index="4" />
  7. <div class="pointAll">
  8. <!-- 2. 开始写JS的时候将这里注释,因为这些小圆点是后面通过JS动态生成的 -->
  9. <!-- <span class="point active"></span>
  10. <span class="point"></span>
  11. <span class="point"></span>
  12. <span class="point"></span> -->
  13. </div>
  14. <span class="skip prev">&lt</span>
  15. <span class="skip next">&gt</span>
  16. </div>
  • CSS部分:
  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. .box {
  8. position: relative;
  9. width: 1000px;
  10. margin: 20px auto;
  11. }
  12. .box .slider {
  13. width: 1000px;
  14. height: 350px;
  15. display: none;
  16. }
  17. .box .slider.active {
  18. display: block;
  19. }
  20. .pointAll .point {
  21. display: inline-block;
  22. width: 10px;
  23. height: 10px;
  24. border-radius: 100%;
  25. margin: 0 5px;
  26. background-color: white;
  27. }
  28. .pointAll .point:hover {
  29. cursor: pointer;
  30. }
  31. .pointAll .point.active {
  32. background-color: black;
  33. }
  34. .pointAll {
  35. position: absolute;
  36. top: 320px;
  37. left: 50%;
  38. }
  39. .box .skip {
  40. position: absolute;
  41. top: 140px;
  42. display: inline-block;
  43. width: 40px;
  44. height: 80px;
  45. text-align: center;
  46. line-height: 80px;
  47. background-color: lightgray;
  48. color: white;
  49. opacity: 0.2;
  50. font-size: 36px;
  51. }
  52. .box .skip:hover {
  53. opacity: 0.5;
  54. cursor: pointer;
  55. color: black;
  56. }
  57. .box .skip.prev {
  58. left: 0;
  59. border-radius: 0 20% 20% 0;
  60. }
  61. .box .skip.next {
  62. right: 0;
  63. border-radius: 20% 0 0 20%;
  64. }
  65. </style>
  • JS部分:
  1. <script>
  2. // 3. 获取所有的图片
  3. let imgs = document.querySelectorAll('.box .slider');
  4. // console.log(imgs);
  5. let pointAll = document.querySelector('.pointAll');
  6. // 4. 根据图片的数量动态生成所有的小圆点,并将第一张图片设置为默认
  7. imgs.forEach(function (img, index) {
  8. let span = document.createElement('span');
  9. if (index === 0) {
  10. span.classList.add('point', 'active');
  11. }
  12. span.classList.add('point');
  13. // 一定要将图片的自定义索引赋给小圆点
  14. span.dataset.index = img.dataset.index;
  15. pointAll.appendChild(span);
  16. });
  17. // 5. 获取所有的小圆点,对其父级绑定事件(事件代理)
  18. let points = document.querySelectorAll('.point');
  19. // console.log(points);
  20. pointAll.addEventListener('click', function (ev) {
  21. // console.log(ev.target);
  22. // 通过事件触发者的data-index去找到对应索引的图片,并显示出来
  23. imgs.forEach(function (img) {
  24. if (ev.target.dataset.index === img.dataset.index) {
  25. imgs.forEach(function (img) {
  26. img.classList.remove('active');
  27. });
  28. img.classList.add('active');
  29. // 找到对应的图片以后也要将对应的小圆点的设置高亮
  30. // 将小圆点高亮写成一个公共函数,因为后面点击箭头的时候还会用到
  31. setPointActive(ev.target.dataset.index);
  32. }
  33. });
  34. }, false);
  35. // 6. 小圆点高亮公共方法
  36. function setPointActive(index) {
  37. // 先清空所有小圆点高亮样式
  38. points.forEach(function (point) {
  39. point.classList.remove('active');
  40. });
  41. // 根据传过来的当前索引找到对应的小圆点,然后高亮显示
  42. points.forEach(function (point) {
  43. if (point.dataset.index === index) {
  44. point.classList.add('active');
  45. }
  46. });
  47. }
  48. // 7. 获取前一张和下一张箭头
  49. let skips = document.querySelectorAll('.skip');
  50. // console.log(skips);
  51. // 8. 对两个箭头绑定事件
  52. skips[0].addEventListener('click', skipImg, false);
  53. skips[1].addEventListener('click', skipImg, false);
  54. function skipImg(ev) {
  55. // 先拿到当前的图片
  56. let nowImg = null;
  57. imgs.forEach(function (img) {
  58. if (img.classList.contains('active')) {
  59. nowImg = img;
  60. }
  61. });
  62. // console.log(nowImg);
  63. // 9. 判断点击的箭头是哪一个
  64. // 如果是前一张
  65. if (ev.target.classList.contains('prev')) {
  66. nowImg.classList.remove('active');
  67. nowImg = nowImg.previousElementSibling;
  68. // 判断前一张图片是否存在
  69. if (nowImg !== null && nowImg.nodeName === "IMG") {
  70. nowImg.classList.add('active');
  71. } else {
  72. nowImg = imgs[imgs.length - 1];
  73. nowImg.classList.add('active');
  74. }
  75. }
  76. // 如果是下一张
  77. if (ev.target.classList.contains('next')) {
  78. nowImg.classList.remove('active');
  79. nowImg = nowImg.nextElementSibling;
  80. // 判断后一张图片是否存在
  81. if (nowImg !== null && nowImg.nodeName === "IMG") {
  82. nowImg.classList.add('active');
  83. } else {
  84. nowImg = imgs[0];
  85. nowImg.classList.add('active');
  86. }
  87. }
  88. // 小圆点跟随当前图片的自定义索引高亮
  89. setPointActive(nowImg.dataset.index);
  90. }
  91. // 10. 设置自动播放
  92. // 定义一个周期性计时器
  93. let timer = setInterval(show, 2000);
  94. function show() {
  95. // 先找到当前图片
  96. let nowImg = null;
  97. imgs.forEach(function (img) {
  98. if (img.classList.contains('active')) {
  99. nowImg = img;
  100. }
  101. });
  102. nowImg.classList.remove('active');
  103. nowImg = nowImg.nextElementSibling;
  104. // 判断下一张图片是否存在
  105. if (nowImg !== null && nowImg.nodeName === "IMG") {
  106. nowImg.classList.add('active');
  107. } else {
  108. nowImg = imgs[0];
  109. nowImg.classList.add('active');
  110. }
  111. setPointActive(nowImg.dataset.index);
  112. }
  113. // 获取到轮播图
  114. let box = document.querySelector('.box');
  115. // 鼠标移入时关闭计时器
  116. box.addEventListener('mouseover', function () {
  117. clearInterval(timer);
  118. }, false);
  119. // 鼠标移出时重新开启
  120. box.addEventListener('mouseout', function () {
  121. timer = setInterval(show, 2000);
  122. }, false);
  123. </script>

1.3 效果如下:

  • 点击小圆点时:

  • 点击箭头时:

  • 自动播放:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议