2_轮播图.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. overflow: hidden;
  23. }
  24. #img-box {
  25. width: 2950px;
  26. position: absolute;
  27. left: 0;
  28. /* 向左移动 延迟时间 匀速执行 */
  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. border-radius: 10px;
  43. background: aqua;
  44. text-align: center;
  45. line-height: 20px;
  46. color: white;
  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. top: 215px;
  59. opacity: 0.4;
  60. display: none;
  61. }
  62. #next {
  63. right: 0;
  64. }
  65. #next span {
  66. font-size: 40px;
  67. }
  68. #prev span {
  69. font-size: 40px;
  70. }
  71. </style>
  72. </head>
  73. <body>
  74. <div id="container">
  75. <div id="img-box">
  76. <img class="selected choice" src="image/1.jpg" alt="">
  77. <img class="selected" src="image/2.jpg" alt="">
  78. <img class="selected" src="image/3.jpg" alt="">
  79. <img class="selected" src="image/4.jpg" alt="">
  80. <img class="selected" 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="next">
  90. <span class="iconfont icon-next"></span>
  91. </div>
  92. <div id="prev">
  93. <span class="iconfont icon-prev"></span>
  94. </div>
  95. </div>
  96. <script>
  97. var btns = document.getElementsByTagName('li')
  98. var imgs = document.getElementsByClassName('selected')
  99. var imgBox = document.getElementById('img-box')
  100. for (var i = 0; i < btns.length; i++) {
  101. btns[i].index = i
  102. btns[i].onclick = function () {
  103. for (var k = 0; k < btns.length; k++) {
  104. btns[k].className = ''
  105. }
  106. this.className = 'select'
  107. //是反方向的
  108. imgBox.style.left = -590 * this.index + 'px'
  109. }
  110. }
  111. </script>
  112. </body>
  113. </html>