Advertisement
nGenchev

right_clickMenu

Dec 25th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. Was very useful for me. For the sake of people like me, expecting the drawing of menu, I put here the code I used to make the right-click menu:
  2.  
  3. HTML: contextmenu.html
  4.  
  5. <!doctype html>
  6. <html>
  7. <head>
  8. <!-- jQuery should be at least version 1.7 -->
  9. <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  10. <script src="contextmenu.js"></script>
  11. <link rel="stylesheet" href="contextmenu.css" />
  12. </head>
  13.  
  14. <body>
  15. <div id="test1">
  16. <a href="www.google.com" class="test">Google</a>
  17. <a href="www.google.com" class="test">Link 2</a>
  18. <a href="www.google.com" class="test">Link 3</a>
  19. <a href="www.google.com" class="test">Link 4</a>
  20. </div>
  21.  
  22. <!-- initially hidden right-click menu -->
  23. <div class="hide" id="rmenu">
  24. <ul>
  25. <li>
  26. <a href="http://www.google.com">Google</a>
  27. </li>
  28.  
  29. <li>
  30. <a href="http://localhost:8080/login">Localhost</a>
  31. </li>
  32.  
  33. <li>
  34. <a href="C:\">C</a>
  35. </li>
  36. </ul>
  37. </div>
  38. </body>
  39. </html>
  40. CSS: contextmenu.css
  41.  
  42. .show {
  43. z-index:1000;
  44. position: absolute;
  45. background-color:#C0C0C0;
  46. border: 1px solid blue;
  47. padding: 2px;
  48. display: block;
  49. margin: 0;
  50. list-style-type: none;
  51. list-style: none;
  52. }
  53.  
  54. .hide {
  55. display: none;
  56. }
  57.  
  58. .show li{ list-style: none; }
  59. .show a { border: 0 !important; text-decoration: none; }
  60. .show a:hover { text-decoration: underline !important; }
  61. JS: contextmenu.js - used from the accepted answer
  62.  
  63. $(document).ready(function() {
  64.  
  65.  
  66. if ($("#test").addEventListener) {
  67. $("#test").addEventListener('contextmenu', function(e) {
  68. alert("You've tried to open context menu"); //here you draw your own menu
  69. e.preventDefault();
  70. }, false);
  71. } else {
  72.  
  73. //document.getElementById("test").attachEvent('oncontextmenu', function() {
  74. //$(".test").bind('contextmenu', function() {
  75. $('body').on('contextmenu', 'a.test', function() {
  76.  
  77.  
  78. //alert("contextmenu"+event);
  79. document.getElementById("rmenu").className = "show";
  80. document.getElementById("rmenu").style.top = mouseY(event) + 'px';
  81. document.getElementById("rmenu").style.left = mouseX(event) + 'px';
  82.  
  83. window.event.returnValue = false;
  84.  
  85.  
  86. });
  87. }
  88.  
  89. });
  90.  
  91. // this is from another SO post...
  92. $(document).bind("click", function(event) {
  93. document.getElementById("rmenu").className = "hide";
  94. });
  95.  
  96.  
  97.  
  98. function mouseX(evt) {
  99. if (evt.pageX) {
  100. return evt.pageX;
  101. } else if (evt.clientX) {
  102. return evt.clientX + (document.documentElement.scrollLeft ?
  103. document.documentElement.scrollLeft :
  104. document.body.scrollLeft);
  105. } else {
  106. return null;
  107. }
  108. }
  109.  
  110. function mouseY(evt) {
  111. if (evt.pageY) {
  112. return evt.pageY;
  113. } else if (evt.clientY) {
  114. return evt.clientY + (document.documentElement.scrollTop ?
  115. document.documentElement.scrollTop :
  116. document.body.scrollTop);
  117. } else {
  118. return null;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement