美文网首页让前端飞Web前端之路
案例:自定义的鼠标右击菜单

案例:自定义的鼠标右击菜单

作者: Hello杨先生 | 来源:发表于2019-07-09 16:20 被阅读7次
image.png
<style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            -khtml-user-select: none;
            user-select: none;
        }

        ul {
            text-align: center;
            
        }

        ul li {
            list-style: none;
            /* margin: 10px 0; */
            height: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid black;
            background: white;
           transition:  background  .3s linear;
        }
        ul li:hover {
            background: gray;
        }

        #rightMenu {
            display: none;
           
            position: absolute;
            /* width: 100px; */
            /*  */
        }
    </style>
<body>
    <a href="http://www.baidu.com">去百度</a>

    <div id="rightMenu">
        <ul>
            <li>Cut</li>
            <li>Copy</li>
            <li>Paste</li>
            <li>Run</li>
            <li>Definition</li>
        </ul>
    </div>
    <script>
        var myA = document.getElementsByTagName("a")[0];

        //!标准
        // myA.onclick = function () {
        //     return false;
        // }

        //标准
        // function deEvent(e) {
        //     var evE = e || event;
        //     evE.preventDefault();
        //     evE.returnValue = false; //IE下阻止默认事件
        // }

        //阻止鼠标右键默认行为
        // document.oncontextmenu = function () {
        //     return false;
        // }
        var myMenu = document.getElementsByTagName("div")[0];
        window.onload = function () {

            document.oncontextmenu = function (e) {
                var e = e || window.event;

                e.preventDefault();
                e.returnValue = false;

                myMenu.style.left = e.clientX + 'px';
                myMenu.style.top = e.clientY + 'px';
                myMenu.style.display = "block";

                myMenu.style.width = "100px";
                myMenu.style.height = "160px";
                myMenu.
                console.log(e.clientX);
                console.log(e.clientY);
            
                return false ;
            }
            document.onclick = function () {
                myMenu.style.display = 'none'; //隐藏菜单

            }
        }
    </script>

相关文章

网友评论

    本文标题:案例:自定义的鼠标右击菜单

    本文链接:https://www.haomeiwen.com/subject/nwtfkctx.html