3_淡入淡出轮播图.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. ul {
  14. list-style: none;
  15. }
  16. #container {
  17. width: 590px;
  18. height: 470px;
  19. margin: 100px auto;
  20. position: relative;
  21. }
  22. #img-box img {
  23. position: absolute;
  24. opacity: 0;
  25. transition: opacity 1s linear;
  26. }
  27. #img-box .selected {
  28. opacity: 1;
  29. }
  30. #btns li {
  31. width: 20px;
  32. height: 20px;
  33. background: purple;
  34. color: white;
  35. text-align: center;
  36. line-height: 20px;
  37. border-radius: 10px;
  38. margin-right: 5px;
  39. float: left;
  40. }
  41. #btns {
  42. position: absolute;
  43. right: 10px;
  44. bottom: 10px;
  45. }
  46. #btns .select {
  47. background: red;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="container">
  53. <div id="img-box">
  54. <img class="selected" src="image/1.jpg" alt="">
  55. <img src="image/2.jpg" alt="">
  56. <img src="image/3.jpg" alt="">
  57. <img src="image/4.jpg" alt="">
  58. <img src="image/5.jpg" alt="">
  59. </div>
  60. <ul id="btns">
  61. <li class="select">1</li>
  62. <li>2</li>
  63. <li>3</li>
  64. <li>4</li>
  65. <li>5</li>
  66. </ul>
  67. <script>
  68. var btns = document.getElementsByTagName('li')
  69. var imgs = document.getElementsByTagName('img')
  70. for (var i = 0; i < btns.length; i++) {
  71. btns[i].index = i
  72. btns[i].onclick = function () {
  73. for (var j = 0; j < btns.length; j++) {
  74. btns[j].className = ''
  75. imgs[j].className = ''
  76. }
  77. this.className = 'select'
  78. imgs[this.index].className = 'selected'
  79. }
  80. }
  81. </script>
  82. </div>
  83. </body>
  84. </html>