利用contextmenu事件,自定义右键

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var div1 = document.getElementById("div1");
            var ul1 = document.getElementById("ul1");
            div1.oncontextmenu = function (event) {
                event.preventDefault();
                var left = (event.clientX+10)+"px";
                var right = (event.clientY-15)+"px";
 
                ul1.hidden = false;
                ul1.style.left = left;
                ul1.style.top = right;
            }
 
            window.onclick = function (event) {
                ul1.hidden = true;
            }
        }
    </script>
    <style type="text/css">
        ul {
            list-style: none;
            border: 1px red solid;
            background: #00ff90;
            position: absolute;
            padding:2px;
        }
 
        li {
            margin:20px 0px;
        }
        span {
           margin:5px 10px;
           display:block;
           text-align:center;
        }
    </style>
</head>
<body>
    <div style="width:100px;border:solid 1px blue" id="div1">点击我啊,菜鸡</div>
    <ul hidden="hidden" id="ul1">
        <li><span>新浪微博</span></li>
        <li><span>QQ空间</span></li>
        <li><span>QQ</span></li>
    </ul>
</body>
</html>
 

 

posted on 2016-05-25 19:51  drafire  阅读(148)  评论(0编辑  收藏  举报