简体   繁体   English

元素的简单 HTML5 键盘导航

[英]Simple HTML5 keyboard navigation for elements

I'm trying to implement some keyboard navigation (arrows left , right & enter/space to select) into my website.我正在尝试在我的网站中实现一些键盘导航(向左向右输入/空格选择)。 I'm guessing some jQuery will be required for this!我猜这需要一些jQuery Here's an idea of what I'm looking for (see first image).这是我在寻找什么的想法(见第一张图片)。 I want to be able to use images where the 'sections' are and have the current selection highlighted.我希望能够使用“部分”所在的图像并突出显示当前选择。

在此处输入图片说明

Is there a simple way of doing this?有没有一种简单的方法可以做到这一点?

Furthermore, if there is a simple way of doing this, how would I expand it to the following:此外,如果有一种简单的方法可以做到这一点,我将如何将其扩展为以下内容:

在此处输入图片说明

jQuery is not my area at all. jQuery 根本不是我的领域。 I've looked everywhere online and I'm finding it extremely hard to find a simple way of implementing keyboard controls into my website.我在网上到处找,我发现很难找到一种在我的网站中实现键盘控件的简单方法。 Any links or suggestions would be greatly appreciated!任何链接或建议将不胜感激!

Thanks in advance.提前致谢。

you can do it, just add the code that you want to the following你可以做到,只需在下面添加你想要的代码

  $(document).keydown(function(e) {
      switch(e.which) {
          case 37: // left
              moveInSectorToLeft();
          break;

         case 38: // up
             nothing(); //Dont know if you want to use it
         break;

         case 39: // right
             moveInSectorToRight();
         break;

         case 40: // down
             selectCurrentSection();
         break;

         default: return; 
     }
     e.preventDefault(); 
  });

you can check an example in this fiddle : https://jsfiddle.net/ew8Lnt83/你可以在这个小提琴中检查一个例子: https : //jsfiddle.net/ew8Lnt83/

You can use the keydown event from jQuery, I wrote a simple fiddle to help you understand how to implement it, I hope it helps:您可以使用 jQuery 的keydown事件,我写了一个简单的小提琴来帮助您了解如何实现它,希望对您有所帮助:

$(function(){
    height = $('#myTable tr').length+1;    
    width = $('#myTable td').length;
    $.each($('#myTable td'), function(key, value){
        $(this).attr('id', key);
    });
});
$(document).keydown(function(e)
{    
   switch(e.which) {        
        case 37: // left
           move(1);
        break;
        case 38: // up
            move(2);
        break;
        case 39: // right
           move(3);
        break;          
        case 40: // down
           move(4);
        break;          
        default: return; // exit this handler for other keys
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
}); 
function move(direction){
    var cur_id = parseInt($('.selected').attr('id'));
    switch(direction){
        case 1://left
           selectTd(cur_id-1);
            break;
        case 2://up
            selectTd(cur_id-height);
            break;
        case 3://right
            selectTd(cur_id+1);
            break;
        case 4://down
             selectTd(cur_id+height);
            break;

    }

}
function selectTd(id){
    if(id > (width-1) || id < 0) return alert("Out of bounds");
   $('.selected').removeClass('selected');  
   $('#'+id).addClass('selected');     
}

https://jsfiddle.net/z6gea722/ https://jsfiddle.net/z6gea722/

You can check a more robust way of keyboard navigation handling with https://github.com/amanboss9/naviboard library.您可以使用https://github.com/amanboss9/naviboard库检查更强大的键盘导航处理方式。 As tab key helps you to navigate through a pattern and lose focus once you are done with the page navigation.由于tab键可帮助您浏览模式并在完成页面导航后失去焦点。 shift + tab helps in reverse navigation but still you have no control over the navigation according to the element overlaying on the screen. shift + tab有助于反向导航,但您仍然无法根据屏幕上覆盖的元素控制导航。

You can go through the demo of this by below jsfiddle: https://jsfiddle.net/amanboss9/unf94tjo/3/embedded/result/您可以通过以下 jsfiddle 进行演示: https ://jsfiddle.net/amanboss9/unf94tjo/3/embedded/result/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM