2_滑动轮播图.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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="font/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. overflow: hidden;
  21. margin: 100px auto;
  22. position: relative;
  23. }
  24. #img-box {
  25. width: 2950px;
  26. position: absolute;
  27. /* 向左移动 延迟时间1s 匀速执行 */
  28. left: 0;
  29. transition: left 1s linear;
  30. }
  31. #img-box img {
  32. float: left;
  33. }
  34. #btns {
  35. position: absolute;
  36. right: 10px;
  37. bottom: 10px;
  38. }
  39. #btns li {
  40. width: 20px;
  41. height: 20px;
  42. background: aqua;
  43. color: white;
  44. text-align: center;
  45. line-height: 20px;
  46. border-radius: 10px;
  47. float: left;
  48. margin-right: 5px;
  49. }
  50. #btns .select {
  51. background: red;
  52. }
  53. #next,
  54. #prev {
  55. width: 40px;
  56. height: 40px;
  57. position: absolute;
  58. opacity: 0.4;
  59. top: 215px;
  60. }
  61. #next {
  62. right: 0;
  63. }
  64. #next span {
  65. font-size: 40px;
  66. }
  67. #prev span {
  68. font-size: 40px;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div id="container">
  74. <div id="img-box">
  75. <img class="selected choice" src="image/1.jpg" alt="">
  76. <img class="selected" src="image/2.jpg" alt="">
  77. <img class="selected" src="image/3.jpg" alt="">
  78. <img class="selected" src="image/4.jpg" alt="">
  79. <img class="selected" src="image/5.jpg" alt="">
  80. </div>
  81. <ul id="btns">
  82. <li class="select">1</li>
  83. <li>2</li>
  84. <li>3</li>
  85. <li>4</li>
  86. <li>5</li>
  87. </ul>
  88. <div id="next">
  89. <span class="iconfont icon-next"></span>
  90. </div>
  91. <div id="prev">
  92. <span class="iconfont icon-prev"></span>
  93. </div>
  94. </div>
  95. <script>
  96. var btn = document.getElementsByTagName('li')
  97. var imgs = document.getElementsByClassName('selected')
  98. var imgBox = document.getElementById('img-box')
  99. var next = document.getElementById('next')
  100. var prev = document.getElementById('prev')
  101. iNow = 0;
  102. for (var i = 0; i < btn.length; i++) {
  103. btn[i].index = i
  104. btn[i].onclick = function () {
  105. for (var j = 0; j < btn.length; j++) {
  106. btn[j].className = ''
  107. }
  108. iNow = this.index
  109. this.className = 'select'
  110. imgBox.style.left = -590 * this.index + 'px'
  111. }
  112. }
  113. next.onclick = function () {
  114. iNow++;
  115. if(iNow > 4){
  116. iNow = 0
  117. }
  118. for(var i=0;i<btn.length;i++){
  119. btn[i].className = ''
  120. }
  121. btn[iNow].className = 'select'
  122. imgBox.style.left = -590 * iNow + 'px'
  123. }
  124. </script>
  125. </body>
  126. </html>