轮播图.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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>Document</title>
  8. <link rel="stylesheet" href="icon/iconfont.css">
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. ul {
  15. list-style: none;
  16. }
  17. #container {
  18. width: 590px;
  19. height: 470px;
  20. margin: 100px auto;
  21. position: relative;
  22. }
  23. .selected {
  24. display: none;
  25. }
  26. .choice {
  27. display: block;
  28. }
  29. #btns {
  30. position: absolute;
  31. right: 10px;
  32. bottom: 10px;
  33. }
  34. #btns li {
  35. width: 20px;
  36. height: 20px;
  37. background: aqua;
  38. color: #eee;
  39. border-radius: 10px;
  40. text-align: center;
  41. line-height: 20px;
  42. float: left;
  43. margin-right: 5px;
  44. }
  45. #btns .select {
  46. background: red;
  47. }
  48. #next,
  49. #prev {
  50. width: 40px;
  51. height: 40px;
  52. position: absolute;
  53. top: 215px;
  54. opacity: 0.4;
  55. display: none;
  56. }
  57. #next {
  58. right: 0;
  59. }
  60. #next span {
  61. font-size: 40px;
  62. }
  63. #prev span {
  64. font-size: 40px;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <div id="container">
  70. <div id="img-box">
  71. <img class="selected choice" src="image/1.jpg" alt="">
  72. <img class="selected" src="image/2.jpg" alt="">
  73. <img class="selected" src="image/3.jpg" alt="">
  74. <img class="selected" src="image/4.jpg" alt="">
  75. <img class="selected" src="image/5.jpg" alt="">
  76. </div>
  77. <ul id="btns">
  78. <li class="select">1</li>
  79. <li>2</li>
  80. <li>3</li>
  81. <li>4</li>
  82. <li>5</li>
  83. </ul>
  84. <div id="next">
  85. <span class="iconfont icon-24gl-nextSquare"></span>
  86. </div>
  87. <div id="prev">
  88. <span class="iconfont icon-24gl-previousSquare"></span>
  89. </div>
  90. </div>
  91. <script>
  92. var btn = document.getElementsByTagName('li')
  93. var imgs = document.getElementsByClassName('selected')
  94. var next = document.getElementById('next')
  95. var prev = document.getElementById('prev')
  96. var container = document.getElementById('container')
  97. iNow = 0;
  98. for (var i = 0; i < btn.length; i++) {
  99. btn[i].index = i
  100. btn[i].onclick = function () {
  101. for (var j = 0; j < btn.length; j++) {
  102. btn[j].className = ''
  103. imgs[j].className = "selected"
  104. }
  105. this.className = 'select'
  106. imgs[this.index].className = 'selected choice'
  107. iNow = this.index
  108. }
  109. }
  110. //下一个点击事件
  111. next.onclick = function () {
  112. console.log(iNow)
  113. iNow++;
  114. if (iNow > btn.length - 1) {
  115. iNow = 0
  116. }
  117. // for (var i = 0; i < btn.length; i++) {
  118. // btn[i].className = ''
  119. // imgs[i].className = "selected"
  120. // }
  121. // btn[iNow].className = 'select'
  122. // imgs[iNow].className = 'selected choice'
  123. myFun(iNow)
  124. }
  125. //上一个点击事件
  126. prev.onclick = function () {
  127. iNow--;
  128. if (iNow < 0) {
  129. iNow = btn.length - 1
  130. }
  131. myFun(iNow)
  132. }
  133. //鼠标划入事件
  134. container.onmousemove = function () {
  135. next.style.display = 'block'
  136. prev.style.display = 'block'
  137. clearInterval(timer)
  138. }
  139. //鼠标划出事件
  140. container.onmouseout = function () {
  141. next.style.display = 'none'
  142. prev.style.display = 'none'
  143. timer = setInterval(function () {
  144. next.onclick()
  145. }, 2000)
  146. }
  147. //定时器
  148. var timer = setInterval(function () {
  149. next.onclick()
  150. }, 2000)
  151. var myFun = function (xx) {
  152. for (var i = 0; i < btn.length; i++) {
  153. btn[i].className = ''
  154. imgs[i].className = "selected"
  155. }
  156. btn[xx].className = 'select'
  157. imgs[xx].className = 'selected choice'
  158. }
  159. </script>
  160. </body>
  161. </html>