包含自动轮播、点击切换、显示图片信息和页码方框显示码数的 HTML 和 JavaScript 示例:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        #carousel-container {
            position: relative;
            width: 80%;
            margin: auto;
            overflow: hidden;
        }

        #carousel {
            display: flex;
            transition: transform 0.5s ease-in-out;
        }

        .carousel-item {
            min-width: 100%;
            box-sizing: border-box;
            position: relative;
        }

        .carousel-item img {
            width: 100%;
            height: auto;
        }

        .carousel-item p {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            background: rgba(0, 0, 0, 0.7);
            color: #fff;
            padding: 10px;
            margin: 0;
            font-size: 14px;
        }

        #prev, #next {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            font-size: 24px;
            color: #333;
            background-color: #fff;
            border: 1px solid #ccc;
            padding: 8px;
            border-radius: 50%;
        }

        #prev { left: 10px; }
        #next { right: 10px; }

        #page-indicator {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .page-dot {
            width: 20px;
            height: 20px;
            background-color: #ccc;
            border-radius: 50%;
            margin: 0 5px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            color: #fff;
        }

        .active-dot {
            background-color: #333;
        }
    </style>
</head>
<body>

<div id="carousel-container">
    <div id="carousel">
        <div class="carousel-item">
            <img src="image1.jpg" alt="Image 1">
            <p>Image 1 Description</p>
        </div>
        <div class="carousel-item">
            <img src="image2.jpg" alt="Image 2">
            <p>Image 2 Description</p>
        </div>
        <div class="carousel-item">
            <img src="image3.jpg" alt="Image 3">
            <p>Image 3 Description</p>
        </div>
        <!-- Add more images as needed -->
    </div>
    <div id="prev">&lt;</div>
    <div id="next">&gt;</div>
    <div id="page-indicator"></div>
</div>

<script>
    var carousel = document.getElementById('carousel');
    var prevButton = document.getElementById('prev');
    var nextButton = document.getElementById('next');
    var pageIndicator = document.getElementById('page-indicator');
    var currentIndex = 0;

    // 图片信息数组
    var imageInfo = [
        "Code 1",
        "Code 2",
        "Code 3"
        // Add more descriptions as needed
    ];

    // 添加页码方框
    for (var i = 0; i < carousel.children.length; i++) {
        var dot = document.createElement('div');
        dot.className = 'page-dot';
        dot.setAttribute('data-index', i);
        dot.textContent = i + 1;
        dot.addEventListener('click', function() {
            clearInterval(autoSlide);
            showSlide(parseInt(this.getAttribute('data-index')));
        });
        pageIndicator.appendChild(dot);
    }

    var dots = document.querySelectorAll('.page-dot');

    // 自动轮播
    var autoSlide = setInterval(function() {
        showSlide(currentIndex + 1);
    }, 3000); // 切换间隔为3秒

    function showSlide(index) {
        currentIndex = (index + carousel.children.length) % carousel.children.length;
        var translateValue = -currentIndex * 100 + '%';
        carousel.style.transform = 'translateX(' + translateValue + ')';

        // 更新页码方框
        dots.forEach(function(dot, i) {
            dot.classList.toggle('active-dot', i === currentIndex);
        });

        // 显示图片信息
        alert(imageInfo[currentIndex]);
    }

    // 点击切换
    prevButton.addEventListener('click', function() {
        clearInterval(autoSlide);
        showSlide(currentIndex - 1);
    });

    nextButton.addEventListener('click', function() {
        clearInterval(autoSlide);
        showSlide(currentIndex + 1);
    });
</script>

</body>
</html>

实现

相关推荐
racerun2 分钟前
vue VueResource & axios
前端·javascript·vue.js
J总裁的小芒果18 分钟前
THREE.js 入门(六) 纹理、uv坐标
开发语言·javascript·uv
m0_5485147719 分钟前
前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
java·前端·javascript
浮游本尊27 分钟前
Nginx配置:如何在一个域名下运行两个网站
前端·javascript
新中地GIS开发老师35 分钟前
《Vue进阶教程》(12)ref的实现详细教程
前端·javascript·vue.js·arcgis·前端框架·地理信息科学·地信
PieroPc1 小时前
Python tkinter写的《电脑装配单》和 Html版 可打印 可导出 excel 文件
python·html·电脑
Cachel wood1 小时前
Django REST framework (DRF)中的api_view和APIView权限控制
javascript·vue.js·后端·python·ui·django·前端框架
幽络源小助理2 小时前
Python使用requests_html库爬取掌阅书籍(附完整源码及使用说明)
python·html·python爬虫·爬虫教程·requests_html·爬取书籍·掌阅
前端Hardy2 小时前
HTML&CSS:惊!3D 折叠按钮
css·3d·html
放逐者-保持本心,方可放逐2 小时前
SSE 流式场景应用 及 方案总结
javascript·axios·fetch·eventsource