nav.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Keyboard Navigation in MenuButton - 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 MenuButton</h2>
  14. <p>Press Alt+W to focus the menubutton. Once the menubutton get focus, you will be able to navigate menubutton using keyboard keys.</p>
  15. <div style="margin:20px 0;"></div>
  16. <div class="easyui-panel" style="padding:5px;">
  17. <a id="btn-home" href="#" class="easyui-linkbutton" data-options="plain:true">Home</a>
  18. <a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'icon-edit'">Edit</a>
  19. <a href="#" class="easyui-menubutton" data-options="menu:'#mm2',iconCls:'icon-help'">Help</a>
  20. <a href="#" class="easyui-menubutton" data-options="menu:'#mm3'">About</a>
  21. </div>
  22. <div id="mm1" style="width:150px;">
  23. <div data-options="iconCls:'icon-undo'">Undo</div>
  24. <div data-options="iconCls:'icon-redo'">Redo</div>
  25. <div class="menu-sep"></div>
  26. <div>Cut</div>
  27. <div>Copy</div>
  28. <div>Paste</div>
  29. <div class="menu-sep"></div>
  30. <div>
  31. <span>Toolbar</span>
  32. <div>
  33. <div>Address</div>
  34. <div>Link</div>
  35. <div>Navigation Toolbar</div>
  36. <div>Bookmark Toolbar</div>
  37. <div class="menu-sep"></div>
  38. <div>New Toolbar...</div>
  39. </div>
  40. </div>
  41. <div data-options="iconCls:'icon-remove'">Delete</div>
  42. <div>Select All</div>
  43. </div>
  44. <div id="mm2" style="width:100px;">
  45. <div>Help</div>
  46. <div>Update</div>
  47. <div>About</div>
  48. </div>
  49. <div id="mm3" class="menu-content" style="background:#f0f0f0;padding:10px;text-align:left">
  50. <img src="http://www.jeasyui.com/images/logo1.png" style="width:150px;height:50px">
  51. <p style="font-size:14px;color:#444;">Try jQuery EasyUI to build your modern, interactive, javascript applications.</p>
  52. </div>
  53. <script type="text/javascript">
  54. (function($){
  55. function getParentMenu(rootMenu, menu){
  56. return findParent(rootMenu);
  57. function findParent(pmenu){
  58. var p = undefined;
  59. $(pmenu).find('.menu-item').each(function(){
  60. if (!p && this.submenu){
  61. if ($(this.submenu)[0] == $(menu)[0]){
  62. p = pmenu;
  63. } else {
  64. p = findParent(this.submenu);
  65. }
  66. }
  67. });
  68. return p;
  69. }
  70. }
  71. function getParentItem(pmenu, menu){
  72. var item = undefined;
  73. $(pmenu).find('.menu-item').each(function(){
  74. if ($(this.submenu)[0] == $(menu)[0]){
  75. item = $(this);
  76. return false;
  77. }
  78. });
  79. return item;
  80. }
  81. $.extend($.fn.menubutton.methods, {
  82. enableNav: function(enabled){
  83. var curr;
  84. $(document).unbind('.menubutton');
  85. if (enabled == undefined){enabled = true;}
  86. if (enabled){
  87. $(document).bind('keydown.menubutton', function(e){
  88. var currButton = $(this).find('.m-btn-active,.m-btn-plain-active,.l-btn:focus');
  89. if (!currButton.length){
  90. return;
  91. }
  92. if (!curr || curr.button != currButton[0]){
  93. curr = {
  94. menu: currButton.data('menubutton') ? $(currButton.menubutton('options').menu) : $(),
  95. button: currButton[0]
  96. };
  97. }
  98. var item = curr.menu.find('.menu-active');
  99. switch(e.keyCode){
  100. case 13: // enter
  101. item.trigger('click');
  102. break;
  103. case 27: // esc
  104. currButton.trigger('mouseleave');
  105. break;
  106. case 38: // up
  107. var prev = !item.length ? curr.menu.find('.menu-item:last') : item.prevAll('.menu-item:first');
  108. prev.trigger('mouseenter');
  109. return false;
  110. case 40: // down
  111. var next = !item.length ? curr.menu.find('.menu-item:first') : item.nextAll('.menu-item:first');
  112. next.trigger('mouseenter');
  113. return false;
  114. case 37: // left
  115. var pmenu = getParentMenu(currButton.data('menubutton') ? $(currButton.menubutton('options').menu) : $(), curr.menu);
  116. if (pmenu){
  117. item.trigger('mouseleave');
  118. var pitem = getParentItem(pmenu, curr.menu);
  119. if (pitem){
  120. pitem.trigger('mouseenter');
  121. }
  122. curr.menu = pmenu;
  123. } else {
  124. var prev = currButton.prevAll('.l-btn:first');
  125. if (prev.length){
  126. currButton.trigger('mouseleave');
  127. prev.focus();
  128. }
  129. }
  130. return false;
  131. case 39: // right
  132. if (item.length && item[0].submenu){
  133. curr.menu = $(item[0].submenu);
  134. curr.button = currButton[0];
  135. curr.menu.find('.menu-item:first').trigger('mouseenter');
  136. } else {
  137. var next = currButton.nextAll('.l-btn:first');
  138. if (next.length){
  139. currButton.trigger('mouseleave');
  140. next.focus();
  141. }
  142. }
  143. return false;
  144. }
  145. });
  146. }
  147. }
  148. });
  149. })(jQuery);
  150. $(function(){
  151. $.fn.menubutton.methods.enableNav();
  152. $(document).keydown(function(e){
  153. if (e.altKey && e.keyCode == 87){
  154. $('#btn-home').focus();
  155. }
  156. })
  157. });
  158. </script>
  159. </body>
  160. </html>