js 自定义右键菜单

2017-10-12  本文已影响0人  发光驴子
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>Title</title>
  </head>
        <style>
            * {margin:0; padding:0;}
            #ul1 {width:100px; background:#CCC; border:1px solid black; position:absolute; display:none;}
        </style>
        <script>
//右键菜单事件
      document.oncontextmenu=function (ev) {
          var scllTop=document.documentElement.scrollTop||document.body.scrollTop;
          var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;


          var oEvent=ev||event;
          var oUl=document.getElementById("ul1");
          oUl.style.listStyle="none";
          oUl.style.display="block";
          oUl.style.left=oEvent.clientX+scrollLeft+"px";
          oUl.style.top=oEvent.clientY+scllTop+"px";


          return false;//是阻止默认的右键事件,
      };

      document.onclick=function () {
          var oUl=document.getElementById("ul1");
          oUl.style.display="none";
}
  </script>


  <body style="height: 2000px">

  <ul id="ul1">
      <li>登录</li>
      <li>ss</li>
      <li>kk</li>
      <li>加入</li>
  </ul>

  </body>
  </html>
上一篇下一篇

猜你喜欢

热点阅读