帮我写段html 轮播图
时间: 2023-05-29 09:02:35 浏览: 170
<!DOCTYPE html>
<html>
<head>
<title>轮播图</title>
<style type="text/css">
.slider {
width: 500px;
height: 300px;
overflow: hidden;
position: relative;
margin: 0 auto;
background-color: #f5f5f5;
}
.slide {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
transition: all 0.5s ease-in-out;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.navigation {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.navigation button {
width: 10px;
height: 10px;
margin: 0 5px;
padding: 0;
border: none;
border-radius: 50%;
background-color: #aaa;
cursor: pointer;
}
.navigation button.active {
background-color: #333;
}
.prev, .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
border: none;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
cursor: pointer;
font-size: 18px;
}
.prev:hover, .next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.prev {
left: 20px;
}
.next {
right: 20px;
}
</style>
</head>
<body>
<div class="slider">
<div class="slide">
<img src="https://via.placeholder.com/500x300.png?text=Slide+1" alt="Slide 1">
</div>
<div class="slide">
<img src="https://via.placeholder.com/500x300.png?text=Slide+2" alt="Slide 2">
</div>
<div class="slide">
<img src="https://via.placeholder.com/500x300.png?text=Slide+3" alt="Slide 3">
</div>
<button class="prev">❮</button>
<button class="next">❯</button>
<div class="navigation">
<button class="dot active"></button>
<button class="dot"></button>
<button class="dot"></button>
</div>
</div>
<script type="text/javascript">
var slideIndex = 1;
showSlide(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlide(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlide(slideIndex = n);
}
function showSlide(n) {
var i;
var slides = document.getElementsByClassName("slide");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
document.querySelector('.prev').addEventListener('click', function() {
plusSlides(-1);
});
document.querySelector('.next').addEventListener('click', function() {
plusSlides(1);
});
var dots = document.querySelectorAll('.dot');
for (var i = 0; i < dots.length; i++) {
dots[i].addEventListener('click', function() {
var slideNumber = parseInt(this.dataset.slide);
currentSlide(slideNumber);
});
}
</script>
</body>
</html>
注意事项:
1. 需要jQuery库支持;
2. 需要引入轮播图相关css样式;
3. 根据具体需求修改css样式;
4. 需要引入轮播图片,可自行修改图片地址。
阅读全文