简体   繁体   English

图片幻灯片不会滑动

[英]Image slide won't slide

I have programmed an image slider in javascript, and it's "functional," but it doesn't alternate the images except for the very first and last images. 我已经用javascript编程了一个图像滑块,它是“功能性”的,但是除了最前面的图像和最后一个图像之外,它不会替代其他图像。 For example, if I'm on the first image and press the right button, it won't change to the next image in the array. 例如,如果我在第一个图像上并按向右按钮,它将不会更改为数组中的下一个图像。 However, if I were to push the left button, it will change to last image in the array. 但是,如果我按左按钮,它将变为阵列中的最后一个图像。 The same thing will happen for the last image. 最后一张图片也会发生同样的事情。 I don't know how to fix this. 我不知道该如何解决。 Can you help? 你能帮我吗?

I think it might have to do something with the "total" variable, but I'm not sure. 我认为它可能必须与“总计”变量有关,但是我不确定。

Here's the code... 这是代码...

window.onload = function () {
            var nmbr_imgs = 4;
            var imgs_holder = ["IMGS/Actinium.png", "IMGS/Aluminum.png", "IMGS/Astatine.png",  "IMGS/Barium.png"];
            var total = imgs_holder.length;
            var left_btn = document.getElementById('left_btn');
            var right_btn = document.getElementById('right_btn');
            var imgs_display = document.getElementById('imgs_display');

            left_btn.addEventListener('click', function() {
                var lefting = total - 1;
                imgs_display.src = imgs_holder[lefting];
                if (lefting < 0) {
                    imgs_display.src = imgs_holder[(nmbr_imgs - 1)];
                }
            }, false);

            right_btn.addEventListener('click', function() {
                var righting = total + 1;
                imgs_display.src = imgs_holder[righting];
                if (righting > (nmbr_imgs - 1)) {
                    imgs_display.src = imgs_holder[0];
                }
            }, false);
        }

Your listeners are off a bit ... 您的听众有点不高兴...

total = total - 1;
imgs_display.src = imgs_holder[total];
if(lefting < 0) {
  total = nmbr_imgs - 1;
  imgs_display = imgs_holder[total]
}

... try incrementing/decrementing total, not lefting/righting which are reset to total +/- 1 each time. ...尝试递增/递减总计,而不是左/右对齐,每次将其重置为总计+/- 1。

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

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