-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_menu_example.html
110 lines (110 loc) · 4.55 KB
/
context_menu_example.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
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="[MAZ01001.github.io] Example of a custom context menu in HTML, JavaScript, and CSS">
<meta name="author" content="MAZ01001">
<link rel="apple-touch-icon" sizes="180x180" href="../img/apple-touch-icon.png">
<link rel="icon" type="image/x-icon" href="../img/MAZ_logo.svg">
<link rel="icon" type="image/png" sizes="32x32" href="../img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../img/favicon-16x16.png">
<link rel="manifest" href="../img/site.webmanifest">
<link rel="mask-icon" href="../img/safari-pinned-tab.svg" color="#ff9900">
<link rel="shortcut icon" href="../img/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="../img/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<title>Context menu example</title>
<style>
/* test area styling */
body{
background-color: #aaa;
height: 200vh;
width: 200vw;
}
div#box{
border-radius: 4em;
text-align: center;
background-color: #777;
height: 180vh;
width: 180vw;
position: absolute;
transform: translate(-50%,-50%);
top: 100vh;
left: 100vw;
}
/* context menu styling */
div#rmenu{
--top: -2rem;
--left: -2rem;
--translate_ho: -100%;
--translate_ve: -100%;
opacity: 0;
visibility: hidden;
position: fixed;
top: var(--top);
left: var(--left);
transform: translate(var(--translate_ho),var(--translate_ve));
max-width: max-content;
max-height: 100vh;
overflow: overlay;
transition: opacity 500ms ease-in 500ms,
visibility 0s linear 1s;
border: 10px solid black;
background: #000F;
color: #0F0F;
height: fit-content;
width: fit-content;
}
div#rmenu.visible{
opacity: 1;
visibility: visible;
transition: opacity 0s linear 0s,
visibility 0s linear 0s;
}
</style>
</head>
<body>
<div id="box" class="contextmenu_rmenu"><h1>test RM-click</h1></div>
<div id="rmenu">
<div style="display:inline-block;background-color:#333;">
<h1>test test test test test test</h1>
<br><br><br><br><br><h2>big</h2>
</div>
</div>
<script>
// context menu scripting
const rmenu=document.getElementById('rmenu'),
rmenu_position=
/** @param {number} screenX @param {number} screenY */
(screenX,screenY)=>{
const top=screenY,
left=screenX,
{height,width}=rmenu.getBoundingClientRect();
rmenu.style.setProperty('--top',top+'px');
rmenu.style.setProperty('--left',left+'px');
if(
width>(window.innerWidth-left)
&&left>(window.innerWidth-left)
)rmenu.style.setProperty('--translate_ho','-100%');
else rmenu.style.setProperty('--translate_ho','0');
if(height>(window.innerHeight-top)){
if(height>window.innerHeight)rmenu.style.setProperty('--translate_ve',`-${top}px`);
else rmenu.style.setProperty('--translate_ve',`-${height-(window.innerHeight-top)}px`);
}else rmenu.style.setProperty('--translate_ve','0');
};
document.body.addEventListener('resize',()=>rmenu.classList.remove('visible'),{passive:true});
document.body.addEventListener('contextmenu',event=>{
if(!event.target.classList.contains('contextmenu_rmenu'))return;
if(event.composedPath().includes(rmenu))return;
event.preventDefault();
rmenu_position(event.clientX,event.clientY);
//// rmenu_position(event.pageX-window.pageXOffset,event.pageY-window.pageYOffset);
rmenu.classList.add('visible');
},{passive:false});
rmenu.addEventListener('mouseleave',()=>rmenu.classList.remove('visible'),{passive:true});
rmenu.addEventListener('mouseover',()=>rmenu.classList.add('visible'),{passive:true});
</script>
</body>
</html>