帮我生成一个5张图片轮播的网页源码
时间: 2023-03-21 14:00:27 浏览: 95
以下是一个简单的5张图片轮播的网页源码示例,你可以根据需要进行修改和定制。
```html
<!DOCTYPE html>
<html>
<head>
<title>图片轮播</title>
<style type="text/css">
#slideshow {
position: relative;
width: 800px;
height: 400px;
margin: auto;
}
#slideshow img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 1s ease-in-out;
}
#slideshow img.active {
opacity: 1;
}
</style>
</head>
<body>
<div id="slideshow">
<img src="img1.jpg" class="active">
<img src="img2.jpg">
<img src="img3.jpg">
<img src="img4.jpg">
<img src="img5.jpg">
</div>
<script type="text/javascript">
var images = document.querySelectorAll('#slideshow img');
var current = 0;
var slideInterval = setInterval(nextSlide, 5000);
function nextSlide() {
images[current].classList.remove('active');
current = (current + 1) % images.length;
images[current].classList.add('active');
}
</script>
</body>
</html>
```
说明:
1. 在`<head>`标签中添加了一些CSS样式来控制轮播效果,其中`#slideshow`表示轮播容器,`#slideshow img`表示轮播图片,`.active`表示当前活动的图片。
2. 在`<body>`标签中添加了一个`<div>`元素作为轮播容器,并在其中插入了5张图片,第一张图片默认为活动状态(即`.active`类)。
3. 在`<script>`标签中编写了JavaScript代码来控制轮播效果,其中`images`是一个存储所有轮播图片的数组,`current`是当前活动图片的索引,`slideInterval`是定时器,`nextSlide`是轮播函数,它将当前活动图片的`.active`类移除,将索引加1,并将下一张图片的`.active`类添加上去。最后,通过`setInterval`函数每隔5秒钟调用一次`nextSlide`函数实现自动轮播。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)