news 2026/1/24 2:03:10

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/1/15 21:49:36

使用wechatiOS工具批量下载公众号文章

使用VoxCPM-1.5-TTS与wechatiOS构建公众号语音化流水线 在信息过载的今天&#xff0c;越来越多的人开始尝试“听”内容而非“读”内容——通勤路上、健身时刻、睡前放松&#xff0c;音频成了更友好的知识载体。而微信公众号作为中文世界最丰富的内容池之一&#xff0c;却长期停…

作者头像 李华
网站建设 2026/1/20 15:09:07

OpenStack中img镜像多实例创建与日志分析

Sonic数字人语音同步技术实战&#xff1a;从音频到视频的生成全解析 你有没有想过&#xff0c;只需要一张静态照片和一段录音&#xff0c;就能让画面中的人物“活”起来&#xff0c;开口说话&#xff1f;这不再是科幻电影里的桥景。如今&#xff0c;借助像 Sonic 这样的前沿AI模…

作者头像 李华
网站建设 2026/1/18 15:26:25

OpenCV调用YOLO3实现GPU加速检测

OpenCV调用YOLO3实现GPU加速检测 在部署目标检测模型到生产环境时&#xff0c;很多开发者都遇到过这样的困惑&#xff1a;明明代码里设置了CUDA后端和目标设备&#xff0c;为什么推理速度还是跟CPU差不多&#xff1f;这背后其实藏着一个被广泛忽视的关键点——OpenCV是否真正支…

作者头像 李华
网站建设 2025/12/31 12:46:46

PPT中3D模型功能详解与实战应用

PPT中3D模型功能详解与实战应用 在一场产品发布会上&#xff0c;主讲人轻轻一挥手&#xff0c;幻灯片中的智能手表模型便缓缓旋转&#xff0c;镜头推进到表冠细节&#xff0c;随即弹出一段拆解动画——所有操作无需切换软件&#xff0c;全部由 PowerPoint 实时完成。这不是科幻…

作者头像 李华
网站建设 2026/1/23 6:00:43

平面电磁波在介质中的传播与波动方程推导

平面电磁波在介质中的传播与波动方程推导 当人们谈论无线信号穿透墙壁、光在光纤中传输&#xff0c;或雷达探测远距离目标时&#xff0c;其背后统一的物理图景正是——电磁波在介质中的传播。这一现象的数学根基&#xff0c;并非来自某种经验公式&#xff0c;而是深植于一百多年…

作者头像 李华
网站建设 2026/1/11 2:35:48

TensorFlow实现VGG16猫狗识别实战

基于 TensorFlow 2.9 实现猫狗分类&#xff1a;VGG16 模型的完整训练实践 在深度学习的实际项目中&#xff0c;图像分类往往是入门与进阶的必经之路。而“猫狗大战”——即从照片中识别出是猫还是狗——这个看似简单的问题&#xff0c;实则涵盖了数据加载、预处理、模型构建、训…

作者头像 李华