如何使用键盘滚动页面元素? [英] How to scroll page elements with the keyboard?

查看:352
本文介绍了如何使用键盘滚动页面元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想要达到与Google阅读器相同的效果:当您按下j时,您会被按下到下一篇文章,当您按下k时,您可以回到上一篇文章。

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?

推荐答案

使用 onkeyup 请使用 keyCode 确定按下的键: http: //jsfiddle.net/pimvdb/gzRwN/1/

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

这篇关于如何使用键盘滚动页面元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆