I am new to JavaScript and jQuery so here is my question. How can i add to this code a simple key press action. Like if i press Right Arrow, go to the next image. and if i press the left arrow, go to the previous image. I've looked it up and tried something myself but i couldn't integrate anything into this particular code.
Now, i could simply use another code and use a lightbox gallery or something, but i don't want that because i've alredy got somewhere with the website and i can't make it all over again.
function showImage(smSrc, lgSrc) {
document.getElementById('largeImg').src = smSrc;
showLargeImagePanel();
unselectAll();
setTimeout(function() {
document.getElementById('largeImg').src = lgSrc;
}, 1)
}
function showLargeImagePanel() {
document.getElementById('largeImgPanel').style.display = 'block';
}
function unselectAll() {
if (document.selection)
document.selection.empty();
if (window.getSelection)
window.getSelection().removeAllRanges();
}
#largeImgPanel {
text-align: center;
display: none;
position: fixed;
z-index: 100;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(100, 100, 100, 0.8);
}
<img src="./images/t_p0001.jpg" style="cursor:pointer" onclick="showImage(this.src, './images/p0001.JPG');" />
<img src="./images/t_p0002.jpg" style="cursor:pointer" onclick="showImage(this.src, './images/p0002.JPG');" />
<img src="./images/t_p0003.jpg" style="cursor:pointer" onclick="showImage(this.src, './images/p0003.JPG');" />
<img src="./images/t_p0004.jpg" style="cursor:pointer" onclick="showImage(this.src, './images/p0004.JPG');" />
<div id="largeImgPanel" onclick="this.style.display='none'">
<img id="largeImg" style="height:100%; margin:0; padding:0;" />
</div>
To detect key press using JQuery, you can bind a function to the keydown
event :
$(document).keydown(function(e) {
switch(e.which) {
case 37: // left
break;
case 38: // up
break;
case 39: // right
break;
case 40: // down
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
(taken from there)
To change the displayed img, what you could do is something along the lines of this :
$("#largeImgPanel").html('<img src="./images/t_p0001.jpg" style="cursor:pointer"');
Then you'd have to implement a system to know which image is being displayed, and use this information to determine the next image to show.
EDIT : as requested, to make it go through the images :
var imgs = ["./images/t_p0001.jpg", "./images/...", etc];
var currentImg = 0;
Then in the keydown event handler, you'd have :
case 37: // left
if (currentImg === 0) {
currentImg = imgs.length - 1;
}
else {
currentImg--;
}
break;
case 39: // right
if (currentImg === imgs.length - 1) {
currentImg = 0;
}
else {
currentImg++;
}
break;
Then you just have to update the src
property of your img
tag (after the switch) :
if (e.which === 37 || e.which === 39) {
$("#largeImgPanel>img").attr("src", imgs[currentImg]);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments