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/4/16 12:25:29

30岁转行AI大模型,刚好赶上风口!非常详细收藏我这一篇就够了

引言 “30岁&#xff0c;人生过半&#xff0c;转行还来得及吗&#xff1f;”这是很多人在职业瓶颈期的自我怀疑。但我想告诉你&#xff0c;30岁转行AI大模型&#xff0c;不仅来得及&#xff0c;还刚好赶上了风口&#xff01; 我是如何从一个传统行业的从业者&#xff0c;成功转…

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

Ryuko-NEHT Reloaded! MAME 0.116 游戏合集

Ryuko-NEHT Reloaded! MAME 0.116 游戏合集 站在巨人的肩上&#xff0c;走的更远。 镜像/应用大全&#xff0c;欢迎访问 一键模型下载&#xff0c;推理&#xff0c;微调&#xff0c;合并工具。 # 一锤定音&#xff08;大模型工具&#xff09; ms-swift 是魔搭社区提供的大模型…

作者头像 李华
网站建设 2026/4/16 12:55:26

MAME Ryuko-NEHT Reloaded 0.116 游戏列表与ROM信息

MAME Ryuko-NEHT Reloaded 0.116 游戏列表与ROM信息 站在巨人的肩上&#xff0c;走的更远。 镜像/应用大全&#xff0c;欢迎访问 一键模型下载&#xff0c;推理&#xff0c;微调&#xff0c;合并工具。 # 一锤定音&#xff08;大模型工具&#xff09; 一个脚本&#xff0c;支…

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

Ultralytics YOLOv8 使用指南与实战

Ultralytics YOLOv8 使用指南与实战 在智能安防、自动驾驶和工业质检等场景中&#xff0c;实时目标检测已成为不可或缺的技术。然而&#xff0c;构建一个高效、稳定且易于部署的视觉系统往往面临诸多挑战&#xff1a;环境配置复杂、依赖冲突频发、推理性能难以优化……有没有一…

作者头像 李华
网站建设 2026/4/16 12:52:22

利用CVE-2017-8890漏洞ROOT天猫魔屏A1

利用CVE-2017-8890漏洞ROOT天猫魔屏A1 本来对阿里的东西挺有好感的&#xff0c;天猫魔屏这玩意儿买回来还能当投影仪看个电影&#xff0c;结果越用越不对劲——系统封闭不说&#xff0c;连个ADB都没有。想装点第三方软件&#xff1f;门都没有。 后来偶然翻到一个老帖子说可以…

作者头像 李华
网站建设 2026/4/14 14:41:04

Node.js实现JavaScript后端化处理

Node.js实现JavaScript后端化处理 在一次项目讨论中&#xff0c;同事突然问我&#xff1a;“有没有可能让服务端直接跑前端那套JS图像处理逻辑&#xff1f;”我笑了笑说&#xff1a;“不仅可能&#xff0c;我们还能用Node.js把整个AI修复流程自动化——比如一键给老照片上色。”…

作者头像 李华