b.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. body{
  9. margin: 0;
  10. }
  11. #carousel-container {
  12. width: 100%;
  13. height: 400px;
  14. position: relative;
  15. overflow: hidden;
  16. }
  17. #carousel-container img {
  18. width: 100%;
  19. height: auto;
  20. position: absolute;
  21. top: 0;
  22. left: 0;
  23. opacity: 0;
  24. transition: opacity 0.5s ease-in-out;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="carousel-container">
  30. <img src="./themes/laughing/source/img/1.png" alt="Image 1">
  31. <img src="./themes/laughing/source/img/2.png" alt="Image 1">
  32. <img src="./themes/laughing/source/img/3.png" alt="Image 1">
  33. </div>
  34. <script>
  35. // 获取轮播图容器和图片元素
  36. var container = document.getElementById('carousel-container');
  37. var images = container.getElementsByTagName('img');
  38. // 定义索引变量
  39. var currentIndex = 0;
  40. // 切换图片的函数
  41. function showImage(index) {
  42. // 隐藏所有图片
  43. for (var i = 0; i < images.length; i++) {
  44. images[i].style.opacity = 0;
  45. }
  46. // 显示指定索引的图片
  47. images[index].style.opacity = 1;
  48. }
  49. // 自动切换图片的函数
  50. function autoSlide() {
  51. // 切换到下一张图片
  52. currentIndex++;
  53. if (currentIndex >= images.length) {
  54. currentIndex = 0;
  55. }
  56. // 显示当前图片
  57. showImage(currentIndex);
  58. }
  59. // 定时触发自动切换
  60. setInterval(autoSlide, 2000);
  61. </script>
  62. </body>
  63. </html>