html+js 轮播图

打印 上一主题 下一主题

主题 1015|帖子 1015|积分 3045

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x


  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>轮播图示例</title>
  7.     <style>
  8.         /* 基本样式 */
  9.         body {
  10.             margin: 0;
  11.             padding: 0;
  12.             display: flex;
  13.             justify-content: center;
  14.             align-items: center;
  15.             height: 100vh;
  16.             background-color: #f0f0f0;
  17.             font-family: Arial, sans-serif;
  18.         }
  19.         /* 轮播图容器 */
  20.         .carousel {
  21.             width: 600px;
  22.             height: 400px;
  23.             position: relative;
  24.             overflow: hidden;
  25.             border-radius: 10px;
  26.             box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  27.         }
  28.         /* 图片容器 */
  29.         .carousel-images {
  30.             display: flex;
  31.             width: 100%;
  32.             height: 100%;
  33.             transition: transform 0.5s ease-in-out;
  34.         }
  35.         /* 图片样式 */
  36.         .carousel-images img {
  37.             width: 100%;
  38.             height: 100%;
  39.             object-fit: cover;
  40.             flex-shrink: 0;
  41.         }
  42.         /* 左右按钮 */
  43.         .carousel-button {
  44.             position: absolute;
  45.             top: 50%;
  46.             transform: translateY(-50%);
  47.             background-color: rgba(0, 0, 0, 0.5);
  48.             color: white;
  49.             border: none;
  50.             padding: 10px;
  51.             cursor: pointer;
  52.             font-size: 18px;
  53.             border-radius: 50%;
  54.             transition: background-color 0.3s ease;
  55.         }
  56.         .carousel-button:hover {
  57.             background-color: rgba(0, 0, 0, 0.8);
  58.         }
  59.         /* 左按钮 */
  60.         .carousel-button.prev {
  61.             left: 10px;
  62.         }
  63.         /* 右按钮 */
  64.         .carousel-button.next {
  65.             right: 10px;
  66.         }
  67.         /* 指示器容器 */
  68.         .carousel-indicators {
  69.             position: absolute;
  70.             bottom: 10px;
  71.             left: 50%;
  72.             transform: translateX(-50%);
  73.             display: flex;
  74.             gap: 5px;
  75.         }
  76.         /* 指示器样式 */
  77.         .carousel-indicators span {
  78.             width: 10px;
  79.             height: 10px;
  80.             background-color: rgba(255, 255, 255, 0.5);
  81.             border-radius: 50%;
  82.             cursor: pointer;
  83.             transition: background-color 0.3s ease;
  84.         }
  85.         .carousel-indicators span.active {
  86.             background-color: white;
  87.         }
  88.     </style>
  89. </head>
  90. <body>
  91.     <!-- 轮播图容器 -->
  92.     <div class="carousel">
  93.         <!-- 图片容器 -->
  94.         <div class="carousel-images">
  95.             <img src="https://image.meiye.art/pic_1631411429199mjIE7yxqjZxWNdOvWLxL2?imageMogr2/thumbnail/640x/interlace/1" alt="Image 1">
  96.             <img src="https://image.meiye.art/pic_16324946992753vSLEv0P2s-1PY95ynOZn?imageMogr2/thumbnail/640x/interlace/1" alt="Image 2">
  97.             <img src="https://image.meiye.art/pic_1628403749737?imageMogr2/thumbnail/640x/interlace/1" alt="Image 3">
  98.         </div>
  99.         <!-- 左右按钮 -->
  100.         <button class="carousel-button prev">&lt;</button>
  101.         <button class="carousel-button next">&gt;</button>
  102.         <!-- 指示器 -->
  103.         <div class="carousel-indicators">
  104.             <span class="active"></span>
  105.             <span></span>
  106.             <span></span>
  107.         </div>
  108.     </div>
  109.     <script>
  110.         // 获取元素
  111.         const carouselImages = document.querySelector('.carousel-images');
  112.         const prevButton = document.querySelector('.carousel-button.prev');
  113.         const nextButton = document.querySelector('.carousel-button.next');
  114.         const indicators = document.querySelectorAll('.carousel-indicators span');
  115.         let currentIndex = 0; // 当前显示的图片索引
  116.         const totalImages = carouselImages.children.length; // 图片总数
  117.         // 更新指示器状态
  118.         function updateIndicators() {
  119.             indicators.forEach((indicator, index) => {
  120.                 indicator.classList.toggle('active', index === currentIndex);
  121.             });
  122.         }
  123.         // 切换到指定图片
  124.         function goToImage(index) {
  125.             if (index < 0) {
  126.                 index = totalImages - 1; // 如果小于0,切换到最后一张
  127.             } else if (index >= totalImages) {
  128.                 index = 0; // 如果超出范围,切换到第一张
  129.             }
  130.             currentIndex = index;
  131.             carouselImages.style.transform = `translateX(-${currentIndex * 100}%)`;
  132.             updateIndicators();
  133.         }
  134.         // 切换到上一张图片
  135.         prevButton.addEventListener('click', () => {
  136.             goToImage(currentIndex - 1);
  137.         });
  138.         // 切换到下一张图片
  139.         nextButton.addEventListener('click', () => {
  140.             goToImage(currentIndex + 1);
  141.         });
  142.         // 点击指示器切换图片
  143.         indicators.forEach((indicator, index) => {
  144.             indicator.addEventListener('click', () => {
  145.                 goToImage(index);
  146.             });
  147.         });
  148.         // 自动播放
  149.         let autoPlayInterval = setInterval(() => {
  150.             goToImage(currentIndex + 1);
  151.         }, 3000);
  152.         // 鼠标悬停时停止自动播放
  153.         const carousel = document.querySelector('.carousel');
  154.         carousel.addEventListener('mouseenter', () => {
  155.             clearInterval(autoPlayInterval);
  156.         });
  157.         // 鼠标离开时恢复自动播放
  158.         carousel.addEventListener('mouseleave', () => {
  159.             autoPlayInterval = setInterval(() => {
  160.                 goToImage(currentIndex + 1);
  161.             }, 3000);
  162.         });
  163.     </script>
  164. </body>
  165. </html>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表