在HTML中使用多个幻灯片时,JavaScript滑块错误
我在HTML网站上有一个JavaScript滑块,代码如下:
let slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides() {
let i;
let slides = document.getElementsByClassName("mySlides");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
slides[slideIndex - 1].style.display = "block";
setTimeout(showSlides, 2000);
}
<div class="col-lg-6 col-md-10 slideshow-container">
<div class="mySlides fades">
<div class="numbertext">1 / 2</div>
<img src="fb1.jpg" alt="Image">
</div>
<div class="mySlides fades">
<div class="numbertext">2 / 2</div>
<img src="fb2.jpg">
</div>
</div>
这很好,现在我在同一页面中添加了另一个滑块:
<div class="col-lg-6 col-md-10 slideshow-container">
<div class="mySlides fades">
<div class="numbertext">1 / 2</div>
<img src="auto1.jpg" alt="Image">
</div>
<div class="mySlides fades">
<div class="numbertext">2 / 2</div>
<img src="auto2.jpg">
</div>
</div>
现在,问题是碰撞的,我认为,由于两者都有相同的JavaScript代码,所以两个幻灯片都在计算幻灯片和空白图像中的总图像,任何人都可以告诉我如何解决此问题,谢谢
i have a javascript slider in my html website, the code is like below:
let slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides() {
let i;
let slides = document.getElementsByClassName("mySlides");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
slides[slideIndex - 1].style.display = "block";
setTimeout(showSlides, 2000);
}
<div class="col-lg-6 col-md-10 slideshow-container">
<div class="mySlides fades">
<div class="numbertext">1 / 2</div>
<img src="fb1.jpg" alt="Image">
</div>
<div class="mySlides fades">
<div class="numbertext">2 / 2</div>
<img src="fb2.jpg">
</div>
</div>
this is working fine, now i have added another slider in the same page:
<div class="col-lg-6 col-md-10 slideshow-container">
<div class="mySlides fades">
<div class="numbertext">1 / 2</div>
<img src="auto1.jpg" alt="Image">
</div>
<div class="mySlides fades">
<div class="numbertext">2 / 2</div>
<img src="auto2.jpg">
</div>
</div>
now the issue is its kind of colliding, i think because both has same javascript code, both the slide is calculating total images in both the slides and blank images are coming in both, can anyone please tell me how to fix this, thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:您调用函数
showlides()
带有param(slideIndex
),因为该函数使用函数使用全局声明的var 。我建议,添加或删除类(例如
.Active
),而不是使用JS进行样式。优势是,您可以通过该类获取函数中的索引,而不是使用全局var:使用该类别,您可以在
slideshow-container
中循环而不会遇到麻烦SlideIndex
。工作示例:
(我删除了功能
plusslides()
andcurrentslide()
,因为它们在该示例中没有使用,至少<代码> plusslides()需要另一个功能,而不是showslides()
)带有更多滑块和幻灯片的示例:
First: You call the function
showSlides()
with a param (slideIndex
) that isn't necessary, because the function uses the global declared var.I recommend, to add or remove a class (for example
.active
) instead of styling with js. The advantage is, that you can get the index in the function via that class instead of using the global var for that:With that in use you can loop through the
slideshow-container
s without getting trouble with theslideIndex
.Working example:
(i removed the functions
plusSlides()
andcurrentSlide()
, because they aren't used in that example and at leastplusSlides()
needs another functionality thanshowSlides()
)Example with more sliders and slides: