Customize browser default right-click menu bar

This article briefly introduces how to replace the browser's default right-click menu bar

Right-click anywhere on the page, and the customized menu will be displayed following the click position of the mouse

Renderings:

 

Not much to say, just go to the code!

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>加强自定义菜单</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
 
        ul {
            width: 230px;
            background-color: rgb(255, 255, 255);
            position: fixed;
            left: 100px;
            top: 100px;
            border: 1px solid rgb(105, 105, 105);
            padding-top: 10px;
            text-indent: 10px;
            list-style: none;
            display: none;
        }
 
        li{
            margin-bottom: 13px;
            color: #000;
            font: 800 16px/26px "宋体";
            border-bottom: 1px solid #ccc;
        }
        li:last-child{
            border-bottom: none;
            margin-bottom: 0;
            padding-bottom: 5px;
        }
 
 
        li:hover{
            background-color: rgb(69, 255, 193);
            box-shadow: 0px 0px 26px 0px rgb(69, 255, 193);
        }
 
        ul li:first-child:hover{
            background-color: rgb(91, 230, 255);
            box-shadow: 0px 0px 26px 0px rgb(91, 230, 255);
        }
    </style>
</head>
 
<body>
    <!-- 2. 加强自定义菜单【√】
    ·单击鼠标右键的时候弹出自定义菜单(修身,齐家,治国,平天下)
    ·菜单项有点击时log一下菜单名称,之后菜单消失
    ·鼠标滑过哪个菜单项哪个菜单项高亮 -->
    <ul>
        <li id="Refresh">刷新(F5)</li>
        <li>修身(s)</li>
        <li>齐家(q)</li>
        <li>治国(z)</li>
        <li>平天下(p)</li>
    </ul>
 
    <script>
        var ul = document.querySelector("ul")
        var lis = document.querySelectorAll("li")
        var F5 = document.getElementById("Refresh")
        // var Rclick = document.oncontextmenu()
 
        // 阻止浏览器的右键弹出列表的默认行为
        document.addEventListener(
            "contextmenu",
            function(e){
                e.preventDefault()
                ul.style.display = "block"
                ul.style.left = e.pageX + "px"
                ul.style.top = e.pageY + "px"
            }
        )
 
        // 实现点击li菜单项时log一下菜单名称,及鼠标滑过哪个菜单项哪个菜单项高亮
        ul.addEventListener(
            "click",
            function(e){
                if(e.target.nodeName === "LI"){
                    console.log(e.target.innerText);
                    ul.style.display = "none"
                }
            }
        )
 
        // 实现刷新按钮
        F5.addEventListener(
            "click",
            function(e){
                window.location.reload()
            }
        )
 
        // 单击页面的任意位置关闭ul
        document.addEventListener(
            "click",
            (e)=>{
                ul.style.display = "none"
            }
        )
    </script>
</body>
 
</html>

Guess you like

Origin blog.csdn.net/love906897406/article/details/125978888