javascript自定义右键菜单并屏蔽默认右键菜单【原创】

原理很简单,就是右击鼠标(oncontextmenu事件)return false阻止默认右键菜单,之后显示自定义的右键菜单,菜单显示位置由事件源的clientX和clientY计算得出。



[html]  view plain  copy
 print ?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>拖拽+自定义右键菜单</title>  
  6. <style type="text/css">  
  7. body{ margin:0; padding:0}  
  8. #ul1{ width:130px; height:120px; padding:10px 3px; background:#fff; border:#ACA899 1px solid; display:none; position:absolute; left:0;top:0;}  
  9. #ul1 li{ width:130px; height:28px; line-height:28px; font-size:14px; border-bottom:#ACA899 1px dashed; text-align:center; list-style:none}  
  10. #ul1 li:hover{ background:#316AC5}  
  11. </style>  
  12. <script type="text/javascript">  
  13. window.onload=function()  
  14. {  
  15. var ul=document.getElementById("ul1")  
  16. document.oncontextmenu=function(ev)  
  17. {  
  18. var ev=ev||window.event  
  19. var l=ev.clientX  
  20. var t=ev.clientY  
  21. ul.style.display="block"  
  22. ul.style.left=l+'px'  
  23. ul.style.top=t-16+'px'  
  24. return false;  
  25. }  
  26. document.onclick=function()  
  27. {  
  28. ul.style.display="none"  
  29. }  
  30. }  
  31. </script>  
  32. </head>  
  33.   
  34. <body>  
  35. <ul id="ul1">  
  36. <li>自定义右键菜单</li>  
  37. <li>自定义右键菜单</li>  
  38. <li>自定义右键菜单</li>  
  39. <li>自定义右键菜单</li>  
  40. </ul>  
  41. </body>  
  42. </html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值