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 lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>自定义菜单栏</title> <style> #menu { height: 180px; overflow-x: hidden; border: 1px solid #cacaca; box-shadow: 2px 2px 5px #d4d4d4; position: absolute; display: none; }
#menu ul { padding: 0; list-style: none; }
#menu ul li { width: 130px; height: 25px; line-height: 25px; padding: 0 10px; } </style>
</head>
<body> <div id="menu"> <ul> <a href="#"> <li>菜单1</li> </a> <a href="#"> <li>菜单2</li> </a> <a href="#"> <li>菜单3</li> </a> <a href="#"> <li>菜单4</li> </a> <a href="#"> <li>菜单5</li> </a> <a href="#"> <li>菜单6</li> </a> <a href="#"> <li>菜单6</li> </a> <a href="#"> <li>菜单6</li> </a> <a href="#"> <li>菜单6</li> </a> <a href="#"> <li>菜单6</li> </a> </ul> </div>
<script> const menu = document.querySelector("#menu");
window.oncontextmenu = function (e) { e.preventDefault(); const menuHeight = menu.offsetHeight; const menuWidth = menu.offsetWidth;
if (menuWidth + e.clientX > window.innerWidth) { menu.style.left = e.clientX - menuWidth + 5 + 'px'; } else { menu.style.left = e.clientX + 'px'; } if (menuHeight + e.clientY > window.innerHeight) { menu.style.top = e.clientY - menuHeight + 5 + 'px'; } else { menu.style.top = e.clientY + 'px'; }
menu.style.width = '125px'; menu.style.display = 'block'; } window.onclick = function () { menu.style.display = 'none'; } </script> </body>
</html>
|