How to scroll page elements with the keyboard?

前端 未结 3 1319
不知归路
不知归路 2021-01-07 04:37

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 t

3条回答
  •  無奈伤痛
    2021-01-07 05:03

    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);
        }
    };
    

提交回复
热议问题