代码实现
''''html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定义网页右键菜单</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
body {
position: relative;
}
ul {
position: absolute;
left: 0;
top: 0;
width: 200px;
height: 300px;
background-color: #d43d3d;
border-radius: 8px;
overflow: hidden;
list-style-type: none;
display: none;
}
ul li {
height: 50px;
width: 100%;
cursor: pointer;
border-bottom: solid 1px #6898f1;
color: white;
text-align: center;
line-height: 50px;
font-size: 18px;
}
ul li:hover {
background-color: #da1ae8;
color: #333;
}
</style>
</head>
<body>
<ul class="menu" id="menu">
<li>复制</li>
<li>粘贴</li>
<li>会员充值</li>
<li>联系我们</li>
</ul>
<script>
let menu = document.getElementById("menu")
document.oncontextmenu = function (e) {
e.preventDefault()
}
document.onmousedown = function (e) {
if (e.button == "2") {
menu.style.display = "block"
menu.style.left = e.clientX + "px"
menu.style.top = e.clientY + "px"
}
}
document.onclick = function () {
menu.style.display = "none"
}
</script>
</body>
</html>