-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contextmenu.html
102 lines (98 loc) · 2.92 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html>
<head>
<title>Right-click Context Menu In JavaScript</title>
</head>
<style>
.contextMenu {
position: absolute;
width: 100px;
height: auto;
color: #ffffff;
background: #262933;
border: 1px solid #333333;
box-shadow: 2px 2px 2px -1px rgba(0, 0, 0, 0.5);
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
display: none;
}
.contextMenu ul {
list-style: none;
padding: 0;
margin: 0;
}
.contextMenu ul li {
border-bottom: #ffffff 1px solid;
padding: 0;
margin: 0;
}
.contextMenu ul li:last-child{
border-bottom: none;
}
.contextMenu ul li a{
text-align: left;
display: block;
padding: 5px 10px;
color: #ffffff;
text-transform: capitalize;
text-decoration: none;
}
.contextMenu ul li a:hover {
background: #2777FF;
}
</style>
<body>
<h2>Right-click Context Menu In JavaScript</h2>
<h4>Right Click on any section of page to get Custom Context Menu</h4>
<script>
_currentMenuVisible = null;
document.addEventListener('contextmenu', e => {
e.preventDefault();
createContextMenu(e.clientX,e.clientY);
});
document.addEventListener('click', e => {
closeCurrentlyOpenedMenu();
});
/* close context menu */
window.onkeyup = function(e) {
if (e.keyCode === 27) {
closeCurrentlyOpenedMenu();
}
}
function createContextMenu(x, y) {
closeCurrentlyOpenedMenu();
const ctxMenuElem = document.createElement('div');
ctxMenuElem.classList.add('contextMenu');
const ulElem = document.createElement('ul');
const menuArr = ['edit', 'copy', 'delete'];
for (let ele of menuArr) {
let liElem = document.createElement('li');
liElem.innerHTML = '<a href="#">' + ele + '</a>';
ulElem.appendChild(liElem);
}
ctxMenuElem.appendChild(ulElem);
document.body.appendChild(ctxMenuElem);
_currentMenuVisible = ctxMenuElem;
ctxMenuElem.style.display = 'block';
ctxMenuElem.style.left = x + "px";
ctxMenuElem.style.top = y + "px";
}
function closeContextMenu(menu) {
menu.style.left='0px';
menu.style.top='0px';
console.log(menu);
document.body.removeChild(menu);
_currentMenuVisible = null;
}
function closeCurrentlyOpenedMenu() {
if (_currentMenuVisible !== null) {
closeContextMenu(_currentMenuVisible);
}
}
</script>
</body>
</html>