练习6_DOM轮播图.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <link rel="stylesheet" href="https://at.alicdn.com/t/c/font_4974983_z46pr7nyg5p.css">
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. li {
  14. list-style: none;
  15. }
  16. .swiper-content {
  17. width: 1226px;
  18. height: 460px;
  19. margin: 100px auto;
  20. position: relative;
  21. }
  22. .swiper-nav {
  23. width: 234px;
  24. height: 100%;
  25. background-color: rgba(105, 101, 101, 0.6);
  26. position: relative;
  27. z-index: 1;
  28. }
  29. .swiper-nav ul {
  30. padding: 30px 0;
  31. }
  32. .swiper-nav ul li {
  33. color: #fff;
  34. font-size: 14px;
  35. padding: 0 20px;
  36. height: 42px;
  37. line-height: 42px;
  38. }
  39. .swiper-nav ul li:hover {
  40. background-color: #ff6700;
  41. cursor: pointer;
  42. }
  43. .swiper-nav li i {
  44. float: right;
  45. }
  46. .swiper-content .swiper-img img {
  47. width: 1226px;
  48. height: 460px;
  49. }
  50. .swiper-content .swiper-img li {
  51. position: absolute;
  52. top: 0;
  53. left: 0;
  54. display: none;
  55. }
  56. .swiper-content .swiper-img .active {
  57. display: block;
  58. }
  59. .swiper-btn .arr-btn {
  60. position: absolute;
  61. top: 50%;
  62. margin-top: -35px;
  63. width: 100%;
  64. }
  65. .swiper-btn .arr-btn .left-btn {
  66. position: absolute;
  67. left: 234px;
  68. width: 41px;
  69. height: 70px;
  70. background-image: url("./image/icon-slides.png");
  71. background-position: -84px 0;
  72. }
  73. .swiper-btn .arr-btn .left-btn:hover {
  74. background-position: 0 0;
  75. cursor: pointer;
  76. }
  77. .swiper-btn .arr-btn .right-btn {
  78. position: absolute;
  79. right: 0;
  80. width: 41px;
  81. height: 70px;
  82. background-image: url("./image/icon-slides.png");
  83. background-position: -125px 0;
  84. }
  85. .swiper-btn .arr-btn .right-btn:hover {
  86. background-position: -41px 0;
  87. cursor: pointer;
  88. }
  89. .swiper-btn .dot-btn ul {
  90. position: absolute;
  91. right: 30px;
  92. bottom: 30px;
  93. }
  94. .swiper-btn .dot-btn li {
  95. width: 6px;
  96. height: 6px;
  97. background-color: rgba(0, 0, 0, .4);
  98. border: 2px solid hsla(0, 0%, 100%, .3);
  99. border-radius: 50%;
  100. float: left;
  101. margin: 0 5px;
  102. }
  103. .swiper-btn .dot-btn li:hover {
  104. background: hsla(0, 0%, 100%, .4);
  105. border-color: rgba(0, 0, 0, .4);
  106. cursor: pointer;
  107. }
  108. .swiper-btn .dot-btn .active {
  109. background: hsla(0, 0%, 100%, .4);
  110. border-color: rgba(0, 0, 0, .4);
  111. }
  112. </style>
  113. </head>
  114. <body>
  115. <div class="swiper-content">
  116. <!-- 左侧导航 -->
  117. <div class="swiper-nav">
  118. <ul>
  119. <li>手机 <i class="iconfont icon-arrow"></i> </li>
  120. <li>电视 <i class="iconfont icon-arrow"></i> </li>
  121. <li>家电 <i class="iconfont icon-arrow"></i></li>
  122. <li>笔记本 平板 显示器 <i class="iconfont icon-arrow"></i></li>
  123. <li>出行 穿戴 <i class="iconfont icon-arrow"></i></li>
  124. <li>耳机 音箱 <i class="iconfont icon-arrow"></i></li>
  125. <li>健康 儿童 <i class="iconfont icon-arrow"></i></li>
  126. <li>生活 箱包 <i class="iconfont icon-arrow"></i></li>
  127. <li>智能 路由器 <i class="iconfont icon-arrow"></i></li>
  128. <li>电源 配件 <i class="iconfont icon-arrow"></i></li>
  129. </ul>
  130. </div>
  131. <!-- 轮播图 图片区域 -->
  132. <div class="swiper-img">
  133. <ul>
  134. <li class="swiper-item active">
  135. <img src="./image/slider1.jpg" alt="">
  136. </li>
  137. <li class="swiper-item">
  138. <img src="./image/slider2.jpeg" alt="">
  139. </li>
  140. <li class="swiper-item">
  141. <img src="./image/slider3.png" alt="">
  142. </li>
  143. <li class="swiper-item">
  144. <img src="./image/slider4.jpeg" alt="">
  145. </li>
  146. <li class="swiper-item">
  147. <img src="./image/slider5.jpg" alt="">
  148. </li>
  149. <li class="swiper-item">
  150. <img src="./image/slider6.jpg" alt="">
  151. </li>
  152. </ul>
  153. </div>
  154. <!-- 轮播图 按钮区域 -->
  155. <div class="swiper-btn">
  156. <div class="arr-btn">
  157. <div class="left-btn"></div>
  158. <div class="right-btn"></div>
  159. </div>
  160. <div class="dot-btn">
  161. <ul>
  162. <li class="dot-item active"></li>
  163. <li class="dot-item"></li>
  164. <li class="dot-item"></li>
  165. <li class="dot-item"></li>
  166. <li class="dot-item"></li>
  167. <li class="dot-item"></li>
  168. </ul>
  169. </div>
  170. </div>
  171. </div>
  172. <script>
  173. // 第一步获取要控制的元素
  174. // 获取小点按钮
  175. var dotItem = document.getElementsByClassName("dot-item");
  176. // 获取轮播图图片
  177. var swiperItem = document.getElementsByClassName("swiper-item");
  178. // 获取向左按钮
  179. var leftBtn = document.getElementsByClassName("left-btn");
  180. leftBtn = leftBtn[0];
  181. // 获取向右按钮
  182. var rightBtn = document.getElementsByClassName("right-btn");
  183. rightBtn = rightBtn[0];
  184. // 获取轮播图容器
  185. var swiperContent = document.getElementsByClassName("swiper-content");
  186. swiperContent = swiperContent[0];
  187. // 定义变量保存当前是第几个轮播图
  188. var nowIndex = 0;
  189. // 定义定时函数的接收变量
  190. var timer = null;
  191. // 把切换图片封装成一个函数
  192. // 你告诉这个显示第几张图片即可
  193. // k 带标要显示的轮播图
  194. function changeImg(k) {
  195. // 移除所有被选中的的轮播图和小圆点
  196. for (var i = 0; i < swiperItem.length; i++) {
  197. // 移除轮播图选中效果
  198. swiperItem[i].classList.remove("active");
  199. // 移除小圆点选中效果
  200. dotItem[i].classList.remove("active");
  201. }
  202. // 给选中的轮播图添加选中效果
  203. swiperItem[k].classList.add("active");
  204. // 给选中的小圆点添加选中效果
  205. dotItem[k].classList.add("active");
  206. }
  207. // 第二步为小圆点绑定事件
  208. for (var i = 0; i < dotItem.length; i++) {
  209. // 为每一个绑定事件的标签分配一个编号
  210. dotItem[i].index = i;
  211. // 为每一个按钮绑定事件
  212. dotItem[i].onclick = function () {
  213. // 同步当前索引
  214. nowIndex = this.index;
  215. // 调用切换图片函数
  216. changeImg(nowIndex);
  217. // console.log(this.index);
  218. // // 第四步 移除没有被选中的轮播图及效果
  219. // for(var j=0;j<dotItem.length;j++){
  220. // swiperItem[j].classList.remove("active");
  221. // dotItem[j].classList.remove("active");
  222. // }
  223. // // 第三步切换轮播图图片 及选中效果
  224. // swiperItem[this.index].classList.add("active");
  225. // // 给当前点击的小点加上选中效果
  226. // this.classList.add("active");
  227. }
  228. }
  229. // 为向右按钮绑定事件
  230. rightBtn.onclick = function () {
  231. // 每次点击向右按钮进行 让当前索引nowIndex +1操作
  232. nowIndex++;
  233. // 如果超出了轮播图数量的范围 返回第一张图片
  234. if (nowIndex >= swiperItem.length) {
  235. nowIndex = 0
  236. }
  237. // 调用切换图片的函数
  238. changeImg(nowIndex);
  239. // nowIndex++;
  240. // nowIndex%=6;
  241. // for(var i=0;i<swiperItem.length;i++){
  242. // swiperItem[i].classList.remove("active");
  243. // dotItem[i].classList.remove("active");
  244. // }
  245. // swiperItem[nowIndex].classList.add("active");
  246. // dotItem[nowIndex].classList.add("active");
  247. }
  248. // 为向左按钮绑定事件
  249. leftBtn.onclick = function () {
  250. nowIndex--;
  251. if (nowIndex < 0) {
  252. nowIndex = swiperItem.length - 1;
  253. }
  254. changeImg(nowIndex);
  255. }
  256. // 定时播放
  257. function play() {
  258. timer = setInterval(function () {
  259. nowIndex++
  260. if (nowIndex >= swiperItem.length) {
  261. nowIndex = 0;
  262. }
  263. changeImg(nowIndex)
  264. }, 2000)
  265. }
  266. // 调用定时播放函数
  267. play();
  268. // 鼠标移入清除定时函数
  269. swiperContent.onmouseenter = function () {
  270. clearInterval(timer);
  271. }
  272. // 鼠标移出继续定时轮播
  273. swiperContent.onmouseleave = function () {
  274. play();
  275. }
  276. </script>
  277. </body>
  278. </html>