不要弄清楚计数器在javascript中的作用是什么

时间:2019-04-21 18:11:11

标签: javascript counter development-environment slide

我想在js中创建一个全屏图像滑块,所以我看了一个教程,并且我理解了大部分“计数器”旁边的内容。我知道它代表了当前图像,但我不知道它是怎么做的,如何被分配。

代码在这里

let sliderImages = document.querySelectorAll(".slide"),
    arrowLeft = document.querySelector("#arrow-left"),
    arrowRight = document.querySelector("#arrow-right"),
    current = 0; (!!!!THIS ONE I DON`T UNDERSTAND HOW IT S REPRESENTING THE CURRENT IMAGE!!!

// Clear all images
function reset() {
    for (let i = 0; i < sliderImages.length; i++) {
        sliderImages[i].style.display = "none";
    }
}

// Init slider
function startSlide() {
    reset();
    sliderImages[0].style.display = "block";
}

// Show prev
function slideLeft() {
    reset();
    sliderImages[current - 1].style.display = "block";
    current--;
}
// Left arrow click
arrowLeft.addEventListener("click", function () {
    if (current === 0) {
        current = sliderImages.length;
    }
    slideLeft();
});

// Show next
function slideRight() {
    reset();
    sliderImages[current + 1].style.display = "block";
    current++;
}


// Right arrow click
arrowRight.addEventListener("click", function () {
    if (current === sliderImages.length - 1) {
        current = -1;
    }
    slideRight();
});

startSlide();

!这是补习https://youtu.be/7ZO2RTMNSAY的链接

1 个答案:

答案 0 :(得分:1)

当前用于包含所有带有滑动类名称的元素的数组的索引。

阅读有关数组的更多信息可能对您有益:https://www.w3schools.com/js/js_arrays.asp