简体   繁体   English

为什么我的图片库不能正常工作?

[英]Why isn't my image gallery working?

I'm pretty new with javascript and I've (almost) made an image gallery. 我对javascript很陌生,并且(几乎)做了一个图片库。 It includes two buttons (pictures not actuall HTML buttons). 它包括两个按钮(图片不是实际的HTML按钮)。 Anyway, the button that selects the next picture is working fine, though, the one that is supposed to back to the previous picture is working but once you've passed the first picture it jumps to a white (you know when it's just white and a little picture symbol in the middle). 无论如何,选择下一张图片的按钮可以正常工作,但是应该返回到上一张图片的按钮也可以使用,但是一旦您通过了第一张图片,它就会跳到白色(您知道何时只有白色,然后中间有一个小图片符号)。 I have no idea why. 我不知道为什么。

Javascript: 使用Javascript:

<script>
        var myImage= new Array(); 
        myImage[0]="pics/IMG1.jpg";       
        myImage[1]="pics/IMG2.jpg";
        myImage[2]="pics/IMG3.jpg";
        myImage[3]="pics/IMG4.jpg"; 

    var ImageCnt = 0; 

    function next(){
        ImageCnt++;

        if(ImageCnt == 4) { 
            ImageCnt = 0; 

        }
        document.getElementById("img1").src = myImage[ImageCnt];
    }

    function previous () {
        ImageCnt--; 

        if(ImageCnt == 0) {
            ImageCnt = 4;

        }
        document.getElementById("img1").src = myImage[ImageCnt];            
    }
</script>

HTML (only img parts): HTML(仅img部分):

            <div class="img_box">
            <img id="img1" src="pics/img1.jpg" onmousedown="return false;"></img>
            </div>

        <div class="img_switch">
            <a href="#" onclick="previous();return false;"><img class="img_switch_pic" src="pics/previous.png" width="100px"/></a>
            <a href="#" onclick="next();return false;"><img class="img_switch_pic" src="pics/next.png" width="100px"/></a>
        </div>

Thanks in advance! 提前致谢! :) :)

Your next/previous logic is flawed: 您的下一个/上一个逻辑有缺陷:

    if(ImageCnt == 0) {
        ImageCnt = 4;
    }

You've only defined myImage 0->3, so when you reset the ImageCnt to 4, you're looking up an image that doesn't exist. 您仅定义了myImage 0-> 3,因此将ImageCnt重置为4时,您正在查找的图像不存在。 As well, you've hard-coded the expected length of the images array, meaning that if you add more images later, you'll have to modify both functions to accept that new length. 同样,您已经对图像数组的预期长度进行了硬编码,这意味着如果以后添加更多图像,则必须修改这两个函数以接受该新长度。

Try 尝试

prev: 上一篇:

ImageCnt--;
if (ImageCnt < 0) {
   ImageCnt = myImage.length -1;
}

next: 下一个:

ImageCnt++;
if (ImageCnt >= myImage.length) {
   imageCnt = 0;
}

This makes the checks dynamic, and you don't have to do anything except update your myImage array with more/fewer images. 这使检查变得动态,除了用更多/更少的图像更新myImage数组外,您无需执行任何其他操作。

For your previous code, try decreasing the values like this instead: 对于您之前的代码,请尝试减少如下所示的值:

if(ImageCnt == -1) {
ImageCnt = 3;
}

The problem is that your array only goes to 3 (0-3 instead of 1-4) so it is reaching for an image that is not there. 问题在于您的数组仅变为3(0-3而不是1-4),因此它到达的图像不存在。

function previous () { ImageCnt--; 函数previous(){ImageCnt--;

    if(ImageCnt == -1) {
        ImageCnt = 3;

    }
    document.getElementById("img1").src = myImage[ImageCnt];            
}

You just go 0 to 3. Just change ImageCnt = 4 to 3 More over, if you are at index 0, you have to display your image0 您只需要从0到3进行操作即可。只需将ImageCnt = 4更改为3即可,如果您位于索引0,则必须显示image0

So you want to be on imagecnt == -1 to display your 4th image (index = 3) 因此,您想在imagecnt == -1上显示第4张图像(索引= 3)

in your previous method you have an error. 在以前的方法中,您有一个错误。 Should be that way: 应该是这样的:

   if(ImageCnt == -1) {
        ImageCnt = 3;

    }

Because the 4 index does not exist in the Array and 0 position neither! 因为数组4和索引0中都不存在索引!

Hope it helps! 希望能帮助到你!

Cheers! 干杯!

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

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