1

I am building a very simple website with a series of images arranged vertically on a page. I would like to be able to use the up and down arrow keys to smooth scroll from one image to the next, up or down. This way the number of keystrokes required for browsing will be minimized and the unmatched spacing of page down and page up can be avoided.

I imagine that this is going to be a javascript function. However, I am not very experienced using javascript, so I hope you can consider a beginner in the answer. Right now, the images are arranged inside a single div and separated with br (multiple line breaks). So, I imagine it might be possible to place an id in each img or to place identifying tags around each img for the javascript to use. Or, perhaps, there is another way to make the javascript recognize the steps for scrolling via arrow keys. What is the best way to do this? Thank you.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
user981178
  • 1,417
  • 3
  • 12
  • 14
  • You can use jquery .keydown() event to capture arrow keys pressed. Then identify current (this) x,y position() you user is on, then send user to anchor with next picture. Maybe detect which nth child image you are on and send user to nth+1. This is not very typical web behaviour. Maybe take a look at various carriusel plugins. – Mustapha George Dec 30 '11 at 03:11
  • Thanks for the suggestion. I am sorry if this is terribly easy, but since I am inexperienced in javascript, can you perhaps describe in more detail how to do this? Perhaps, with a jsfiddle? And, how would I implement the smooth scroll with it? Thank you. – user981178 Dec 30 '11 at 03:15
  • Not terribly easy first attempt. Why don't you google "jquery keydown arrow keys" and find a example of moving images with arrow keys to get started. Here is one..http://stackoverflow.com/questions/4404852/how-use-keyboard-arrow-keys-to-move-a-div-100px-left-right-respectively. – Mustapha George Dec 30 '11 at 03:23

1 Answers1

1

This is a fun and easy one if you know jQuery. Catch the up/down arrow key presses and prevent their default scroll action. Then find the next or previous image based on the arrow press. Finally use jQuery's animate to bring the top of the element into view. You'll need to put in some error checking for when you go past the end of the list, but I am sure you can figure it out based on this.

http://jsfiddle.net/aVvQF/4/

$(window).keydown(function(e) {
    e.preventDefault(); //prevent default arrow key behavior

    var targetElement;
    //down
    if (e.keyCode == 40) {
        $targetElement = $('.active').next('img');
    }
    //up
    else if (e.keyCode == 38) {
        $targetElement = $('.active').prev('img');
    }
    if (!$targetElement.length) {return;}
    $('.active').removeClass('active');
    $targetElement.addClass('active');

    //scroll element into view    
    $('html, body').clearQueue().animate({scrollTop: $targetElement.offset().top }, 1000);
});
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • Thanks. This is helpful. I see what you mean though about error checking. Playing with the jsfiddle, it appears that if you enter the up arrow key on the first image or the down arrow key on the last image, it does run the selector right off the page and no up or down arrow key functions work (until you refresh). So, I guess it will be necessary to somehow restrict the up arrow key on the first element and the down arrow key on the last element. – user981178 Dec 30 '11 at 03:41
  • I made an update to prevent the boundary problem and also to prevent the animation queue from building up. – mrtsherman Dec 30 '11 at 03:54
  • How might you code to only prevent the default function for up and down arrow keys? Right now, it appears to be preventing default function for all key presses. – user981178 Dec 30 '11 at 04:03
  • I am not sure if it is the best way, but I have changed to code to move the e.preventDefault(); from where it currently is (in the above code) to just above the $targetElement for each keycode, and it seems to allow other keystrokes now. – user981178 Dec 30 '11 at 04:28
  • Hmm, there does seem to still be some issues with how I would like to use this. The effect does not work if there are any other tags between the img tags. So, I cannot add text links between the images or make the images links themselves. Also, page up and page down or (if I change the navigation to left and right instead of up and down arrows) up and down arrows do not work. Only the javascript navigation or scrollbars work. – user981178 Dec 30 '11 at 04:49
  • You should ask these questions in separate SO posts. Most of them are easily solvedbut are dependent on your HTML structure. Some jquery Apis you may need are parent, nextall. – mrtsherman Dec 30 '11 at 05:13