This article shares the specific code of JavaScript to achieve the effect of clicking on the self-made menu for your reference. The specific content is as follows Application scenario: When we want users to not see the default browser menu pop up when they right-click, we need to prevent the browser from doing the default behavior and execute the desired effect. The first way is to create elements <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body { height: 3000px; } .menu { width: 100px; height: 280px; background-color: red; position: absolute; left: 0; top: 0; display: none; } </style> </head> <body> <script> var Bon = true; var menu = null; document.oncontextmenu = function(event) { if (Bon) { menu = document.createElement("div"); menu.classList.add("menu"); document.body.appendChild(menu); menu.style.left = event.pageX + "px"; menu.style.top = event.pageY + "px"; menu.style.display = "block"; Bon = false; event.preventDefault(); } else { menu.style.left = event.pageX + "px"; menu.style.top = event.pageY + "px"; event.preventDefault(); } } document.onmousedown = function(e) { if (e.button == 0) { var menu = document.querySelector(".menu"); document.body.removeChild(menu); Bon = true; } } </script> </body> </html> The second method : by hiding elements <div class="menu"></div> <script> var menu = document.querySelector(".menu"); document.oncontextmenu = function(event) { menu.style.left = event.pageX + "px"; menu.style.top = event.pageY + "px"; menu.style.display = "block"; event.preventDefault(); } document.onmousedown = function(e) { if (e.button == 0) { menu.style.display = "none"; } } </script> When we right-click, the default menu will not pop up, and the red box I set will pop up. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Automatic backup of MySQL database using shell script
>>: Two ways to completely delete users under Linux
1. CSS element hiding <br />In CSS, there ar...
Use examples to familiarize yourself with the mean...
1. Built-in functions 1. Mathematical functions r...
In the previous article, after using openssl to g...
Table of contents After creating a container loca...
Compared with ordinary programs, dynamic link lib...
Case Description: - Use tables to achieve page ef...
Why do you need to learn CSS overflow mechanism i...
When Mysql associates two tables, an error messag...
Linear-gradient background-image: linear-gradient...
Preface Recently, I was analyzing the startup pro...
Preface The apt-get command is a package manageme...
DCL (Data Control Language): Data control languag...
Preface Today, when I was using a self-written co...
Many people use Linux Homebrew. Here are three ti...