简体   繁体   English

图像数组和箭头键

[英]Image Array and Arrow Keys

I'm just trying to get a hang of javascript, and this is a script I assembled from various sources, just simple enough so I can understand on the level it is. 我只想尝试一下javascript,这是一个我从各种来源组装的脚本,只是简单到足以让我能理解它的水平。 Please don't get too complex, though if you do I would appreciate small explanation and don't expect me to know... 请不要太复杂,但如果你这样做,我会很感激小解释,不要指望我知道......

<script type="text/javascript" language="JavaScript">
imgs=Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg");
var x=0;
document.onkeydown = checkKey;
function checkKey(e) {
    e = e || window.event;
    if (e.keyCode == '37') {
        document.getElementById("myImage").src=imgs[--x];
    }
    else if (e.keyCode == '39') {
        document.getElementById("myImage").src=imgs[++x];
    }
}
</script>

and in the body... 在身体里......

<img id="myImage" src="01.jpg" style="width:100%">

What it's supposed to do is make a simple image gallery of images that fit to the screen width, which can be navigated from the images in the array using arrow keys. 它应该做的是制作一个适合屏幕宽度的图像的简单图像库,可以使用箭头键从数组中的图像导航。 It works fine, for me at least, except the fact that if I try to go to the previous image while x=0, or the next image from x=4, it extends indefinitely, not looping back to 01.jpg from 05.jpg and vice versa as I want. 它至少对我来说很好,除了如果我试图在x = 0时转到上一个图像,或者从x = 4转到下一个图像,它会无限延伸,而不是从05循环回到01.jpg。 jpg,反之亦然。 My theory didn't work. 我的理论不起作用。 I just know bits and pieces about this. 我对此知之甚少。 Anybody know an easy way to with the least modification to this original script? 对这个原始脚本的修改最少,有人知道一个简单的方法吗?

It doesn't have to look professional or 'pretty'. 它不必看起来很专业或“漂亮”。 I'm just bored, and experimenting around. 我只是觉得无聊,并且在试验。 Maybe future aspirations to be a web designer, but not for now certainly :D Oh, and please no JQuery referencing? 也许未来成为网页设计师的愿望,但现在肯定不是:D哦,请不要JQuery引用? Sure they work, but I just want to understand a solution to this on a basic level, and get a standalone script. 当然它们有效,但我只想在基本层面上理解这个解决方案,并获得一个独立的脚本。

I think this should do the trick: 我认为这应该可以解决问题:

<script type="text/javascript" language="JavaScript">
imgs=Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg");
var number_of_images = imgs.length
var x=0;
document.onkeydown = checkKey;
function checkKey(e) {
    e = e || window.event;
    if (e.keyCode == '37') {
        if (x == 0) 
            x = number_of_images - 1;
        else
            x--;
    }
    else if (e.keyCode == '39') {
        if (x == (number_of_images - 1))
            x = 0;
        else
            x++;
    }
    document.getElementById("myImage").src=imgs[x];
}
</script>

Check if x == 0 or x == imgs.length - 1 in your code and adjust accordingly: 检查代码中是否x == 0x == imgs.length - 1并进行相应调整:

function checkKey(e) {
    e = e || window.event;
    if (e.keyCode == '37') {
        if (x == 0) x = imgs.length
        document.getElementById("myImage").src=imgs[--x];
    }
    else if (e.keyCode == '39') {
        if (x == imgs.length - 1) x = -1
        document.getElementById("myImage").src=imgs[++x];
    }
}

So decrementing ( x-- ) in the first case will put you at the last index, and incrementing ( ++x ) in the second case will put you at 0 , aka the first index. 因此,在第一种情况下递减( x-- )将使您处于最后一个索引,而在第二种情况下递增( ++x )将使您处于0 ,即第一个索引。

<script type="text/javascript" language="JavaScript">
imgs=Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg");
console.log(imgs.length);
var x=0;
document.onkeydown = checkKey;
function checkKey(e) {
    e = e || window.event;
    if (e.keyCode == '37') {        
        x--;
        if(x == -1){ x = imgs.length - 1; console.log('a max'); }
        //document.getElementById("myImage").src=imgs[x];
        console.log('a '+x);

    }
    else if (e.keyCode == '39') {       
        x++;
        if(x == imgs.length){ x = 0; console.log('b max'); }
        //document.getElementById("myImage").src=imgs[x];
        console.log('b '+x);     
    }
}
</script>

SAME THING ONLY SMALLER (1 line) 相同的东西只有更小(1行)

 i=Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg");var x=0;document.onkeydown=checkKey;function checkKey(e){e=e||window.event;if(e.keyCode=='37'){x--;if(x==-1){x=i.length-1;}document.getElementById("myImage").src=i[x];}else if(e.keyCode=='39'){x++;if(x==i.length){x=0;}document.getElementById("myImage").src=i[x];}}

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

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