Blogger Information
Blog 15
fans 0
comment 0
visits 9201
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0111 轮播图翻页按钮和鼠标事件
fanyigle_php
Original
672 people have browsed it

以老师的dom为基础写的作业

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>轮播图</title>
  7. <link rel="stylesheet" href="banner/style.css" />
  8. </head>
  9. <body>
  10. <div class="container">
  11. <!-- 1. 图片组 -->
  12. <nav class="imgs">
  13. <a href="#"
  14. ><img src="banner/banner1.jpg" alt="" data-index="1" class="active"
  15. /></a>
  16. <a href="#"><img src="banner/banner2.jpg" alt="" data-index="2" /></a>
  17. <a href="#"><img src="banner/banner3.jpg" alt="" data-index="3" /></a>
  18. <a href="#"><img src="banner/banner4.jpg" alt="" data-index="4" /></a>
  19. </nav>
  20. <!-- 2. 图片小按钮 -->
  21. <nav class="btns">
  22. <!-- 这些小按钮应该根据图片数量自动生成 -->
  23. <!-- <a href="" data-index="1" class="active"></a>
  24. <a href="" data-index="2"></a>
  25. <a href="" data-index="3"></a>
  26. <a href="" data-index="4"></a> -->
  27. </nav>
  28. <!-- 3. 翻页 -->
  29. <nav class="skip">
  30. <a href="#" class="prev">&lt;</a>
  31. <a href="#" class="next">&gt;</a>
  32. </nav>
  33. </div>
  34. <script>
  35. // 所有图片
  36. const imgs = document.querySelectorAll(".container > .imgs img");
  37. // 按钮组
  38. const btnGroup = document.querySelector(".container > .btns");
  39. // 翻页按钮
  40. const skip = document.querySelector(".container > .skip");
  41. // 创建出一组与图片数量对应的小按钮
  42. (function autoCreateBtns(ele, imgLength) {
  43. const frag = document.createDocumentFragment();
  44. for (let i = 0; i < imgLength; i++) {
  45. const a = document.createElement("a");
  46. a.href = "#";
  47. a.dataset.index = i + 1;
  48. if (i === 0) a.classList.add("active");
  49. frag.appendChild(a);
  50. }
  51. ele.appendChild(frag);
  52. })(btnGroup, imgs.length);
  53. // 调用创建小按钮的函数
  54. //autoCreateBtns(btnGroup, imgs.length);
  55. // 为刚刚生成的小按钮们添加点击事件
  56. const btns = document.querySelectorAll(".container > .btns > *");
  57. // 下面声明二个公共函数
  58. // 1. 获取激活的元素
  59. function getActiveEle(eles) {
  60. let activities = [...eles].filter((img) =>
  61. img.classList.contains("active")
  62. );
  63. return activities.shift();
  64. }
  65. // 2. 设置激活的元素,根据按钮索引更新正在显示的图片
  66. function setActiveEle(btnIndex) {
  67. [imgs, btns].forEach((arr) => {
  68. // 将之前的状态全部重置到初始化(清空)
  69. getActiveEle(arr).classList.remove("active");
  70. arr.forEach((item) => {
  71. if (item.dataset.index === btnIndex) {
  72. console.log(item);
  73. item.classList.add("active");
  74. }
  75. });
  76. });
  77. }
  78. // 为每一个小按钮添加事件
  79. // btns.forEach((btn) =>
  80. // btn.addEventListener("click", (ev) =>
  81. // setActiveEle(ev.target.dataset.index)
  82. // )
  83. // );
  84. btnGroup.addEventListener("click", (ev) =>
  85. setActiveEle(ev.target.dataset.index)
  86. );
  87. // btns.forEach(function (btn) {
  88. // btn.onclick = function (ev) {
  89. // setActiveEle(ev.target.dataset.index);
  90. // };
  91. // });
  92. // const btns =
  93. // 作业:
  94. // 1. 为翻页按钮添加功能
  95. skip.addEventListener("click", (ev) => {
  96. let activeIndex = getActiveEle(imgs).dataset.index;
  97. if (ev.target.className === "prev") {
  98. console.log("点击了上一个");
  99. // 获取当前激活的图片的dataindex,如果dataindex=1,重置dataindex=5
  100. // 激活dataindex-1的图片为激活,
  101. if (activeIndex == 1) activeIndex = 5;
  102. activeIndex--;
  103. } else {
  104. // 获取当前激活的图片的dataindex,如果dataindex=4,重置dataindex=1
  105. // 激活dataindex+1的图片为激活,
  106. if (activeIndex == 4) activeIndex = 0;
  107. activeIndex++;
  108. }
  109. setActiveEle(`${activeIndex}`);
  110. });
  111. // 2. 当鼠标移出时,图片的每隔2秒的自动播放,当鼠标移入时自动停止播放
  112. // mouseenter , mouseleave 事件来做
  113. // mouseout带了子元素,弃用
  114. let imgGroup = document.querySelector(".container");
  115. // const ev = new Event("click");
  116. const nextBtn = document.querySelector(".container .next");
  117. let intervalID;
  118. mouseOutHandler();
  119. function mouseOverHandler() {
  120. console.log("鼠标移入");
  121. // 清理自动点击
  122. clearInterval(intervalID);
  123. }
  124. function mouseOutHandler() {
  125. console.log("鼠标移出");
  126. intervalID = setInterval("nextBtn.click()", 2000);
  127. }
  128. imgGroup.addEventListener("mouseleave", mouseOutHandler);
  129. imgGroup.addEventListener("mouseenter", mouseOverHandler);
  130. // 3. 上节课的选项卡, 懒加载二选一
  131. </script>
  132. </body>
  133. </html>
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