news 2026/4/15 15:01:05

134-136 完成轮播图界面,完成点击按钮切换图片,完成轮播图

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
134-136 完成轮播图界面,完成点击按钮切换图片,完成轮播图

完成轮播图界面,完成点击按钮切换图片,完成轮播图

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>4张图片轮播图</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } /* 轮播图容器 */ .carousel-container { position: relative; width: 450px; height: 450px; margin: 50px auto; overflow: hidden; border-radius: 10px; box-shadow: 0 0 20px rgba(0,0,0,0.2); } /* 图片轮播列表 */ .carousel-list { display: flex; width: 400%; /* 4张图片,所以宽度为400% */ height: 100%; transition: transform 0.5s ease; } .carousel-item { width: 100%; height: 100%; } .carousel-item img { width: 100%; height: 100%; object-fit: cover; } /* 左右切换按钮 */ .carousel-btn { position: absolute; top: 50%; transform: translateY(-50%); width: 40px; height: 40px; background-color: rgba(255,255,255,0.7); border: none; border-radius: 50%; font-size: 20px; cursor: pointer; display: flex; align-items: center; justify-content: center; z-index: 10; transition: background-color 0.3s; } .carousel-btn:hover { background-color: rgba(255,255,255,1); } .prev-btn { left: 20px; } .next-btn { right: 20px; } /* 页码指示器 */ .carousel-indicators { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; gap: 10px; } .indicator-btn { width: 12px; height: 12px; border-radius: 50%; border: none; background-color: rgba(255,255,255,0.5); cursor: pointer; transition: background-color 0.3s, transform 0.3s; } .indicator-btn.active { background-color: #ffffff; transform: scale(1.2); } </style> </head> <body> <div class="carousel-container" id="carousel"> <!-- 轮播图片列表 --> <div class="carousel-list" id="carouselList"> <div class="carousel-item"> <img src="./img/1.jpg" alt="轮播图1"> </div> <div class="carousel-item"> <img src="./img/2.jpg" alt="轮播图2"> </div> <div class="carousel-item"> <img src="./img/3.jpg" alt="轮播图3"> </div> <div class="carousel-item"> <img src="./img/4.jpg" alt="轮播图4"> </div> </div> <!-- 左右切换按钮 --> <button class="carousel-btn prev-btn" id="prevBtn">&lt;</button> <button class="carousel-btn next-btn" id="nextBtn">&gt;</button> <!-- 页码指示器 --> <div class="carousel-indicators" id="indicators"></div> </div> <script> // 获取DOM元素 const carousel = document.getElementById('carousel'); const carouselList = document.getElementById('carouselList'); const prevBtn = document.getElementById('prevBtn'); const nextBtn = document.getElementById('nextBtn'); const indicators = document.getElementById('indicators'); // 轮播图配置 const imgCount = 4; // 图片数量 let currentIndex = 0; // 当前显示的图片索引 let autoPlayTimer = null; // 自动轮播定时器 const autoPlayInterval = 3000; // 自动轮播间隔(毫秒) // 生成页码指示器按钮 function createIndicators() { for (let i = 0; i < imgCount; i++) { const btn = document.createElement('button'); btn.className = 'indicator-btn'; if (i === 0) btn.classList.add('active'); // 初始第一个按钮激活 btn.dataset.index = i; // 存储索引数据 // 点击页码按钮切换图片 btn.addEventListener('click', () => { currentIndex = i; updateCarousel(); }); indicators.appendChild(btn); } } // 更新轮播图显示和页码状态 function updateCarousel() { // 计算偏移量,移动图片列表 const offset = -currentIndex * (100 / imgCount); carouselList.style.transform = `translateX(${offset}%)`; // 更新页码按钮激活状态 const indicatorBtns = document.querySelectorAll('.indicator-btn'); indicatorBtns.forEach((btn, index) => { if (index === currentIndex) { btn.classList.add('active'); } else { btn.classList.remove('active'); } }); } // 上一张 function prevImg() { currentIndex = (currentIndex - 1 + imgCount) % imgCount; updateCarousel(); } // 下一张 function nextImg() { currentIndex = (currentIndex + 1) % imgCount; updateCarousel(); } // 启动自动轮播 function startAutoPlay() { autoPlayTimer = setInterval(nextImg, autoPlayInterval); } // 停止自动轮播 function stopAutoPlay() { clearInterval(autoPlayTimer); } // 初始化 function initCarousel() { createIndicators(); // 创建页码按钮 startAutoPlay(); // 启动自动轮播 // 绑定左右按钮事件 prevBtn.addEventListener('click', prevImg); nextBtn.addEventListener('click', nextImg); // 鼠标悬停暂停轮播,离开继续 carousel.addEventListener('mouseenter', stopAutoPlay); carousel.addEventListener('mouseleave', startAutoPlay); } // 页面加载完成后初始化轮播图 window.addEventListener('DOMContentLoaded', initCarousel); </script> </body> </html>

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/31 18:24:59

梯度下降法原理与应用解析

梯度下降法原理与应用解析 在机器学习的世界里&#xff0c;模型训练的本质往往归结为一个核心问题&#xff1a;如何找到一组最优参数&#xff0c;让预测误差最小&#xff1f; 这个过程听起来像是一场在黑暗中摸索的旅程——你不知道全局地形&#xff0c;只能靠脚下的一点坡度信…

作者头像 李华
网站建设 2026/4/1 1:30:10

Java实现GIF动态验证码生成与解析

Java实现GIF动态验证码生成与解析 在如今自动化攻击日益频繁的背景下&#xff0c;传统的静态验证码已经难以抵御高级OCR识别和机器学习破解手段。为了提升系统的安全性&#xff0c;越来越多的应用开始采用动态视觉干扰策略——而其中&#xff0c;GIF格式的动态验证码正以其“人…

作者头像 李华
网站建设 2026/4/9 15:21:17

【Open-AutoGLM部署终极指南】:手把手教你从零完成高效AI模型部署

第一章&#xff1a;Open-AutoGLM部署详细步骤详解环境准备与依赖安装 在部署 Open-AutoGLM 之前&#xff0c;需确保系统已配置 Python 3.9 及 pip 包管理工具。推荐使用虚拟环境以隔离依赖。创建并激活虚拟环境&#xff1a; # 创建虚拟环境 python -m venv open-autoglm-env# 激…

作者头像 李华
网站建设 2026/4/8 9:35:14

Docker安装配置与基础操作指南

Docker 安装配置与基础操作指南 在现代 AI 开发中&#xff0c;环境配置往往是令人头疼的第一道门槛。你是否曾为 PyTorch 版本不兼容、CUDA 驱动错配或依赖包冲突而耗费数小时&#xff1f;Docker 的出现正是为了终结这种“在我机器上能跑”的窘境。 作为一款开源的应用容器引…

作者头像 李华
网站建设 2026/4/12 20:12:57

ComfyUI集成DDColor实现老照片上色修复

ComfyUI集成DDColor实现老照片上色修复 在家庭相册的某个角落&#xff0c;或许你曾翻出一张泛黄的老照片——祖辈的结婚照、儿时的全家福、早已消失的街景。它们承载着记忆&#xff0c;却因时间褪去了色彩&#xff0c;变得模糊而遥远。如果有一种方式&#xff0c;能让这些黑白…

作者头像 李华
网站建设 2026/4/10 13:18:43

解决Keras中multi_gpu_model弃用问题

解决Keras中multi_gpu_model弃用问题 在使用TensorFlow进行深度学习模型训练时&#xff0c;你是否曾遇到这样的报错&#xff1f; AttributeError: module tensorflow.keras.utils has no attribute multi_gpu_model如果你正从旧版Keras代码迁移到现代TensorFlow环境&#xff…

作者头像 李华