-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontextmenu.html
62 lines (59 loc) · 1.52 KB
/
contextmenu.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
.contextmenu{
width: 200px;
border: solid 1px #0000FF;
display: none;
position: absolute;
}
.contextmenu li{
width: 200px;
line-height: 30px;
text-align: center;
background: #DDD;
margin: 10px 0;
}
.contextmenu li:hover{
background: #4D4D4D;
}
</style>
</head>
<body>
<ul class="contextmenu">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
const contextmenu = document.getElementsByClassName('contextmenu')[0];
window.addEventListener('contextmenu',function (ev) {
contextmenu.style.display = 'block';
contextmenu.style.top = ev.pageY + 'px';
contextmenu.style.left = ev.pageX + 'px';
ev.preventDefault(); //阻止默认右键菜单出现
})
contextmenu.onclick = function (ev) {
if(ev.target.tagName === 'LI'){
console.log(ev.target);
}
ev.cancelBubble = true; //阻止冒泡,否则菜单点击会触发window的点击事件,菜单会消失
}
window.onclick = function (ev) {
contextmenu.style.display = 'none';
}
</script>
</body>
</html>