js右键生成菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>右键菜单</title> </head> <style> *{ margin: 0; padding: 0; } ul{ list-style: none; width: 200px; padding: 10px; border: 1px solid black; display: none; position: absolute; } ul li:hover{ background: skyblue; } </style> <body> <ul id="list"> <li class="aaa">1111</li> <li class="bbb">2222</li> <li class="ccc">3333</li> <li class="ddd">4444</li> </ul> <script> document.addEventListener("contextmenu",function(evt){ evt.preventDefault() list.style.display="block" if(x >= document.documentElement.clientWidth-list.offsetWidth){ x=document.documentElement.clientWidth-list.offsetWidth } if(y >= document.documentElement.clientHeight-list.offsetHeight){ y=document.documentElement.clientHeight-list.offsetHeight } var x=evt.clientX var y=evt.clientY list.style.left=x + "px" list.style.top=y + "px" }) document.addEventListener("click",()=>{ list.style.display="none" }) list.onclick=function(evt){ console.log("list",evt.target) if(evt.target.className==="aaa"){ console.log(111111) } } </script> </body> </html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>右键菜单</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
width: 200px;
padding: 10px;
border: 1px solid black;
display: none;
position: absolute;
}
ul li:hover{
background: skyblue;
}
</style>
<body>
<ul id="list">
<li class="aaa">1111</li>
<li class="bbb">2222</li>
<li class="ccc">3333</li>
<li class="ddd">4444</li>
</ul>
<script>
document.addEventListener("contextmenu",function(evt){
evt.preventDefault()
list.style.display="block"
if(x >= document.documentElement.clientWidth-list.offsetWidth){
x=document.documentElement.clientWidth-list.offsetWidth
}
if(y >= document.documentElement.clientHeight-list.offsetHeight){
y=document.documentElement.clientHeight-list.offsetHeight
}
var x=evt.clientX
var y=evt.clientY
list.style.left=x + "px"
list.style.top=y + "px"
})
document.addEventListener("click",()=>{
list.style.display="none"
})
list.onclick=function(evt){
console.log("list",evt.target)
if(evt.target.className==="aaa"){
console.log(111111)
}
}
</script>
</body>
</html>