news 2026/5/30 15:16:49

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/5/29 14:01:35

AI淘金全攻略:5大热门岗位薪资揭秘+转行技能包,程序员必备收藏指南

文章详细解析了AI行业5大核心岗位(AI产品经理、解决方案专家、应用工程师、算法工程师、数据运营)的职责与薪资&#xff0c;并针对不同背景人士提供转行指南。强调AI产品经理需理解技术边界、数据敏感度及四大工具(Prompt工程、RAG、微调、Function Calling)。指出2025年AIGC岗…

作者头像 李华
网站建设 2026/5/26 20:56:31

doris对于hdfs的异步导入方式

Doris 支持通过 Broker Load 方式实现 HDFS 数据的异步导入&#xff0c;该方式适用于大数据量场景且不阻塞客户端操作。以下是关键步骤和注意事项&#xff1a;1. 核心流程Broker 代理访问通过 Doris 的 Broker 进程访问 HDFS&#xff08;需提前部署 Broker 并配置 HDFS 权限&am…

作者头像 李华
网站建设 2026/5/23 13:10:00

Excel实用技巧大全:从入门到精通

Excel实用技巧大全&#xff1a;从入门到精通 你有没有遇到过这样的场景&#xff1f; 月底报表堆积如山&#xff0c;同事还在手动复制粘贴、反复核对数据&#xff0c;而有人轻点几下鼠标&#xff0c;透视表自动生成汇总结果&#xff1b;别人为身份证号变成“1.1E17”抓耳挠腮时…

作者头像 李华
网站建设 2026/5/20 12:02:15

VS2019下C++调用YOLOv3动态链接库实现目标检测

在 Windows 环境下使用 C 部署 YOLOv3 目标检测&#xff1a;从零搭建高效推理系统 当我们在工业质检、智能监控或嵌入式视觉项目中追求极致的性能与稳定性时&#xff0c;C 往往是比 Python 更合适的选择。尽管 Python 提供了丰富的深度学习生态和快速原型开发能力&#xff0c;…

作者头像 李华
网站建设 2026/5/30 12:17:32

FreeBSD 11.0-RELEASE 发布亮点与升级指南

FreeBSD 11.0-RELEASE 发布亮点与升级指南 在现代操作系统演进的浪潮中&#xff0c;FreeBSD 始终以稳健、安全和高效著称。作为 BSD 家族的核心成员&#xff0c;它不仅承载着类 UNIX 系统的经典设计哲学&#xff0c;也在持续吸收前沿技术&#xff0c;适应从数据中心到嵌入式设…

作者头像 李华