Blogger Information
Blog 41
fans 2
comment 0
visits 29311
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js之轮播图
月光下,遗忘黑暗
Original
677 people have browsed it

1.代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>轮播图</title>
  6. <link rel="stylesheet" href="0409/banner/banner.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <!-- 图片组-->
  11. <navs class="imgs">
  12. <a href="#"><img src="0409/banner/banner1.jpg" data-index="1" alt="" class="active"></a>
  13. <a href="#"><img src="0409/banner/banner2.jpg" data-index="2" alt=""></a>
  14. <a href="#"><img src="0409/banner/banner3.jpg" data-index="3" alt=""></a>
  15. <a href="#"><img src="0409/banner/banner4.jpg" data-index="4" alt=""></a>
  16. </navs>
  17. <!-- 图片下面的按钮-->
  18. <nav class="btns">
  19. <!-- 小按钮应该动态生成-->
  20. <!-- <a href="" data-index="1"></a>-->
  21. <!-- <a href="" data-index="2"></a>-->
  22. <!-- <a href="" data-index="3"></a>-->
  23. <!-- <a href="" data-index="4"></a>-->
  24. </nav>
  25. <!-- 翻页-->
  26. <nav class="skip">
  27. <a class="prev">&lt;</a>
  28. <a class="next">&gt;</a>
  29. </nav>
  30. </div>
  31. <script>
  32. // 所有图片
  33. const imgs = document.querySelectorAll(".container >.imgs img");
  34. // 按钮组
  35. const btnGroup = document.querySelector(".container > .btns");
  36. // 翻页按钮
  37. const skip = document.querySelectorAll(".container > .skip a");
  38. //const skip = document.querySelectorAll(".container > .skip");
  39. // 创建出一组与图片数量对应的小按钮
  40. function autoCrateBtns(ele, imgLength) {
  41. // 复用文档片断来简化/优化编程
  42. const frag = document.createDocumentFragment();
  43. for (let i = 0; i < imgLength; i++) {
  44. const a = document.createElement("a");
  45. a.href = "#";
  46. a.dataset.index = i + 1;
  47. if (i === 0) a.classList.add("active");
  48. frag.append(a);
  49. }
  50. ele.append(frag);
  51. }
  52. autoCrateBtns(btnGroup, imgs.length);
  53. // 为刚刚生成的小按钮添加点击事件
  54. const btns = document.querySelectorAll(".container > .btns > *");
  55. // 二个公共函数
  56. // 1. 获取激活的元素
  57. function getActiveEle(eles) {
  58. // 将元素集合转为真正的数组
  59. // console.log([...eles]);
  60. let activeEles = [...eles].filter((ele) => ele.classList.contains("active"));
  61. // console.log(activeEles.shift());
  62. return activeEles.shift();
  63. }
  64. // getActiveEle(btns);
  65. // getActiveEle(imgs);
  66. // 2. 设置激活的元素,根据按钮索引更新正在显示的图片
  67. // 参数就是当前正在点击的按钮的索引
  68. function setActiveEle(btnIndex) {
  69. // console.log(btnIndex);
  70. // console.log(imgs, btns);
  71. // 1. 先将之前的激活的样式去掉
  72. [imgs, btns].forEach((arr) => {
  73. getActiveEle(arr).classList.remove("active");
  74. // 2. 再根据当前的的自定义索引来重新设置应该激活的按钮和图片
  75. arr.forEach((item) => {
  76. if (item.dataset.index == btnIndex) {
  77. item.classList.add("active");
  78. }
  79. });
  80. });
  81. }
  82. // 为按钮添加事件
  83. btns.forEach((btn) => btn.addEventListener("click", (ev) => setActiveEle(ev.target.dataset.index)));
  84. // 鼠标移入时关闭自动播放,移出时启动自动播放
  85. // setInterval(),间歇式定时器(重复执行)
  86. // dispathEvent(): 事件派发器
  87. // 左右按钮绑定的事件
  88. function addClick(btn) {
  89. //获取向右点击按钮
  90. if(btn.target.classList.contains('next')) {
  91. let index = +getActiveEle(imgs).dataset.index;
  92. // 判断是否在最后一页
  93. index = (index == imgs.length) ? 1 : index + 1;
  94. setActiveEle(index);
  95. }
  96. if(btn.target.classList.contains('prev')) {
  97. let index = +getActiveEle(imgs).dataset.index;
  98. index = (index == 1) ? imgs.length : index-1;
  99. setActiveEle(index);
  100. }
  101. }
  102. //为左右点击按钮绑定事件
  103. skip.forEach(btn => btn.addEventListener('click',(ev)=>addClick(ev)));
  104. //轮播图自动切换
  105. let change;
  106. function autoPlay(changeTime) {
  107. const ev = new Event("click");
  108. change = setInterval(() => {skip[1].dispatchEvent(ev)},changeTime);
  109. }
  110. window.onload = () => autoPlay(1500);
  111. //鼠标移入开始自动切换
  112. document.querySelector('.container').onmouseenter = () => clearInterval(change);
  113. document.querySelector('.container').onmouseleave = () => autoPlay(1500);
  114. </script>
  115. </body>
  116. </html>

2.效果

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:如果自己独立完成,可以写到哪一步, 可以试试看
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post