簡體   English   中英

javascript幻燈片的遞歸函數不起作用

[英]javascript slideshow's recursive function not working

我不知道為什么 if 語句不起作用。

    <script>
    var slideIndex = 0;
function slideShow(){
    var images= document.getElementsByClassName('slider-box')[0].getElementsByTagName('img');

    images[slideIndex].classList.remove('show-slide');
    if(slideIndex > images.length-1)
        slideIndex = 0;

    slideIndex++;
    images[slideIndex].classList.add('show-slide');

    setInterval(slideShow,2000);
}
      slideShow();
</script>

我有 3 張 HTML 圖像。

變量 slideIndex 上升到 3 並保持在 3。

您不應該使用classList,因為它沒有得到正確支持:

 var slider = document.getElementById("slider"); var sliderIndex = 0; function slide() { for (var i = 0; i < slider.children.length; i++) { var cls = slider.children[i].getAttribute("class").replace(/.show/ig, ''); slider.children[i].setAttribute("class", cls); } slider.children[sliderIndex].setAttribute( "class", slider.children[sliderIndex].getAttribute("class") + " show" ); sliderIndex++; if (sliderIndex > slider.children.length - 1) { sliderIndex = 0; } } slide(); setInterval(slide, 3000);
 #slider {} #slider .slide { position: absolute; -moz-transition: all 1s linear 0s; -o-transition: all 1s linear 0s; -webkit-transition: all 1s linear 0s; transition: all 1s linear 0s; opacity: 0 !important; filter: alpha(opacity=0) !important; } #slider .slide.show { opacity: 1 !important; filter: alpha(opacity=100) !important; }
 <div id="slider"> <img class="slide" src="http://placehold.it/310x150/E8117F/000000?text=1" /> <img class="slide" src="http://placehold.it/310x150/7812E5/000000?text=2" /> <img class="slide" src="http://placehold.it/310x150/128AE5/000000?text=3" /> <img class="slide" src="http://placehold.it/310x150/12E594/000000?text=4" /> <img class="slide" src="http://placehold.it/310x150/E5B412/000000?text=5" /> </div>

嘗試這個:

<script>
var slideIndex = 0;
function slideShow(){
    var images= document.getElementsByClassName('slider-box')[0].getElementsByTagName('img');

    images[slideIndex].classList.remove('show-slide');

    slideIndex++;
    if(slideIndex > images.length) slideIndex = 0;
    images[slideIndex].classList.add('show-slide');

    setInterval(slideShow,2000);
}
slideShow();
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM