Home > Web Front-end > JS Tutorial > body text

How to implement right-click menu effect using javascript

小云云
Release: 2018-03-15 16:46:21
Original
1477 people have browsed it

I suddenly saw the contextmenu attribute when I was looking at js advanced programming recently. I have encountered it before when doing projects. It is used to prevent the right mouse button from popping up (ps: prohibit others from copying). I suddenly thought about it and tried it today. This attribute is used to achieve the following right-click menu effects.

How to implement right-click menu effect using javascript

How to implement right-click menu effect using javascript##

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/page/index.css">
    <title>javascript实现右键菜单效果</title>
    <style>
        *{padding:0;margin:0;}
        .menu{display:none;position:fixed;width:125px;}
        .menu ul li{            height:30px;            width:120px;            margin-top:-1px ;            line-height:30px;            border:1px solid #eee; 
            text-align:center;            list-style:none;        }
        .menu ul li:hover{            background:#ccc;            cursor:default;            user-select: none;        }
    </style></head><body>
    <p class="menu">
       <ul>
           <li>导航菜单1</li>
           <li>导航菜单2</li>
           <li>导航菜单3</li>
           <li>导航菜单4</li>
       </ul>
    </p></body></html><script>

    function $(selector){
        return document.querySelectorAll(selector);
    }    var li = $(".menu ul li");    var menu = $(".menu")[0];    //右键菜单单击
    document.oncontextmenu = function(event){
        var ev = event || window.event;        var mX = event.clientX;        var mY = event.clientY;
        menu.style.display = "block";
        menu.style.left = mX + "px";
        menu.style.top = mY + "px";        return false;   //取消window自带的菜单弹出来
    }    //点击页面菜单消失
    document.onclick = function(){
        menu.style.display = "none";
    }    //阻止点击li冒泡
    for(var i = 0, len = li.length; i < len; i++ ){
        li.item(i).onclick = function(event){
            var ev = event || window.event;
            console.log(this.innerText);            if(ev.stopPropagation()){
                ev.stopPropagation();
            }else{
                ev.cancelBubble = false;
            }
        }
    }</script>
Copy after login

Related recommendations:


Customized right-click menu is implemented using JS Simple example sharing

js to implement the right-click menu function

Implementing the right-click menu and drag-and-drop function based on JavaScript

The above is the detailed content of How to implement right-click menu effect using javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template