<!DOCTYPE html> <html> <hand> <style> .out{ width: 1460px; height: 750px; border:1px solid #BFBFBF; margin: 20px auto; box-shadow:0px 0px 10px 5px #8e8c8c; } h1{ color:black; text-align: center; font-family: "字魂4086号-雅宋漂泊体"; font-size: 75px; } .slideshow-container { max-width: 1000px; position: relative; margin: auto; } .mySlides { display: none; } .prev,.next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } .next { right: 0; border-radius: 3px 0 0 3px; } .prev:hover,.next:hover { background-color: rgba(0, 0, 0, 0.8); } .fade { animation-name: fade; animation-duration: 1.5s; } @keyframes fade { from { opacity: 0.4; } to { opacity: 1; } } </style> <script> var slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides((slideIndex += n)); } function currentSlide(n) { showSlides((slideIndex = n)); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); 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"; } </script> </hand> <body> <div class="out"> <h1>甘肃美食介绍</h1> <div class="slideshow-container"> <div class="mySlides fade"> <img src="1.jpg" style="width:100%"> </div> <div class="mySlides fade"> <img src="2.jpg" style="width:100%"> </div> <div class="mySlides fade"> <img src="3.jpg" style="width:100%"> </div> <div class="mySlides fade"> <img src="4.jpg" style="width:100%"> </div> <div class="mySlides fade"> <img src="5.jpg" style="width:100%"> </div> <div class="mySlides fade"> <img src="6.jpg" style="width:100%"> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> </div> </body> </html>这段代码为什么运行的时候没有轮播图?哪里有问题,正确的代码是怎样的?
时间: 2024-01-03 12:17:54 浏览: 162
这段代码没有轮播图是因为没有添加小圆点导航,可以在每个轮播图下面添加一个小圆点,用于导航。代码有几个问题:
1. `<hand>` 标签不是有效的 HTML 标签,应该改成 `<head>`。
2. 在 CSS 样式中,`hand` 应该改成 `head`。
3. 在 HTML 中,应该添加小圆点导航。可以在 `<div class="slideshow-container">` 下面添加一个 `<div class="dots">`,然后在 JavaScript 中添加代码生成小圆点。
4. 在 JavaScript 中,`slideIndex` 变量应该初始化为 0,因为数组下标从 0 开始。同时,在 `showSlides` 函数中,应该把 `slideIndex - 1` 改为 `slideIndex`。
修改后的代码如下所示:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>甘肃美食介绍</title>
<style>
.out {
width: 1460px;
height: 750px;
border: 1px solid #BFBFBF;
margin: 20px auto;
box-shadow: 0px 0px 10px 5px #8e8c8c;
}
h1 {
color: black;
text-align: center;
font-family: "字魂4086号-雅宋漂泊体";
font-size: 75px;
}
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
}
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {
opacity: 0.4;
}
to {
opacity: 1;
}
}
.dots {
text-align: center;
}
.dot {
display: inline-block;
width: 10px;
height: 10px;
margin: 0 5px;
background-color: #bbb;
border-radius: 50%;
cursor: pointer;
}
.active, .dot:hover {
background-color: #717171;
}
</style>
<script>
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
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";
setTimeout(showSlides, 3000); // 切换时间间隔为 3 秒
}
function plusSlides(n) {
showSlides((slideIndex += n));
}
function currentSlide(n) {
showSlides((slideIndex = n));
}
</script>
</head>
<body>
<div class="out">
<h1>甘肃美食介绍</h1>
<div class="slideshow-container">
<div class="mySlides fade">
<img src="1.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="2.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="3.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="4.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="5.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="6.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div class="dots">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
<span class="dot" onclick="currentSlide(4)"></span>
<span class="dot" onclick="currentSlide(5)"></span>
<span class="dot" onclick="currentSlide(6)"></span>
</div>
</div>
</body>
</html>
```
阅读全文