Home Web Front-end JS Tutorial Sharing a simple example of customizing the right-click menu using JS

Sharing a simple example of customizing the right-click menu using JS

May 31, 2017 am 10:09 AM

This article mainly introducesJSAn example of simple implementation of custom right-click menu. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

RT, a simple example, just telling the principle

The code is as follows:

<p id="menu" style="width: 0;height: 0;background: cadetblue;position: absolute"></menu>
Copy after login

Suppose I want to make the p above Set it as a right-click menu and beautify it as you like.

The principle is to use contextmenuevent. When you right-click, this event will be triggered. The eventobject can get the distance clientX from the mouse to the upper left corner of the page. and clientY,

We can use these two attributes to control the horizontal and vertical offset of p, and return false to cancel the default behavior of the event, To simulate the browser's right-click menu.

document.oncontextmenu=function(e){

  var x=e.clientX+&#39;px&#39;;

  var y=e.clientY+&#39;px&#39;;

  var node=document.querySelector(&#39;#menu&#39;);

  node.style.left=x;

  node.style.top=y;

  node.style.width=100+&#39;px&#39;;

  node.style.height=100+&#39;px&#39;;

  return false; //很重要,不能让浏览器显示自己的右键菜单

}
Copy after login

Now is the closing part. The way to close the right-click menu is usually to left-click in the blank area.

document.onclick=function(e){

  if(e.target.id!=&#39;menu&#39;)

  {

    var node=document.querySelector(&#39;#menu&#39;);

    node.style.width=0;

    node.style.height=0;

  }
}
Copy after login

This is just a basic idea, the core is the contextmenu event. On this basis, you can use CSS to beautify and upgrade at will, and add attributes such as transition to achieve the animation effect.

The above is the detailed content of Sharing a simple example of customizing the right-click menu using JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

An avatar on Netflix is ​​a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

How to solve the slow response of Win11 right-click menu? Solution to the slow pop-up menu of right-clicking the mouse in Win11 How to solve the slow response of Win11 right-click menu? Solution to the slow pop-up menu of right-clicking the mouse in Win11 Jan 31, 2024 pm 10:06 PM

After upgrading the Win11 system, some friends found that the pop-up menu when right-clicking the mouse has become slower. What is going on? Is it because of the configuration problem, or what kind of settings should be made? The editor of this website will tell you how to solve it today. In fact, we can adjust the performance to the highest performance, so that there will be no problem of slow response. Solution to the slow right-click pop-up menu of Win11 1. First, find the system settings in the start menu. 3. Then find the advanced system settings in the relevant links below. 5. Then under the visual effects tab, check Adjust for best performance.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to customize shortcut key settings in Eclipse How to customize shortcut key settings in Eclipse Jan 28, 2024 am 10:01 AM

How to customize shortcut key settings in Eclipse? As a developer, mastering shortcut keys is one of the keys to improving efficiency when coding in Eclipse. As a powerful integrated development environment, Eclipse not only provides many default shortcut keys, but also allows users to customize them according to their own preferences. This article will introduce how to customize shortcut key settings in Eclipse and give specific code examples. Open Eclipse First, open Eclipse and enter

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to add a drawing program to the right-click menu of Win11? How to add a drawing program to the right-click menu of Win11 How to add a drawing program to the right-click menu of Win11? How to add a drawing program to the right-click menu of Win11 Feb 11, 2024 pm 09:03 PM

Many win11 users want to add their drawing program to the right-click menu, so how do they add it? Users can directly right-click the Windows Terminal under Start, and then directly copy the following code to the corresponding location. Let this site carefully introduce to users how to add a drawing program to the Win11 right-click menu. How to add a drawing program to the Win11 right-click menu 1. First, right-click the "Start" button (or press the Win+X keys) and select "Windows Terminal (Administrator)". 2. Then paste this string of code into the terminal application [reg.exeadd&ldqu

The operation process of edius custom screen layout The operation process of edius custom screen layout Mar 27, 2024 pm 06:50 PM

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK

See all articles