用html写一个简单的轮播图
时间: 2023-05-25 08:03:33 浏览: 111
<!DOCTYPE html>
<html>
<head>
<title>轮播图</title>
<style type="text/css">
.slideshow {
width: 600px;
height: 400px;
position: relative;
margin: auto;
overflow: hidden;
box-shadow: 0 0 10px rgba(0,0,0,0.7);
}
.slide {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.slide:first-child {
display: block;
}
.prev,
.next {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 32px;
font-weight: bold;
text-decoration: none;
color: #fff;
background-color: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 50%;
cursor: pointer;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
</style>
</head>
<body>
<div class="slideshow">
<img class="slide" src="https://via.placeholder.com/600x400.png?text=Slide+1" alt="Slide 1">
<img class="slide" src="https://via.placeholder.com/600x400.png?text=Slide+2" alt="Slide 2">
<img class="slide" src="https://via.placeholder.com/600x400.png?text=Slide+3" alt="Slide 3">
<a class="prev" onclick="prevSlide()">❮</a>
<a class="next" onclick="nextSlide()">❯</a>
</div>
<script type="text/javascript">
var slideIndex = 1;
showSlide(slideIndex);
function prevSlide() {
showSlide(slideIndex -= 1);
}
function nextSlide() {
showSlide(slideIndex += 1);
}
function showSlide(n) {
var slides = document.getElementsByClassName("slide");
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
</script>
</body>
</html>
阅读全文