你会怎么写轮播图HTML5的代码
时间: 2023-03-27 18:05:12 浏览: 82
html+css+js写轮播图
我可以为您提供一个基本的轮播图HTML5代码,您可以根据需要进行修改和优化:
```
<!DOCTYPE html>
<html>
<head>
<title>轮播图</title>
<style>
.container {
width: 600px;
height: 400px;
position: relative;
margin: auto;
overflow: hidden;
}
.container img {
width: 100%;
height: 100%;
position: absolute;
top: ;
left: ;
opacity: ;
transition: opacity 1s ease-in-out;
}
.container img.active {
opacity: 1;
}
.controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
z-index: 1;
}
.controls button {
width: 10px;
height: 10px;
border-radius: 50%;
border: none;
margin: 10px;
background-color: #fff;
cursor: pointer;
opacity: .5;
transition: opacity .3s ease-in-out;
}
.controls button.active {
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<img src="img1.jpg" class="active">
<img src="img2.jpg">
<img src="img3.jpg">
<img src="img4.jpg">
<img src="img5.jpg">
<div class="controls">
<button class="active"></button>
<button></button>
<button></button>
<button></button>
<button></button>
</div>
</div>
<script>
var images = document.querySelectorAll('.container img');
var buttons = document.querySelectorAll('.controls button');
var index = ;
function changeImage() {
images[index].classList.remove('active');
buttons[index].classList.remove('active');
index = (index + 1) % images.length;
images[index].classList.add('active');
buttons[index].classList.add('active');
}
var intervalId = setInterval(changeImage, 300);
for (var i = ; i < buttons.length; i++) {
buttons[i].addEventListener('click', function() {
clearInterval(intervalId);
images[index].classList.remove('active');
buttons[index].classList.remove('active');
index = Array.from(buttons).indexOf(this);
images[index].classList.add('active');
buttons[index].classList.add('active');
intervalId = setInterval(changeImage, 300);
});
}
</script>
</body>
</html>
```
阅读全文