当前位置: 首页 > article >正文

html+js 轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图示例</title>
    <style>
        /* 基本样式 */
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }

        /* 轮播图容器 */
        .carousel {
            width: 600px;
            height: 400px;
            position: relative;
            overflow: hidden;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }

        /* 图片容器 */
        .carousel-images {
            display: flex;
            width: 100%;
            height: 100%;
            transition: transform 0.5s ease-in-out;
        }

        /* 图片样式 */
        .carousel-images img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            flex-shrink: 0;
        }

        /* 左右按钮 */
        .carousel-button {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            padding: 10px;
            cursor: pointer;
            font-size: 18px;
            border-radius: 50%;
            transition: background-color 0.3s ease;
        }

        .carousel-button:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }

        /* 左按钮 */
        .carousel-button.prev {
            left: 10px;
        }

        /* 右按钮 */
        .carousel-button.next {
            right: 10px;
        }

        /* 指示器容器 */
        .carousel-indicators {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            gap: 5px;
        }

        /* 指示器样式 */
        .carousel-indicators span {
            width: 10px;
            height: 10px;
            background-color: rgba(255, 255, 255, 0.5);
            border-radius: 50%;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .carousel-indicators span.active {
            background-color: white;
        }
    </style>
</head>
<body>
    <!-- 轮播图容器 -->
    <div class="carousel">
        <!-- 图片容器 -->
        <div class="carousel-images">
            <img src="https://image.meiye.art/pic_1631411429199mjIE7yxqjZxWNdOvWLxL2?imageMogr2/thumbnail/640x/interlace/1" alt="Image 1">
            <img src="https://image.meiye.art/pic_16324946992753vSLEv0P2s-1PY95ynOZn?imageMogr2/thumbnail/640x/interlace/1" alt="Image 2">
            <img src="https://image.meiye.art/pic_1628403749737?imageMogr2/thumbnail/640x/interlace/1" alt="Image 3">
        </div>

        <!-- 左右按钮 -->
        <button class="carousel-button prev">&lt;</button>
        <button class="carousel-button next">&gt;</button>

        <!-- 指示器 -->
        <div class="carousel-indicators">
            <span class="active"></span>
            <span></span>
            <span></span>
        </div>
    </div>

    <script>
        // 获取元素
        const carouselImages = document.querySelector('.carousel-images');
        const prevButton = document.querySelector('.carousel-button.prev');
        const nextButton = document.querySelector('.carousel-button.next');
        const indicators = document.querySelectorAll('.carousel-indicators span');

        let currentIndex = 0; // 当前显示的图片索引
        const totalImages = carouselImages.children.length; // 图片总数

        // 更新指示器状态
        function updateIndicators() {
            indicators.forEach((indicator, index) => {
                indicator.classList.toggle('active', index === currentIndex);
            });
        }

        // 切换到指定图片
        function goToImage(index) {
            if (index < 0) {
                index = totalImages - 1; // 如果小于0,切换到最后一张
            } else if (index >= totalImages) {
                index = 0; // 如果超出范围,切换到第一张
            }
            currentIndex = index;
            carouselImages.style.transform = `translateX(-${currentIndex * 100}%)`;
            updateIndicators();
        }

        // 切换到上一张图片
        prevButton.addEventListener('click', () => {
            goToImage(currentIndex - 1);
        });

        // 切换到下一张图片
        nextButton.addEventListener('click', () => {
            goToImage(currentIndex + 1);
        });

        // 点击指示器切换图片
        indicators.forEach((indicator, index) => {
            indicator.addEventListener('click', () => {
                goToImage(index);
            });
        });

        // 自动播放
        let autoPlayInterval = setInterval(() => {
            goToImage(currentIndex + 1);
        }, 3000);

        // 鼠标悬停时停止自动播放
        const carousel = document.querySelector('.carousel');
        carousel.addEventListener('mouseenter', () => {
            clearInterval(autoPlayInterval);
        });

        // 鼠标离开时恢复自动播放
        carousel.addEventListener('mouseleave', () => {
            autoPlayInterval = setInterval(() => {
                goToImage(currentIndex + 1);
            }, 3000);
        });
    </script>
</body>
</html>


http://www.kler.cn/a/573357.html

相关文章:

  • [项目]基于FreeRTOS的STM32四轴飞行器: 二.项目搭建及移植FreeRTOS
  • 在 Apache Tomcat 中,部署和删除项目
  • 物联网感知层常用感应设备
  • Milvus安装linux操作步骤
  • 初识Qt · Qt的基本认识和基本项目代码解释
  • 【含文档+PPT+源码】基于SpringBoot+Vue的个性化健身助手系统
  • 【极客时间】浏览器工作原理与实践-2 宏观视角下的浏览器- 2.1 Chrome架构:仅仅打开了1个页面,为什么有4个进程?
  • MySQL数据迁移——实战锻炼
  • 高频 SQL 50 题(基础版)_1667. 修复表中的名字
  • QT-绘画事件
  • Leetcode 1477. 找两个和为目标值且不重叠的子数组 前缀和+DP
  • pnpm add和pnpm install指定包名安装的区别
  • 使用Python和Transformer模型进行土壤水热模拟与预测的详细步骤和示例代码
  • fastjson1.2.24 CVE-2017-18349 漏洞复现
  • 前端基础之组件
  • Flutter系列教程之(9)——Flutter调用Android原生
  • 深入理解 React.js:构建现代 Web 应用的核心技术
  • 图解MOE大模型的7个核心问题并探讨DeepSeekMoE的专家机制创新
  • 如何计算卷积神经网络每一层的参数数量和特征图大小?
  • 算法学习新姿势:从0开始用hello-algo搭建自己的在线学习平台