nav.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Keyboard Navigation in Menu - jQuery EasyUI Demo</title>
  6. <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
  7. <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
  8. <link rel="stylesheet" type="text/css" href="../demo.css">
  9. <script type="text/javascript" src="../../jquery.min.js"></script>
  10. <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
  11. </head>
  12. <body>
  13. <h2>Keyboard Navigation in Menu</h2>
  14. <p>Press Alt+W to focus the menu. Once the menu get focus, you will be able to navigate menu using keyboard keys.</p>
  15. <div style="margin:20px 0;"></div>
  16. <div class="easyui-panel" title="Menu" style="width:150px;">
  17. <div id="mm-nav" data-options="inline:true" style="width:100%">
  18. <div onclick="javascript:alert('new')">New</div>
  19. <div>
  20. <span>Open</span>
  21. <div style="width:150px;">
  22. <div><b>Word</b></div>
  23. <div>Excel</div>
  24. <div>PowerPoint</div>
  25. <div>
  26. <span>M1</span>
  27. <div style="width:120px;">
  28. <div>sub1</div>
  29. <div>sub2</div>
  30. <div>
  31. <span>Sub</span>
  32. <div style="width:80px;">
  33. <div>sub21</div>
  34. <div>sub22</div>
  35. <div>sub23</div>
  36. </div>
  37. </div>
  38. <div>sub3</div>
  39. </div>
  40. </div>
  41. <div>
  42. <span>Window Demos</span>
  43. <div style="width:120px;">
  44. <div>Window</div>
  45. <div>Dialog</div>
  46. <div>EasyUI</div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. <div data-options="iconCls:'icon-save'">Save</div>
  52. <div data-options="iconCls:'icon-print',disabled:true">Print</div>
  53. <div class="menu-sep"></div>
  54. <div>Exit</div>
  55. </div>
  56. </div>
  57. <script type="text/javascript">
  58. (function($){
  59. function getParentMenu(rootMenu, menu){
  60. return findParent(rootMenu);
  61. function findParent(pmenu){
  62. var p = undefined;
  63. $(pmenu).find('.menu-item').each(function(){
  64. if (!p && this.submenu){
  65. if ($(this.submenu)[0] == $(menu)[0]){
  66. p = pmenu;
  67. } else {
  68. p = findParent(this.submenu);
  69. }
  70. }
  71. });
  72. return p;
  73. }
  74. }
  75. function getParentItem(pmenu, menu){
  76. var item = undefined;
  77. $(pmenu).find('.menu-item').each(function(){
  78. if ($(this.submenu)[0] == $(menu)[0]){
  79. item = $(this);
  80. return false;
  81. }
  82. });
  83. return item;
  84. }
  85. $.extend($.fn.menu.methods, {
  86. enableNav: function(jq, rootMenu){
  87. var firstItemSelector = '.menu-item:not(.menu-item-disabled):first';
  88. var lastItemSelector = '.menu-item:not(.menu-item-disabled):last';
  89. return jq.each(function(){
  90. var menu = $(this);
  91. rootMenu = $(rootMenu).length ? $(rootMenu) : menu;
  92. menu.attr('tabindex','0').css('outline','none').unbind('.menunav').bind('keydown.menunav', function(e){
  93. var item = $(this).find('.menu-active');
  94. switch(e.keyCode){
  95. case 13: // enter
  96. item.trigger('click');
  97. break;
  98. case 27: // esc
  99. rootMenu.find('.menu-active').trigger('mouseleave');
  100. break;
  101. case 38: // up
  102. var prev = item.length ? item.prevAll(firstItemSelector) : menu.find(lastItemSelector);
  103. prev.trigger('mouseenter');
  104. return false;
  105. case 40: // down
  106. var next = item.length ? item.nextAll(firstItemSelector) : menu.find(firstItemSelector);
  107. next.trigger('mouseenter');
  108. return false;
  109. case 37: // left
  110. var pmenu = getParentMenu(rootMenu, menu);
  111. if (pmenu){
  112. item.trigger('mouseleave');
  113. var pitem = getParentItem(pmenu, menu);
  114. if (pitem){
  115. pitem.trigger('mouseenter');
  116. }
  117. pmenu.focus();
  118. }
  119. return false;
  120. case 39: // right
  121. if (item.length && item[0].submenu){
  122. $(item[0].submenu).menu('enableNav', rootMenu).find(firstItemSelector).trigger('mouseenter');
  123. $(item[0].submenu).focus();
  124. }
  125. return false;
  126. }
  127. });
  128. });
  129. }
  130. });
  131. })(jQuery);
  132. $(function(){
  133. $('#mm-nav').menu().menu('enableNav');
  134. $(document).keydown(function(e){
  135. if (e.altKey && e.keyCode == 87){
  136. $('#mm-nav').focus();
  137. }
  138. })
  139. })
  140. </script>
  141. </body>
  142. </html>