<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
ul {
display: none;
position: relative;
}
</style>
</head>
<body>
<div style="width:1000px;height: 1000px;">
<ul>
<li>复制</li>
<li>粘贴</li>
<li>取消</li>
</ul>
</div>
<script>
var myul = document.getElementsByTagName("ul")[0];
//鼠标右键菜单事件
document.oncontextmenu = function (e) {
//阻止默认菜单的出现,兼容写法
e.preventDefault ? e.preventDefault() : e.returnValue = false;
myul.style.display = "block";
var x = e.pageX;
var y = e.pageY;
myul.style.left = x + "px";
myul.style.top = y + "px";
window.onclick = function () {
myul.style.display = "none";
window.onclick=false;
}
}
</script>
</body>
</html>
js鼠标点击右键自定义菜单的实现
最新推荐文章于 2023-01-26 12:34:40 发布