如何在 Web UI 中使用 Javascript 键盘事件

发布于 2024-10-01 10:49:18 字数 547 浏览 5 评论 0原文

我对使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-10-08 10:49:18

如果您已经在使用或计划使用 jQuery,那么利用 which 并不是一个坏主意。如果你看一下这个例子,它就会告诉你具体如何做。将光标放在此处的文本字段中,然后按您感兴趣的键,即向左箭头。 keydown: 中显示的数字是您要查找的代码。

现在,当您编写自己的函数时,您可以将 keydown 绑定到 document 并监听正确的按键。从那里,您可以根据按下的键来调度某些行为。

$(document).bind('keydown', function (e) {
    var code = e.which;
    switch (code) {
        case 39:
            // code to execute when right arrow is pressed
            some_right_arrow_action();
            break;
        case 37:
            // code to execute when left arrow is pressed
            some_left_arrow_action();
            break;
    }
    return;
});

您询问 Pictorymag.com 等网站如何处理此功能。这是 Pictorymag.com 所做的(我已经解压了他们的代码以使其更易于阅读):

$(document).keydown(function (e) {
    switch(e.keyCode) {
        case 39:
        case 74:
            node = s[++curr];
            if(node) {
                $.scrollTo(node,800);
            } else {
                curr = s.length-1;
            }
            break;
        case 37:
        case 75:
            node = s[--curr];
            if (node) {
                $.scrollTo(node, 800);
            } else {
                curr=0;
            }
            break;
    }
});

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 in keydown: is the code you're looking for.

Now, when you write your own function, you might bind keydown to the document and listen for the correct keys. From there, you can dispatch certain behaviors depending on which key was pressed.

$(document).bind('keydown', function (e) {
    var code = e.which;
    switch (code) {
        case 39:
            // code to execute when right arrow is pressed
            some_right_arrow_action();
            break;
        case 37:
            // code to execute when left arrow is pressed
            some_left_arrow_action();
            break;
    }
    return;
});

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):

$(document).keydown(function (e) {
    switch(e.keyCode) {
        case 39:
        case 74:
            node = s[++curr];
            if(node) {
                $.scrollTo(node,800);
            } else {
                curr = s.length-1;
            }
            break;
        case 37:
        case 75:
            node = s[--curr];
            if (node) {
                $.scrollTo(node, 800);
            } else {
                curr=0;
            }
            break;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文