博客列表 >原生轮播图

原生轮播图

再见羊肉串儿
再见羊肉串儿原创
2021年08月03日 20:18:45567浏览
  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. <style>
  9. /* 通用样式 */
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. li {
  16. list-style: none;
  17. }
  18. a {
  19. text-decoration: none;
  20. }
  21. /* 容器样式 */
  22. .container {
  23. /* border: 1px solid; */
  24. width: 90%;
  25. height: 20rem;
  26. position: relative;
  27. top: 2rem;
  28. margin: auto;
  29. }
  30. /* 按钮组样式 */
  31. .container > .btnsGroup {
  32. width: 10rem;
  33. position: absolute;
  34. bottom: 0;
  35. left: 0;
  36. right: 0;
  37. margin: auto;
  38. }
  39. .container > .btnsGroup > a {
  40. display: inline-block;
  41. border: 0.5rem solid gray;
  42. border-radius: 50%;
  43. margin: 0.5rem;
  44. }
  45. .container > .btnsGroup > a:hover {
  46. cursor: pointer;
  47. }
  48. .container > .btnsGroup > .active {
  49. border-color: black;
  50. }
  51. /* 左右按钮样式 */
  52. .container > .changeimgs > span {
  53. position: absolute;
  54. top: 7.5rem;
  55. display: block;
  56. /* border: 1px solid; */
  57. width: 3rem;
  58. height: 5rem;
  59. text-align: center;
  60. line-height: 5rem;
  61. background-color: ghostwhite;
  62. font-weight: bolder;
  63. opacity: 0.3;
  64. }
  65. .container > .changeimgs > .next {
  66. right: 0;
  67. }
  68. /* 图片样式 */
  69. .container > .imgsGroup img {
  70. display: none;
  71. width: 100%;
  72. height: 20rem;
  73. }
  74. .container > .imgsGroup > .active {
  75. display: block;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <div class="container">
  81. <!-- 图片组 -->
  82. <div class="imgsGroup"></div>
  83. <!-- 按钮组 -->
  84. <div class="btnsGroup"></div>
  85. <!-- 左右按钮 -->
  86. <div class="changeimgs">
  87. <span class="prev" onclick="prev(this);">&lt;</span>
  88. <span class="next" onclick="next();">&gt;</span>
  89. </div>
  90. </div>
  91. <script>
  92. let imgs = [
  93. "images/banner1.jpg",
  94. "images/banner2.jpg",
  95. "images/banner3.jpg",
  96. "images/banner4.jpg",
  97. ];
  98. // 按钮组
  99. let btnsGroup = document.querySelector(".btnsGroup");
  100. // 图片组
  101. let imgsGroup = document.querySelector(".imgsGroup");
  102. //load,窗口加载完成后自动执行
  103. window.onload = function () {
  104. createImgs(imgsGroup, imgs.length);
  105. createBtns(btnsGroup, imgs.length);
  106. };
  107. //插入图片
  108. function createImgs(parent, length) {
  109. //创建临时的文档片段,把图片先插入文档片段,然后最终一次性渲染页面
  110. let frag = document.createDocumentFragment();
  111. for (let i = 0; i < length; i++) {
  112. let im = document.createElement("img");
  113. if (i == 0) {
  114. im.classList.add("active");
  115. }
  116. im.dataset.index = `${i + 1}`;
  117. im.src = imgs[i];
  118. frag.append(im);
  119. }
  120. imgsGroup.append(frag);
  121. }
  122. //插入按钮
  123. function createBtns(parent, length) {
  124. //创建临时的文档片段,把按钮先插入文档片段,然后最终一次性渲染页面
  125. let frag = document.createDocumentFragment();
  126. for (let i = 0; i < length; i++) {
  127. let btn = document.createElement("a");
  128. if (i == 0) {
  129. btn.classList.add("active");
  130. }
  131. btn.dataset.index = i + 1;
  132. btn.onclick = showImgs;
  133. frag.append(btn);
  134. }
  135. btnsGroup.append(frag);
  136. }
  137. //点击小按钮切换图片
  138. function showImgs(ev) {
  139. let imgArr = imgsGroup.querySelectorAll("img");
  140. let btnArr = btnsGroup.querySelectorAll("a");
  141. btnArr.forEach(function (item) {
  142. if (item.classList.contains("active"))
  143. item.classList.remove("active");
  144. });
  145. imgArr.forEach(function (items) {
  146. if (items.classList.contains("active"))
  147. items.classList.remove("active");
  148. if (items.dataset.index == ev.target.dataset.index)
  149. items.classList.add("active");
  150. });
  151. ev.target.classList.add("active");
  152. }
  153. // 左边按钮
  154. function prev() {
  155. let currentImg = imgsGroup.querySelector("img.active");
  156. let currentBtn = btnsGroup.querySelector("a.active");
  157. currentImg.classList.remove("active");
  158. currentBtn.classList.remove("active");
  159. // 获取上一个兄弟元素,如果不为空就给他们active属性
  160. let prevImg = currentImg.previousElementSibling;
  161. let prevBtn = currentBtn.previousElementSibling;
  162. if (prevImg !== null && prevBtn !== null) {
  163. prevImg.classList.add("active");
  164. prevBtn.classList.add("active");
  165. } else {
  166. imgsGroup.querySelector("img:last-of-type").classList.add("active");
  167. btnsGroup.querySelector("a:last-of-type").classList.add("active");
  168. }
  169. }
  170. //右边按钮
  171. function next() {
  172. let currentImg = imgsGroup.querySelector("img.active");
  173. let currentBtn = btnsGroup.querySelector("a.active");
  174. currentImg.classList.remove("active");
  175. currentBtn.classList.remove("active");
  176. // 获取下一个兄弟元素,如果不为空,就给active属性
  177. let prevImg = currentImg.nextElementSibling;
  178. let prevBtn = currentBtn.nextElementSibling;
  179. if (prevImg !== null && prevBtn !== null) {
  180. prevImg.classList.add("active");
  181. prevBtn.classList.add("active");
  182. } else {
  183. imgsGroup.querySelector("img:first-of-type").classList.add("active");
  184. btnsGroup.querySelector("a:first-of-type").classList.add("active");
  185. }
  186. }
  187. //自动播放
  188. // 定时器,就是一个整数,用来标识一个定时任务
  189. let timer = null;
  190. let clickEvent = new Event("click");
  191. let container = document.querySelector(".container");
  192. container.addEventListener("mouseover", stopPlay);
  193. container.addEventListener("mouseout", startPlay);
  194. function startPlay(ev) {
  195. timer = setInterval(function () {
  196. document
  197. .querySelector(".changeimgs .next")
  198. .dispatchEvent(clickEvent, next);
  199. }, 2000);
  200. }
  201. function stopPlay() {
  202. clearInterval(timer);
  203. }
  204. </script>
  205. </body>
  206. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议