How to add a custom right-click menu to a webpage?

后端 未结 19 1273
广开言路
广开言路 2020-11-22 15:54

I want to add a custom right-click menu to my web application. Can this be done without using any pre-built libraries? If so, how to display a simple custom right-click menu

相关标签:
19条回答
  • 2020-11-22 16:08

    I use something similar to the following jsfiddle

    function onright(el, cb) {
        //disable right click
        document.body.oncontextmenu = 'return false';
        el.addEventListener('contextmenu', function (e) { e.preventDefault(); return false });
        el.addEventListener('mousedown', function (e) {
            e = e || window.event;
            if (~~(e.button) === 2) {
                if (e.preventDefault) {
                    e.preventDefault();
                } else {
                    e.returnValue = false;
                }
                return false;
            }
        });
    
        // then bind Your cb
        el.addEventListener('mousedown', function (e) {
            e = e || window.event;
            ~~(e.button) === 2 && cb.call(el, e);
        });
    }
    

    if You target older IE browsers you should anyway complete it with the ' attachEvent; case

    0 讨论(0)
  • 2020-11-22 16:12
    <script language="javascript" type="text/javascript">
      document.oncontextmenu = RightMouseDown; 
      document.onmousedown = mouseDown; 
    
      function mouseDown(e) {
        if (e.which==3) {//righClick
          alert("Right-click menu goes here");
        } 
      }
    
      function RightMouseDown() { 
        return false; 
      }
    </script>
    </body> 
    </html>
    
    0 讨论(0)
  • 2020-11-22 16:14

    Tested and works in Opera 12.17, firefox 30, Internet Explorer 9 and chrome 26.0.1410.64

    document.oncontextmenu =function( evt ){
            alert("OK?");
            return false;
            }
    
    0 讨论(0)
  • 2020-11-22 16:15
    <html>
    <head>
    <style>
    .rightclick {
        /* YOUR CONTEXTMENU'S CSS */
        visibility: hidden;
        background-color: white;
        border: 1px solid grey;
        width: 200px;
        height: 300px;
    }
    </style>
    </head>
    <body>
      <div class="rightclick" id="ya">
        <p onclick="alert('choc-a-late')">I like chocolate</p><br><p onclick="awe-so-me">I AM AWESOME</p>
      </div>
      <p>Right click to get sweet results!</p>
    </body>
    <script>
        document.onclick = noClick;
        document.oncontextmenu = rightClick;
        function rightClick(e) {
            e = e || window.event;
            e.preventDefault();
            document.getElementById("ya").style.visibility = "visible";
            console.log("Context Menu v1.3.0 by IamGuest opened.");
       }
    function noClick() {
        document.getElementById("ya").style.visibility = "hidden";
        console.log("Context Menu v1.3.0 by IamGuest closed.");
    }
    </script>
    <!-- Coded by IamGuest. Thank you for using this code! -->
    </html>
    

    You can tweak and modify this code to make a better looking, more efficient contextmenu. As for modifying an existing contextmenu, I'm not sure how to do that... Check out this fiddle for an organized point of view. Also, try clicking the items in my contextmenu. They should alert you a few awesome messages. If they don't work, try something more... complex.

    0 讨论(0)
  • 2020-11-22 16:16

    Simplest jump start function, create a context menu at the cursor position, that destroys itself on mouse leave.

    oncontextmenu = (e) => {
      e.preventDefault()
      let menu = document.createElement("div")
      menu.id = "ctxmenu"
      menu.style = `top:${e.pageY}px;left:${e.pageX}px`
      menu.onmouseleave = function(){ctxmenu.outerHTML=''}
      menu.innerHTML = "<p>Option1</p><p>Option2</p><p>Option3</p><p>Option4</p><p onclick='alert(`Thank you!`)'>Upvote</p>"
      document.body.appendChild(menu)
    }
    #ctxmenu {
      position: fixed;
      background:ghostwhite;
      color: black;
      cursor: pointer;
      border: 1px black solid
    }
    
    #ctxmenu > p {
      padding: 0 1rem;
      margin: 0
    }
    
    #ctxmenu > p:hover {
      background: black;
      color: ghostwhite
    }

    0 讨论(0)
  • 2020-11-22 16:17
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <head>
    
    <title>Context menu - LabLogic.net</title>
    
    </head>
    <body>
    
    <script language="javascript" type="text/javascript">
    
    document.oncontextmenu=RightMouseDown;
    document.onmousedown = mouseDown; 
    
    
    
    function mouseDown(e) {
        if (e.which==3) {//righClick
            alert("Right-click menu goes here");
        }
    }
    
    
    function RightMouseDown() { return false; }
    
    </script>
    
    </body>
    </html>
    

    Tested and works in Opera 11.6, firefox 9.01, Internet Explorer 9 and chrome 17 You can check out a working sample at javascript right click menu

    0 讨论(0)
提交回复
热议问题