1 回答
TA贡献1811条经验 获得超5个赞
首先,您需要将 id 添加到您的容器中,并向您的函数添加其他参数,以便它们可以知道哪些滑块需要更改:
<div class="slideshow-container" id="first">
<!-- Full-width images with number and caption text -->
<div class="mySlides fade">
<img src="/images/1.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/images/2.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/images/3.jpg">
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1, 'first', 'firstSliderIndex')">❮</a>
<a class="next" onclick="plusSlides(1, 'first', 'firstSliderIndex')">❯</a>
<br />
<!-- The dots/circles -->
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1, 'first', 'firstSliderIndex')"></span>
<span class="dot" onclick="currentSlide(2, 'first', 'firstSliderIndex')"></span>
<span class="dot" onclick="currentSlide(3, 'first', 'firstSliderIndex')"></span>
</div>
</div>
你的脚本:
<script>
var indexes = {
firstSliderIndex: 1,
secondSliderIndex: 2,
};
showSlides(indexes.firstSliderIndex, 'first', 'firstSliderIndex');
// Next/previous controls
function plusSlides(n, id, index) { // n - number of slide, id - container id, index - current slide number in slider
showSlides(indexes[index] += n, id, index);
}
// Thumbnail image controls
function currentSlide(n, id, index) {
showSlides(indexes[index] = n, id, index);
}
function showSlides(n, id, index) {
var i;
var slides = document.querySelectorAll(`#${id} .mySlides`);
var dots = document.querySelectorAll(`#${id} .dot`);
if (n > slides.length) {
indexes[index] = 1;
}
if (n < 1) {indexes[index] = 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[indexes[index]-1].style.display = "block";
dots[indexes[index]-1].className += " active";
}
</script>
当然,这段代码可以重构或修改,但这是最简单的方法
- 1 回答
- 0 关注
- 61 浏览
添加回答
举报