Customize right-click menu
Customized right-click menu
Customized right-click menu. For some ERP systems, there are many functional operations, so we usually put several commonly used functions in the customized right-click menu for the convenience of users.
Implementing a custom menu is very simple. First, we need to block the original right-click menu. The custom menu appears at the position of the mouse. Click to hide the custom menu. This is the process.
*{margin:0;padding:0} a{text-decoration:none} ul li{list-style:none} .menu{border:1px solid black;border-radius:5px;display:inline-block;position:fixed;top:100px;left:550px; overflow:hidden;padding-bottom:10px;box-shadow:0 0 10px 0;z-index:999;display:none;background:white} .menu ul li{height:30px;width:100%} .menu ul li a{height:30px;display:inline-block;width:100%;text-align:left;line-height:30px;padding:5px 10px} .menu li a:hover{background-color:#EEF5E2}
<div class="menu" id="demo"> <ul> <li><a href="#">向录入员发送消息</a></li> <li><a href="#">发送选中作业</a></li> <li><a href="#">设置作业状态</a></li> <li><a href="#">哈哈哈哈哈</a></li> <li><a href="#">嘻嘻嘻嘻嘻</a></li> <li><a href="#">呵呵呵呵呵呵</a></li> </ul> </div>
1. Shield the original right-click menu
There is an event provided in JS to accomplish this, which is oncontextmenu. This is the event-bound area. When you right-click, the original right-click menu will no longer appear:
document.oncontextmenu = function(){ return false; }
2. Get the size of the custom menu
The custom menu is initially hidden, and we have no way to get the width and height of hidden elements. We can first set the menu element to visibility: hidden, get the width and height of the element, and then remove this attribute:
function getHideDOMWH(obj){ //obj为菜单元素的DOM对象 var wh = {}; obj.style.visibility = "hidden"; wh.w = obj.scrollWidth; wh.h = obj.scrollHeight; obj.style.visibility = null; return wh; }
3. The custom menu appears at the current mouse position
First we need to get the current mouse position , the current mouse position is where the custom menu appears. At the same time, pay attention to the situation where the distance from the right or the distance from below is not enough to display the menu:
document.onmousedown = function(e){ //obj为菜单元素DOM对象 var e = e || window.event; if(e.button==2){ var left = e.clientX; var top = e.clientY; var windowHeight = document.body.scrollHeight; var windowWidth = document.body.scrollWidth; var wh = getHideDOMWH(obj); var contentHeight = wh.h; var contentWidth = wh.w; obj.style.left = windowWidth-left>contentWidth?left+"px":windowWidth-contentWidth+"px"; obj.style.top = windowHeight-top>contentHeight?top+"px":top-contentHeight+"px"; obj.style.display = "inline-block"; } }
When the width of the browser window minus the distance from the left when the cursor is clicked is less than the width of the menu, it means the distance from the cursor to the right It is no longer enough to display the menu, so we let the menu stick to the right. At this time, the left value of the menu is the width of the browser window minus the width of the menu. When the height of the browser window minus the distance from the cursor to the upper side is less than the height of the menu, it means that the distance from the cursor to the bottom is not enough to display the menu. At this time, the top value of the menu is the distance from the cursor to the top minus the distance of the menu. high.
4. Hide the menu whether you click on the menu element or other elements
document.onclick = function(){ //obj为菜单元素DOM对象 obj.style.display = "none"; }
For ease of use, we can encapsulate the above process into a method, or extend a method on the prototype of the native JS Element (IE7 and below are will report an error), or on the prototype of JQ, here we encapsulate a method.
function contextMenu(obj){ //obj为DOM对象 document.oncontextmenu = function(){ return false; } document.onmousedown = function(e){ var e = e||window.event; if(e.button==2){ var mouseX = e.clientX; var mouseY = e.clientY; var wh = getObjWH(obj); var contentW = wh.w; var contentH = wh.h; var documentW = document.body.scrollWidth; var documentH = document.body.scrollHeight; obj.style.left = documentW-mouseX<contentW?documentW-contentW+"px":mouseX+"px"; obj.style.top = documentH-mouseY<contentH?mouseY-contentH+"px":mouseY+"px"; obj.style.display = "inline-block"; } } document.onclick = function(){ obj.style.display = "none"; } function getObjWH(obj){ var wh = {}; obj.style.visibility = "hidden"; wh.w = obj.scrollWidth; wh.h = obj.scrollHeight; obj.style.visibility = null; return wh; } }
All our events are bound to the document, and we usually use the bubbling mechanism for events. If we set cancel bubbling on a certain element, there will be some problems, and we need to do something targeted. deal with.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
