自定义右键菜单
让原有的鼠标右键消失,自己定义鼠标右键菜单
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
ul,li{
list-style: none;
padding: 0;
margin: 0;
}
ul{
display: none;
position: absolute;
background: cyan;
}
li{
height: 20px;
50px;
}
li:hover{
background: burlywood;
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload=function(){
document.oncontextmenu=function(ev){
var clintX=ev.clientX; //ev获取的只是屏幕可视范围的x,y值
var clintY=ev.clientY;
var scollTop=document.documentElement.scrollTop|| document.body.scrollTop; //当有下拉条的时候必须加上当前屏幕不可视范围的left,和top值
var scollLeft=document.documentElement.scrollLeft || document.body.scrollLeft
document.getElementsByTagName('ul')[0].style.left=clintX+scollLeft+'px';
document.getElementsByTagName('ul')[0].style.top=clintY+scollTop+'px';
document.getElementsByTagName('ul')[0].style.display='block';
return false; //阻止鼠标右键菜单出现
}
}
</script>
</head>
<body style="height: 2000px;">
<ul>
<li>复制</li>
<li>粘贴</li>
<li>全选</li>
<li>删除</li>
</ul>
</body>
</html>