I'm currently learning JavaScript and I've put together this script from different sources to help me grasp the basics. I prefer to keep it simple and would appreciate explanations if it gets too complex. Don't assume I know too much about it yet...
<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%">
The script is designed to create a basic image gallery that fits the screen width. You can navigate through the images in the array using the arrow keys. It works well for me, but I'm facing an issue where it doesn't loop back to the first or last image when reaching the end of the array. I'm still trying to figure it out with my limited knowledge. Any suggestions on how to tweak this script with minimal modifications?
I'm just experimenting and not aiming for a professional or polished look. Maybe I'll pursue web design in the future, but for now, I'm just having fun with this. Oh, and please refrain from using JQuery references. I want to understand the solution at a basic level and have a self-contained script.