如何在 Web UI 中使用 Javascript 键盘事件
我对使用 JS 事件感兴趣,例如右\左箭头、空格键等。 想知道什么是正确的方法。我想在此类用户交互中滑动照片、使 DIV 出现等。
我想到了 jQuery 的可能实现,例如 http://api.jquery.com/event.which/ 虽然我不确定这是最好的方法。
以下是此类 UI 实现的两个很好的示例 (他们是如何做到的?) http://www.thesixtyone.com ;
http://www.pictorymag.com/showcases/summer-jobless
我是更多的是一个 C# 人,正在寻找正确的方向来深入研究。
I'm interested in using JS events such as right\left arrow, space bar etc.
Wondering what's the right way to do it. I would like to slide photos, make DIVs appear etc. on such user interactions.
I thought of possible implementations with jQuery such as http://api.jquery.com/event.which/
Though I'm not sure that's the best way.
Here are two good examples for such UI implementations
(How do they do it?)
http://www.thesixtyone.com ;
http://www.pictorymag.com/showcases/summer-jobless
I'm more a C# person, looking for the right direction to dig in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已经在使用或计划使用 jQuery,那么利用
which
并不是一个坏主意。如果你看一下这个例子,它就会告诉你具体如何做。将光标放在此处的文本字段中,然后按您感兴趣的键,即向左箭头。keydown:
中显示的数字是您要查找的代码。现在,当您编写自己的函数时,您可以将
keydown
绑定到document
并监听正确的按键。从那里,您可以根据按下的键来调度某些行为。您询问 Pictorymag.com 等网站如何处理此功能。这是 Pictorymag.com 所做的(我已经解压了他们的代码以使其更易于阅读):
If you're already using jQuery or plan to, taking advantage of
which
is not a bad idea. If you look at the example, it shows you exactly how to do it. Put your cursor in the text field there and press the key that you're interested in, i.e. left arrow. The number that is displayed inkeydown:
is the code you're looking for.Now, when you write your own function, you might bind
keydown
to thedocument
and listen for the correct keys. From there, you can dispatch certain behaviors depending on which key was pressed.You asked how sites like Pictorymag.com handle this functionality. Here's what Pictorymag.com does (I've decompressed their code to make it easier to read):