首页 > 其他 > 详细

自定义右键菜单

时间:2016-10-23 17:04:47      阅读:141      评论:0      收藏:0      [点我收藏+]

代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            html, body {
                margin: 0;
                padding: 0;
            }
            ul {
                width: 40px;
                list-style: none;
                display: inline-block;
                margin: 0;
                padding: 0;
                position: fixed;
            }
            ul li {
                padding-left: 10px;
                line-height: 20px;
                background: greenyellow;
            }
        </style>
    </head>
    <body>
        <ul id="customList">
            <li>11</li>
            <li>22</li>
            <li>33</li>
        </ul>
    </body>
    <script>
        document.oncontextmenu= function(event){
            var e= event || window.event;
            e.preventDefault? e.preventDefault():e.returnValue= false;
            var customList= document.getElementById("customList");
            customList.style.left= e.clientX+ "px";
            customList.style.top= e.clientY+ "px";
        }
    </script>
</html>

解析:

document.oncontextmenu: oncontextmenu方法在目标元素上右键时触发,兼容性良好

var e= event || window.event;
e.preventDefault? e.preventDefault():e.returnValue= false;

兼容IE并阻止默认行为

customList.style.left= e.clientX+ "px";
customList.style.top= e.clientY+ "px";

定位自定义的菜单

自定义右键菜单

原文:http://www.cnblogs.com/yanze/p/5990135.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!