根据以下代码,您可以帮助使用多个幻灯片吗?如果页面上只有一个幻灯片,它可以正常工作,在其他情况下,我必须多次复制代码并为元素提供不同的ID。有没有解决方案可以解决这个问题而无需复制代码?
以下是w3schools的代码:
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";
}
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="img_mountains_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
答案 0 :(得分:0)
如果您想在多个幻灯片中使用相同的代码,可以向showSlides
函数添加参数
var slideIndex = 1;
showSlides(slideIndex,"mySlides","dots");
function plusSlides(n,nSliders,nDots) {
showSlides(slideIndex += n,nSliders,nDots);
}
function currentSlide(n,nSliders,nDots) {
showSlides(slideIndex = n,nSliders,nDots);
}
function showSlides(n,nSliders,nDots) {
var i;
var slides = document.getElementsByClassName(String(nSliders));
var dots = document.getElementsByClassName(nDots);
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";
}
像这样你只需要在html文件中改变函数的参数
HTML:
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="img_mountains_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="prev" onclick="plusSlides(-1,'mySlides','dots')">❮</a>
<a class="next" onclick="plusSlides(1,'mySlides','dots')">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1,'mySlides','dots')"></span>
<span class="dot" onclick="currentSlide(2,'mySlides','dots')"></span>
<span class="dot" onclick="currentSlide(3,'mySlides','dots')"></span>
</div>