简体   繁体   English

javascript 上的多个自动幻灯片放映问题

[英]Issue with multiple automatic slideshows on javascript

I'm an absolute newbie on Javascript, but I'm trying to put together three separate automatic slideshows on the same page.我是 Javascript 的绝对新手,但我正在尝试将三个单独的自动幻灯片放在同一页面上。 I've found a couple of helpful links from W3Schools ( https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_auto & https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_rr ), but when using either, I can't include all three slideshows together, or if I repeat the same <script> three times (changing the document.getElementsByClassName ) all of the slideshows seem to collapse.我从 W3Schools ( https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_auto & https://www.w3schools.com/w3css/tryit.asp?filename= tryw3css_slideshow_rr ),但是在使用任一时,我不能将所有三个幻灯片都包含在一起,或者如果我重复相同的<script>三次(更改document.getElementsByClassName )所有幻灯片似乎都崩溃了。 I've also found some options for multiple slideshows, but I have no idea how to make them automatic.我还找到了多个幻灯片的一些选项,但我不知道如何使它们自动。 CSS is no problem, I just can't figure out the combination in the script to make all three slideshows work simultaneously and automatic. CSS 没问题,我只是想不出脚本中的组合来使所有三个幻灯片同时自动工作。

Thanks in advance!提前致谢!

 <script> var slideIndex = 0; showSlides(); function showSlides() { var i; var slides = document.getElementsByClassName("slide1", "slide2", "slide3"); 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, 2000); // Change image every 2 seconds } </script> <script> var myIndex = 0; carousel(); function carousel() { var i; var x = document.getElementsByClassName("slide1"); for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } myIndex++; if (myIndex > x.length) {myIndex = 1} x[myIndex-1].style.display = "block"; setTimeout(carousel, 2000); // Change image every 2 seconds } </script>
 <div id="container1"> <img id="aa" class="slide1" src="id1.png"> <img id="bb" class="slide1" src="id2.png"> <img id="cc" class="slide1" src="id3.png"> </div> <div id="container2"> <img id="dd" class="slide2" src="cr1.png"> <img id="ee" class="slide2" src="cr2.png"> <img id="ff" class="slide2" src="cr3.png"> </div> <div id="container3"> <img id="ab" class="slide3" src="id1.png"> <img id="hh" class="slide3" src="id2.png"> <img id="ii" class="slide3" src="id3.png"> </div>

I had the same problem, I gave each slideshow a different class, then I included a separate function for each class and then I isolated each function by wrapping it in the below我有同样的问题,我给每个幻灯片一个不同的类,然后我为每个类包含一个单独的函数,然后我通过将它包装在下面来隔离每个函数

;(function() { ;(功能() {

JS code JS代码

})() })()

I've edited your code so it should work now :)我已经编辑了你的代码,所以它现在应该可以工作了:)

<div id="container1">
<img id="aa" class="slide1" src="id1.png">
<img id="bb" class="slide1" src="id2.png">
<img id="cc" class="slide1" src="id3.png">
</div>

<div id="container2">
<img id="dd" class="slide2" src="cr1.png">
<img id="ee" class="slide2" src="cr2.png">
<img id="ff" class="slide2" src="cr3.png">
</div>

<div id="container3">
<img id="ab" class="slide3" src="id1.png">
<img id="hh" class="slide3" src="id2.png">
<img id="ii" class="slide3" src="id3.png">
</div>


<script>
;(function() { 
var myIndex = 0;
carousel();

function carousel() {
    var i;
    var x = document.getElementsByClassName("slide1");
    for (i = 0; i < x.length; i++) {
       x[i].style.display = "none";  
    }
    myIndex++;
    if (myIndex > x.length) {myIndex = 1}    
    x[myIndex-1].style.display = "block";  
    setTimeout(carousel, 2000); // Change image every 2 seconds
}
})()

;(function() { 
var myIndex = 0;
carousel();

function carousel() {
    var i;
    var x = document.getElementsByClassName("slide2");
    for (i = 0; i < x.length; i++) {
       x[i].style.display = "none";  
    }
    myIndex++;
    if (myIndex > x.length) {myIndex = 1}    
    x[myIndex-1].style.display = "block";  
    setTimeout(carousel, 2000); // Change image every 2 seconds
}
})()

;(function() { 
var myIndex = 0;
carousel();

function carousel() {
    var i;
    var x = document.getElementsByClassName("slide3");
    for (i = 0; i < x.length; i++) {
       x[i].style.display = "none";  
    }
    myIndex++;
    if (myIndex > x.length) {myIndex = 1}    
    x[myIndex-1].style.display = "block";  
    setTimeout(carousel, 2000); // Change image every 2 seconds
}
})()
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM