Blogger Information
Blog 43
fans 0
comment 0
visits 30465
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图实现
橙絮圆
Original
817 people have browsed it

轮播图

作业标题:0715作业
作业内容:模仿课堂源码, 自己动手试着完成轮播图


  • 轮播图

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>轮播图</title>
    8. <link rel="stylesheet" href="style.css" />
    9. </head>
    10. <body>
    11. <div class="container">
    12. <!--1. 图片组 -->
    13. <div class="img-group"></div>
    14. <img src="" alt="" />
    15. <!-- 2.图片下部的小按钮 -->
    16. <div class="btn-group"></div>
    17. <!-- 3.翻页按钮 -->
    18. <div class="skip">
    19. <a class="prev" onclick="prevImg()">&lt;</a>
    20. <a class="next" onclick="nextImg()">&gt;</a>
    21. </div>
    22. </div>
    23. <script>
    24. const banners = [
    25. "banner/images/banner1.jpg",
    26. "banner/images/banner2.jpg",
    27. "banner/images/banner3.jpg",
    28. "banner/images/banner4.jpg",
    29. ];
    30. //图片组
    31. const imgGroup = document.querySelector(".container > .img-group");
    32. //按钮组
    33. const btnGroup = document.querySelector(".container > .btn-group");
    34. //load:窗口加载完成自动执行:自动将所有轮播图加载出来,并自动生成与轮播图数量相同的小按钮
    35. window.onload = () => {
    36. //1.生成轮播图所有图片
    37. createImgs(imgGroup, banners.length);
    38. //2.生成与轮播图数量对应的小按钮
    39. createBtns(btnGroup, banners.length);
    40. };
    41. //生成图片
    42. function createImgs(parent, length) {
    43. //正确的做法,应该是将所有图片,先在内存中创建,然后统一的插入到页面中,这样就只需要渲染一次
    44. //文档片断元素
    45. const frag = document.createDocumentFragment();
    46. for (let i = 0; i < length; i++) {
    47. const img = document.createElement("img");
    48. img.src = banners[i];
    49. img.alt = `banner${i + 1}`;
    50. //为每一张图片添加一个自定义属性"data-index",用它与小按钮进行绑定
    51. img.dataset.index = `${i + 1}`;
    52. if (i === 0) img.classList.add("active");
    53. //内容中执行了四次
    54. frag.append(img);
    55. }
    56. //页面中只渲染了一次,效率大幅提高
    57. parent.append(frag);
    58. }
    59. //生成按钮
    60. function createBtns(parent, length) {
    61. //文档片断元素
    62. const frag = document.createDocumentFragment();
    63. for (let i = 0; i < length; i++) {
    64. const span = document.createElement("span");
    65. //为每一张图片添加一个自定义属性"data-index",用它与小按钮进行绑定
    66. span.dataset.index = `${i + 1}`;
    67. if (i === 0) span.classList.add("active");
    68. //内容中执行了四次
    69. span.onclick = showImgs;
    70. frag.append(span);
    71. }
    72. //页面中只渲染了一次,效率大幅提高
    73. parent.append(frag);
    74. }
    75. //按钮事件
    76. function showImgs(ev) {
    77. //1.获取所有图片和按钮
    78. const imgArr = imgGroup.querySelectorAll("img");
    79. const btnArr = btnGroup.querySelectorAll("span");
    80. //2.因为我们要根据用户的点击确定当前应该显示哪一个,所以应该将之前的激活全部取消掉
    81. //但是我们又不知道当前是哪个处于激活状态,全部过一遍
    82. // btnArr.forEach((item) => {
    83. // if (item.classList.contains("active")) item.classList.remove("active");
    84. // });
    85. // imgArr.forEach((item) => {
    86. // if (item.classList.contains("active")) item.classList.remove("active");
    87. // });
    88. //将上面两段代码合并
    89. [btnArr, imgArr].forEach((items) => {
    90. items.forEach((item) => {
    91. if (item.classList.contains("active"))
    92. item.classList.remove("active");
    93. });
    94. //3.再给当前正在点击的按钮添加激活,然后再根据当前的按钮确定应该显示哪一张图片
    95. ev.target.classList.add("active");
    96. imgArr.forEach((img) => {
    97. //这张应该显示的图片的data-index应该与按钮的data-index相等,就显示出来
    98. if (ev.target.dataset.index === img.dataset.index) {
    99. img.classList.add("active");
    100. }
    101. });
    102. });
    103. }
    104. //向前翻页事件
    105. function prevImg() {
    106. //1.当前的图片和当前的按钮
    107. const currentImg = imgGroup.querySelector("img.active");
    108. const currentBtn = btnGroup.querySelector("span.active");
    109. //2.去掉当前图片和按钮的激活方式
    110. currentImg.classList.remove("active");
    111. currentBtn.classList.remove("active");
    112. //3.获取当前图片和按钮的前一个兄弟元素
    113. const prevImg = currentImg.previousElementSibling;
    114. const BtnImg = currentBtn.previousElementSibling;
    115. //4.判断,如果存在前一张图片,就设置为激活
    116. if (prevImg !== null && BtnImg !== null) {
    117. prevImg.classList.add("active");
    118. BtnImg.classList.add("active");
    119. } else {
    120. //将最后一张图片设置为激活显示,实现循环显示
    121. imgGroup.lastElementChild.classList.add("active");
    122. btnGroup.lastElementChild.classList.add("active");
    123. }
    124. }
    125. //向后翻页事件
    126. function nextImg() {
    127. //1.当前的图片和当前的按钮
    128. const currentImg = imgGroup.querySelector("img.active");
    129. const currentBtn = btnGroup.querySelector("span.active");
    130. //2.去掉当前图片和按钮的激活方式
    131. currentImg.classList.remove("active");
    132. currentBtn.classList.remove("active");
    133. //3.获取当前图片和按钮的前一个兄弟元素
    134. const nextImg = currentImg.nextElementSibling;
    135. const nextBtn = currentBtn.nextElementSibling;
    136. //4.判断,如果存在前一张图片,就设置为激活
    137. if (nextImg !== null && nextBtn !== null) {
    138. nextImg.classList.add("active");
    139. nextBtn.classList.add("active");
    140. } else {
    141. //将最后一张图片设置为激活显示,实现循环显示
    142. imgGroup.firstElementChild.classList.add("active");
    143. btnGroup.firstElementChild.classList.add("active");
    144. }
    145. }
    146. //自动播放
    147. //事件派发器dispatchEvent
    148. let timer = null;
    149. let clickEvent = new Event("click");
    150. const contains = document.querySelector(".container");
    151. //鼠标移入时停止自动播放,移出时自动播放
    152. contains.addEventListener("mouseover", stopPlay);
    153. contains.addEventListener("mouseout", autoPlay);
    154. //自动播放
    155. function autoPlay(ev) {
    156. //ev:事件对象,在方法总是有效的
    157. timer = setInterval((ev) => {
    158. document
    159. .querySelector(".skip .next")
    160. .dispatchEvent(clickEvent, nextImg);
    161. }, 2000);
    162. }
    163. //自动停止
    164. function stopPlay() {
    165. clearInterval(timer);
    166. }
    167. </script>
    168. </body>
    169. </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