SoFunction
Updated on 2025-03-01

The implementation principle and source code of the js custom right mouse button

Today, let’s record the right mouse button of JS and first decompose its implementation principle:

1. Block the right-click default event; (I thought the one I modified was the default event)

2. Hiding from an ul; (I once thought that this is worthy of div, sweat)

3. Response to the right click of the mouse and display the hidden ul;

4. After the mouse is clicked again, the ul is hidden again

From this point of view, is it much easier to do? First, put the code:

html part

<ul  style="width: 100px;background-color: yellow;position: absolute;z-index: 100;"> 
&lt;li&gt;&lt;a href="#">Begin</a></li>&lt;li&gt;&lt;a href="#">Pause</a></li>&lt;li&gt;&lt;a href="#">By bye</a></li>&lt;/ul&gt;

JavaScript part:

=function(){ 
var forRight=("testRight");//Get the object, it's so familiar now="none"; 
var title=("li"); 

for(var i=0;i&lt;;i++){ 
title[i].onmouseover=function(){ 
="active";//In fact, we can also call other events here.}; 
title[i].onmouseout=function(){//These are two events of the mouse.=""; 
}; 
} 

=function(event){//This is the key point of implementationvar event=event||;//This is not a problem="block"; 
=+"px"; 
=+"px";//The coordinates of the mousereturn false;//Return false here to block the default event}; 
=function(){//It's just for a more vivid imitation="none"; 
}; 
};


Let’s first look at the most critical part of today’s record: if this event returns false, it is to block the default event. If we don’t write anything else, we only write return in this event, as if it’s the following

=function(){ 
return false; 
}

In this way, there will be no response when right-clicking. Then come back and look at the entire event application. It seems that except for this event, the rest are more familiar events, but this kind of integration of events is always lacking. The key is to think about creativity. But no matter where there are too many, let’s do it first, but I want to read three thousand poems carefully, and I don’t want to write them and just ask for chanting. Horse stance, horse stance, horse stance...