我需要正确控制滑块图像与底部点分页。但我在代码中遗漏了一些东西或做错了。
您可以在此处查看工作演示 - https://playcode.io/2070692
整个代码是-
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Slideshow with JS</title>
<link
rel="stylesheet"
href="app.css"
/>
</head>
<body>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 4</div>
<img
src="img/img_nature_wide.jpg"
style="width: 100%"
/>
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 4</div>
<img
src="img/img_snow_wide.jpg"
style="width: 100%"
/>
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 4</div>
<img
src="img/img_mountains_wide.jpg"
style="width: 100%"
/>
<div class="text">Caption Three</div>
</div>
<div class="mySlides fade">
<div class="numbertext">4/4</div>
<img
src="img/img_lights_wide.jpg"
style="width: 100%"
/>
<div class="text">Caption Four</div>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>
<br />
<div style="text-align: center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
<script src="app.js"></script>
</body>
</html>
CSS:
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.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;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.prev, .next,.text {font-size: 11px}
}
JS:
const showSlides = (n) => {
let i;
let slides = document.getElementsByClassName('mySlides');
let 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';
};
showSlides(slideIndex);
document.querySelector('.prev').addEventListener('click', () => {
console.log('clicked');
showSlides((slideIndex += -1));
});
document.querySelector('.next').addEventListener('click', () => {
console.log('clicked');
showSlides((slideIndex += 1));
});
const currentSlide = (n) => {
showSlides((slideIndex = n));
};
const dots = document.querySelectorAll('.dot');
dots.forEach((dot) => {
dot.addEventListener('click', () => {
console.log(`${slideIndex}`);
currentSlide(slideIndex + 1);
});
});
现在我想使用点分页控制幻灯片,您也可以查看演示,但它没有按预期工作。我怀疑JS代码的问题出在这部分。
const dots = document.querySelectorAll('.dot');
dots.forEach((dot) => {
dot.addEventListener('click', () => {
console.log(`${slideIndex}`);
currentSlide(slideIndex + 1);
});
});
假设您的问题是,当您单击这些点时,图像会像单击错误时一样继续:
问题是,在处理点上的点击时,您将下一张幻灯片设置为 currentSlide + 1,因此正是执行下一个按钮的操作
相反,您必须将其设置为点的索引:
dots.forEach((dot, index) => {
dot.addEventListener('click', () => {
console.log(`${index}`);
currentSlide(index + 1);
});
});
请注意,将索引设置为各个点 html 元素的属性,然后将它们用作索引可能会更清晰