轮播图.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!--
  2. author: zsy
  3. date: 2022-10-29
  4. 轮播图
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>Document</title>
  13. <link rel="stylesheet" href="font/iconfont.css">
  14. <style>
  15. * {
  16. margin: 0;
  17. padding: 0;
  18. }
  19. ul {
  20. list-style: none;
  21. }
  22. #container {
  23. width: 590px;
  24. height: 470px;
  25. margin: 100px auto;
  26. position: relative;
  27. }
  28. .actived {
  29. display: none;
  30. }
  31. .choice {
  32. display: block;
  33. }
  34. #btns {
  35. position: absolute;
  36. right: 20px;
  37. bottom: 20px;
  38. }
  39. #btns li {
  40. width: 20px;
  41. height: 20px;
  42. text-align: center;
  43. line-height: 20px;
  44. background: cyan;
  45. border-radius: 10px;
  46. color: white;
  47. float: left;
  48. margin-right: 7px;
  49. }
  50. #btns .select {
  51. background: red;
  52. }
  53. #prev,
  54. #next {
  55. width: 40px;
  56. height: 40px;
  57. position: absolute;
  58. top: 215px;
  59. opacity: 0.4;
  60. display: none;
  61. }
  62. #next {
  63. right: 0;
  64. }
  65. #prev span {
  66. font-size: 40px;
  67. }
  68. #next span {
  69. font-size: 40px;
  70. }
  71. </style>
  72. </head>
  73. <body>
  74. <div id="container">
  75. <div id="img-box">
  76. <img class="actived choice" src="image/1.jpg" alt="">
  77. <img class="actived" src="image/2.jpg" alt="">
  78. <img class="actived" src="image/3.jpg" alt="">
  79. <img class="actived" src="image/4.jpg" alt="">
  80. <img class="actived" src="image/5.jpg" alt="">
  81. </div>
  82. <ul id="btns">
  83. <li class="select">1</li>
  84. <li>2</li>
  85. <li>3</li>
  86. <li>4</li>
  87. <li>5</li>
  88. </ul>
  89. <div id="prev">
  90. <span class="iconfont icon-shangyige"></span>
  91. </div>
  92. <div id="next">
  93. <span class="iconfont icon-xiayige"></span>
  94. </div>
  95. </div>
  96. <script>
  97. var con = document.getElementById('container')
  98. var btns = document.getElementById('btns')
  99. var uli = btns.getElementsByTagName('li')
  100. var imgs = document.getElementsByClassName('actived')
  101. var next = document.getElementById('next')
  102. var prev = document.getElementById('prev')
  103. var iNow = 0;
  104. //循环按钮的每一项
  105. for (var i = 0; i < uli.length; i++) {
  106. //给点击的按钮 索引提取出来
  107. uli[i].index = i
  108. //按钮的点击事件
  109. uli[i].onclick = function () {
  110. // for (var j = 0; j < uli.length; j++) {
  111. // uli[j].className = ''
  112. // imgs[j].className = 'actived'
  113. // }
  114. // iNow = this.index
  115. // this.className = 'select'// this =>uli[i]
  116. // imgs[this.index].className = 'actived choice'
  117. //给全局图片的变量 赋值为当前点击的索引
  118. iNow = this.index
  119. //传参
  120. myFun(iNow)
  121. }
  122. }
  123. //点击下一个
  124. next.onclick = function () {
  125. iNow++;
  126. console.log(iNow)
  127. if (iNow > 4) {
  128. iNow = 0
  129. }
  130. myFun(iNow)
  131. }
  132. //点击上一个
  133. prev.onclick = function () {
  134. iNow--;
  135. if (iNow < 0) {
  136. iNow = 4 //uli.length
  137. }
  138. myFun(iNow)
  139. }
  140. //清空样式,找到点击的索引设置样式
  141. var myFun = function (xx) {
  142. //清空所有样式
  143. for (var i = 0; i < uli.length; i++) {
  144. uli[i].className = ''
  145. imgs[i].className = 'actived'
  146. }
  147. //给点击的按钮设置样式
  148. uli[xx].className = 'select'
  149. //找到点击按钮的索引 给相应的图片设置样式
  150. imgs[xx].className = 'actived choice'
  151. }
  152. //鼠标划入在上面移动 onmousemove
  153. //鼠标划出 onmouseout
  154. //鼠标划入事件
  155. con.onmousemove = function () {
  156. next.style.display = 'block'
  157. prev.style.display = 'block'
  158. clearInterval(timer)
  159. }
  160. //鼠标划出事件
  161. con.onmouseout = function () {
  162. next.style.display = 'none'
  163. prev.style.display = 'none'
  164. timer = setInterval(function () {
  165. next.onclick()
  166. }, 1000)
  167. }
  168. //设置定时器,自动执行点击下一个事件
  169. var timer = setInterval(function () {
  170. next.onclick()
  171. }, 1000)
  172. </script>
  173. </body>
  174. </html>