-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.js
More file actions
44 lines (40 loc) · 1.2 KB
/
main.js
File metadata and controls
44 lines (40 loc) · 1.2 KB
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
const rightClickElement=document.getElementById("rightClickElement");
const menuBox =document.getElementById("rightClickMenu");
const menuItems = document.getElementsByClassName("item");
//Add click event to all menu items
for (var i=0;i<menuItems.length;i++) {
menuItems[i].addEventListener("click",onItemClick);
}
rightClickElement.addEventListener("contextmenu",popMenu);
document.body.addEventListener("click",onBodyClick);
function onItemClick(){
var itemNumber = this.getAttribute("number");
if(itemNumber=="1"){
hideMenu();
rightClickElement.style.backgroundColor="cornflowerblue";
}
if(itemNumber=="2"){
hideMenu();
rightClickElement.style.backgroundColor="mediumaquamarine";
}
if(itemNumber=="3"){
hideMenu();
rightClickElement.style.backgroundColor="palevioletred";
}
if(itemNumber=="4"){
hideMenu();
rightClickElement.style.backgroundColor="lightgray";
}
}
function onBodyClick(){
hideMenu();
}
function hideMenu() {
menuBox.style.display="none";
}
function popMenu(e) {
e.preventDefault();
menuBox.style.display="block";
menuBox.style.top=e.pageY+"px";
menuBox.style.left=e.pageX+"px";
}