Home  >  Article  >  Web Front-end  >  Simple right-click menu class implemented in Javascript

Simple right-click menu class implemented in Javascript

PHPz
PHPzforward
2016-05-16 15:38:211064browse

This article mainly introduces the simple right-click menu class implemented in Javascript. The right-click menu function is implemented through JavaScript custom classes. It has certain reference value. Friends in need can refer to it. The details are as follows:

This It is a right-click menu class written by myself, which blocks IE's inherent right-click menu. There are four parameters in total: the first is the id of p

to display the right-click menu, and the second is the right-click menu layer. id, create a new layer based on this id, menuList is a list of menu items, corresponding to the function triggered after clicking a menu item, classList is the class name of the menu, and the class name corresponding to the menu item, including the mouse slide Obsolete class.

A screenshot of the running effect is as follows:

The online demo address is as follows:

http://demo.jb51.net/ js/2015/js-right-button-menu-class-codes/

The specific code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>右键菜单</title>
<style type="text/css">
.cmenu
{
 position:absolute;
 top:100px;
 left:100px;
 width:200px;
 height:200px;
 background-color:white;
 border:1px solid pink;
}
.liAble
{
 font-family:"宋体";
 color:#6699CC;
 margin-left:10px;
 margin-top:5px;
 list-style-type:none;
 cursor:default;
}
.liMouseOver
{
 margin-left:10px;
 margin-top:5px;
 background-color:#CCFFFF;
 list-style-type:none;
 cursor:default;
}
</style>
</head>
<body>
<p style="margin-left:auto; margin-right:auto; height:300px; width:60%;background-color:#CC6699" id="x">
</p>
<input type="hidden" id="value1" value="4" />
<input type="hidden" id="value2" value="5" />
<script type="text/javascript">
//右键菜单类
function RightHandMenu(p,menup,menuList,classList)
{
 var oThis = this;
 this._menuList = 
 {
 }
 this._classList = 
 {
  objClass:&#39;&#39;,
  MenuClass:&#39;&#39;,
  liAbleClass:&#39;&#39;,
  liMouseOverClass:&#39;&#39;
 }
 this.Init = function()
 {
  this._obj = $(p);
  this._obj.oncontextmenu = function(e){oThis.ShowMenu(e)};
  this._obj.className = this._classList.objClass;
  document.onclick = function(){oThis.HiddenMenu()};
  objToObj(this._classList, classList);
  objToObj(this._menuList, menuList);
 }
 this.CreateMenu = function()
 {
  if($(menup))
  {
   alert("该ID已被占用");
   return;
  }
  this._menu = document.createElement("p");
  this._menu.id = menup;
  this._menu.oncontextmenu = function(e){stopBubble(e)};
  this._menu.className = this._classList.MenuClass;
  this._menu.style.display = "none";
  document.body.appendChild(this._menu);
 }
 this.CreateMenuList = function()
 {
  for(var pro in this._menuList)
  {
   var li = document.createElement("LI");
   li.innerHTML = pro;
   this._menu.appendChild(li);
   li.className = this._classList.liAbleClass;
   li.onclick = this._menuList[pro];
   li.onmouseover = function(){oThis.ChangeLiClass(this,oThis._classList.liMouseOverClass)}
   li.onmouseout = function(){oThis.ChangeLiClass(this,oThis._classList.liAbleClass)}
  }
 }
 this.ChangeLiClass = function(obj,name)
 {
  obj.className = name
 }
 this.ShowMenu = function(e)
 {
  var e = e || window.event;
  stopBubble(e);
  var offsetX = e.clientX;
  var offsetY = e.clientY;
  with(this._menu.style)
  {
   display = "block";
   top = offsetY + "px";
   left = offsetX + "px";
  }
 }
 this.HiddenMenu = function()
 {
  this._menu.style.display = "none";
 }
 this.Init();
 this.CreateMenu();
 this.CreateMenuList();
}
function stopBubble(oEvent)
{
 if(oEvent.stopPropagation) oEvent.stopPropagation();
 else oEvent.cancelBubble = true;
  if(oEvent.preventDefault) oEvent.preventDefault();
 else oEvent.returnValue = false;
}
function $(p)
{
 return &#39;string&#39; == typeof p ? document.getElementById(p) : p;
}
function objToObj(destination,source)
{
 for(var pro in source)
 {
  destination[pro] = source[pro];
 }
 return destination;
}
//构造右键菜单
function Edit()
{
 alert("edit");
}
function Delete()
{
 alert("delete");
}
var menuList = 
{
 编辑:Edit,
 删除:Delete
}
var classList = 
{
 MenuClass:&#39;cmenu&#39;,
 liAbleClass:&#39;liAble&#39;,
 liMouseOverClass:&#39;liMouseOver&#39;
}
var x = new RightHandMenu("x","testp",menuList,classList)
</script>
</body>
</html>

The above is the entire content of this chapter. For more related tutorials, please Visit JavaScript Video Tutorial!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete