javaweb 实现轮播图,装在div盒子里

时间: 2023-07-13 13:39:41 浏览: 54
以下是一个基于JavaWeb的简单轮播图实现,同时装在了一个div盒子里: 1. HTML文件: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaWeb轮播图</title> <style type="text/css"> .carousel { width: 800px; height: 600px; margin: 0 auto; position: relative; overflow: hidden; border: 1px solid #ccc; } .carousel img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; } .carousel img.active { display: block; } .carousel .previous-button, .carousel .next-button { position: absolute; top: 50%; transform: translateY(-50%); background: none; border: none; font-size: 24px; color: #fff; text-shadow: 1px 1px 1px #000; cursor: pointer; z-index: 1; } .carousel .previous-button { left: 0; } .carousel .next-button { right: 0; } </style> </head> <body> <div class="carousel"> <img src="images/1.jpg" class="active"> <img src="images/2.jpg"> <img src="images/3.jpg"> <button class="previous-button">&#10094;</button> <button class="next-button">&#10095;</button> </div> <script type="text/javascript"> var carousel = document.querySelector('.carousel'); var images = carousel.querySelectorAll('img'); var previousButton = carousel.querySelector('.previous-button'); var nextButton = carousel.querySelector('.next-button'); var currentIndex = 0; previousButton.addEventListener('click', function() { images[currentIndex].classList.remove('active'); currentIndex--; if (currentIndex < 0) { currentIndex = images.length - 1; } images[currentIndex].classList.add('active'); }); nextButton.addEventListener('click', function() { images[currentIndex].classList.remove('active'); currentIndex++; if (currentIndex >= images.length) { currentIndex = 0; } images[currentIndex].classList.add('active'); }); var timer = setInterval(function() { images[currentIndex].classList.remove('active'); currentIndex++; if (currentIndex >= images.length) { currentIndex = 0; } images[currentIndex].classList.add('active'); }, 3000); carousel.addEventListener('mouseover', function() { clearInterval(timer); }); carousel.addEventListener('mouseout', function() { timer = setInterval(function() { images[currentIndex].classList.remove('active'); currentIndex++; if (currentIndex >= images.length) { currentIndex = 0; } images[currentIndex].classList.add('active'); }, 3000); }); </script> </body> </html> ``` 2. CSS文件: ```css .carousel { width: 800px; height: 600px; margin: 0 auto; position: relative; overflow: hidden; border: 1px solid #ccc; } .carousel img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; } .carousel img.active { display: block; } .carousel .previous-button, .carousel .next-button { position: absolute; top: 50%; transform: translateY(-50%); background: none; border: none; font-size: 24px; color: #fff; text-shadow: 1px 1px 1px #000; cursor: pointer; z-index: 1; } .carousel .previous-button { left: 0; } .carousel .next-button { right: 0; } ``` 这个实现使用了HTML、CSS和JavaScript来实现轮播图,并通过CSS将轮播图装在了一个div盒子里。

相关推荐

最新推荐

recommend-type

JavaWeb实现简单查询商品功能

主要为大家详细介绍了JavaWeb实现简单查询商品功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

JavaWeb 中Cookie实现记住密码的功能示例

cookie是一种WEB服务器通过浏览器在访问者的硬盘上存储信息的手段。Cookie的目的就是为用户带来方便,为网站带来增值。这篇文章主要介绍了JavaWeb 中Cookie实现记住密码的功能示例,需要的朋友可以参考下
recommend-type

JavaWeb后台购物车类实现代码详解

主要介绍了JavaWeb后台购物车类实现代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

javaweb在线支付功能实现代码

主要为大家详细介绍了javaweb在线支付功能的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

eclipse的web项目实现Javaweb购物车的方法

主要介绍了eclipse的web项目实现Javaweb购物车的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。