• 17.JavaScript实现鼠标右键自定义菜单


    JavaScript实现鼠标右键自定义菜单:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .area1 {
                 200px;
                height: 200px;
                border: 1px solid #ccc;
                float: left;
                background: #008c8c
            }
    
            .area2 {
                 200px;
                height: 200px;
                border: 1px solid #ccc;
                float: right;
                background: #008c8c
            }
    
            .contain-text {
                position: fixed;
                left: 300px;
                top: 300px;
                border: 1px solid #ccc;
                background:#fff;
                display: none;
                padding:10px;
                text-align:left;
            }
    
            .contain-text ul {
                list-style: none;
                margin:0;
                padding:0;
            }
        </style>
    </head>
    
    <body>
        <div class="area1">
        </div>
        <div class="area2">
        </div>
    
        <div class="contain-text">
            <ul>
                <li>菜单1</li>
                <li>菜单2</li>
                <li>菜单3</li>
                <li>菜单4</li>
                <li>菜单5</li>
                <li>菜单6</li>
                <li>菜单7</li>
                <li>菜单8</li>
                <li>菜单9</li>
                <li>菜单10</li>
            </ul>
        </div>
        <script>
            var div1 = document.querySelector(".area1");
            var div2 = document.querySelector(".area2");
            var menu = document.querySelector(".contain-text");//菜单
            //注册自定义鼠标右键菜单事件
            div1.oncontextmenu = div2.oncontextmenu = function (e) {
                e.preventDefault();
                menu.style.display = "block";
                console.log(e.clientX, e.clientY);
                menu.style.left = e.clientX + "px";
                menu.style.top = e.clientY + "px";
            }
    
            window.onmousedown = function(){
                menu.style.display = "none";
            }
        </script>
    </body>
    
    </html>
    index.html

    效果展示:

  • 相关阅读:
    KCF目标跟踪方法分析与总结
    C# 事件
    委托学习(3)
    委托学习(2)
    委托学习(1)
    本地无sqlserver服务下操作数据库 之GSQL
    unity Android 打包后读取 xml 文件
    Unity 3D 调用摄像头捕获照片 录像
    Unity 进度条3D制作(3D版)
    Unity 3D 进度条制作
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/12794116.html
Copyright © 2020-2023  润新知