Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll page elements with the keyboard?

I basically want to achieve the same effect as in Google Reader: when you press "j", you are pushed down to the next article and when you press "k", you can go back up to the previous article. What is the simplest way of doing this?

like image 929
Edvard Avatar asked Jan 19 '26 16:01

Edvard


1 Answers

Using onkeyup and use the keyCode to determine the key pressed: http://jsfiddle.net/pimvdb/gzRwN/1/.

document.body.onkeyup = function(e) {
    var code = e.keyCode;
    if(code === 74) { // key code for j
        window.scrollTo(document.body.scrollLeft,
                        document.body.scrollTop + 500);
    }
};
like image 68
pimvdb Avatar answered Jan 21 '26 07:01

pimvdb