How can i make a keypress action to this code?

Amanci Gabriel

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>

Rosh Donniet

To detect key press using JQuery, you can bind a function to the keydownevent :

$(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.

edited at
0

Comments

0 comments
Login to comment

Related

Can I bind action to a keypress in Emacs?

How can I make my code diagnostic syntax node action work on closed files?

How can I make the code take action when swiping to page x on ViewPager2 in Android?

How can I make a textbox listening to a Keypress-Event and an InputBinding together

How can I simulate a keypress in JavaScript?

how can i mute an iframe on keypress

How I would make the php vaild code execute before action?

How can I change the delay until a held keypress begins to repeat in VS Code?

how can I make the following code better?

How can I make this JavaScript code cleaner?

How can I make branchless code?

How can I make this a prepared Statements code?

How can I make this code more elegant?

How can I make this Java Code Faster?

How can I make this code if and else?

How can I make this code work properly?

How can I make this code tail recursive?

How can I make this code faster in Python?

How can I make this code shorter? Phyphon

How can I make this code concurrency safe?

How can I make a continuous action process smoothly on a device? (GCD)

How can I make interval action with flag in scala cats

How can I make a custom progress bar in the action bar?

How can I make an action be reusable? ReactJS with Redux

How can I make IB create an action connection?

How can I make Floating Action Button rotate on every click

How can I make action bar tabs center align in Android?

How can I make URL action work from a vue component

How Can I set Action bar button icon by Java code