为什么在调用JS函数showSlides(SLIDEINDEX)时我的JS代码没有显示任何其他幻灯片;为什么?即使我更改SLIDEINDEX的值?

时间:2018-07-26 07:02:49

标签: javascript html5 css3

我不知道我的js代码有什么问题,请检查以下内容,并通过更改SLIDEINDEX的值来帮助我显示其他安全幻灯片

    // initialize slideindex with 0 as you want to show the first slide first
var SLIDEINDEX = 3;

showSlides(SLIDEINDEX);

// creating function for showing slides
function showSlides(index){
    // lets select all the slides and dots using querySelectorAll method
    var slides = document.querySelectorAll(".slide");
    var dots = document.querySelectorAll(".dots-navigation");
    // if slide index value is greater than length of slides then jump to 1st slide
    if (index >= slides.length) {SLIDEINDEX = 0;}
    // if we want to jump at the last of the slide incase the user is at the first one
    if (index < 0) {SLIDEINDEX = slides.length - 1;}

    // before showing slides we must hide all the slides and remove active-dots class using for loop
    for (var i = 0; i < slides.length; i++){
        // hide slide elements
        slides[i].style.display = "none";
        // hide dots active-dot class
        dots[i].classList.remove("active-dot");
    }
    // show the slide we want and set the dot class active-dot
    slides[SLIDEINDEX].style.display = "block";
    dots[SLIDEINDEX].classList.add("active-dot");

};

这是其他部分,例如HTML和CSS代码,请点击链接 HTML代码链接:-https://pastebin.com/nUcqnXnj和CSS代码链接:-https://pastebin.com/T2CZPNeh

1 个答案:

答案 0 :(得分:2)

我认为SLIDEINDEX var被错过了。仅当它超出幻灯片限制时才设置它。

...
// if slide index value is greater than length of slides then jump to 1st slide
if (index >= slides.length) {SLIDEINDEX = 0;}
// if we want to jump at the last of the slide incase the user is at the first one
else if (index < 0) {SLIDEINDEX = slides.length - 1;}
// else set SLIDEINDEX with new index
else {SLIDEINDEX = index;}
...