js实现的XP风格的右键菜单

使用说明:
在使用前,先建立菜单对象 
var obj = new MouseMenu("obj"); 
注意,obj 要和 MouseMenu("obj") 的obj 相同 
如建立了 obj 那么 obj 将会有以下方法,开始 obj.length = 0 
方法: 
1. addMenu(express[,icon][,url][,target][,title]); 
功能: 插入一个菜单,并且 返回一个功能和上面说 提起的 obj 一样的菜单对象 
obj[obj.length] 然后 obj.length 将会 +1 
说明: 
express 必须,字符串,显示在插入的菜单上的文本 
icon 可选,整数(>0),如果后面有选项,你不需要ICON的话,填写 "" 
url 可选,字符串,路径 
target 可选,字符串 就是 target 
title 可选,字符串 就是 title 
2. addLink(express,[,icon][,url][,target][,title]) 
功能: 插入一个连接 然后 obj.length 将会 +1 
说明 
express 必须,字符串,显示在插入的菜单上的文本 
icon 可选,整数(>0),如果后面有选项,你不需要ICON的话,填写 "" 
url 必须,字符串,路径 
target 可选,字符串 就是 target 
title 可选,字符串 就是 title 
3. addHR() 
功能: 插入分割线 
说明: 
他不属于 link 和 menu 对象 所以使用时,obj.length 不变 
比如: 
var MM = new MouseMenu("MM") 
MM.addLink("连接","","http://hello.com") 
MM.addHR() 
MM.addMenu("菜单") 
//下面 是 MM[1] 而不是 MM[2] 因为 MM.addHR() MM.length 不会增加 
MM[1].addLink("连接","","http://hello.com") 
好了,说了这么多,应该会用了吧? 
突然有人问:“在哪插入连接……” 
…… 嗯~ 这个 ^_^! 看上面吧~~~~ 
附加:在代码中的其他的数据 
//--------------- 有关数据 -----------------// 
var IconList = new Array(); // icon图片 集合, 下标从 1 开始 
IconList[1] = new Image(); 
IconList[1].src = "icon/sub.gif"; 
//--------------------------- 声音开关 ------ 声音开关 ------------------// 
var mouseCanSound = true; 
var menuSpeed = 50; //---------- 菜单显示速度 ------------// 
var alphaStep = 30; //---------- Alpaha 变化 度 -----------//

网页特效|www.jb51.net|---XP风格的右键菜单
.menutable {
BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM: #307ce8 1px solid; BORDER-LEFT: #307ce8 5px solid; BORDER-RIGHT: #307ce8 1px solid; BORDER-TOP: #94bcf3 1px solid; FONT-SIZE: 12px; POSITION: absolute; Z-INDEX: 100
}
.menutrin {
BACKGROUND-COLOR: #1a71e6; COLOR: #ffffff; CURSOR: hand
}
.menutrout {
COLOR: #000000; CURSOR: hand
}
.menutd0 {
HEIGHT: 25px; TEXT-ALIGN: center; WIDTH: 28px; 改变这个修改菜单高度--->:
}
.menutd1 {
FONT-FAMILY: Webdings; TEXT-ALIGN: right; WIDTH: 46px
}
.linktd1 {
WIDTH: 46px
}
.menutd2 {
WIDTH: 4px
}
.menuhr {
BORDER-BOTTOM: #307ce8 1px inset; BORDER-LEFT: #307ce8 1px inset; BORDER-RIGHT: #307ce8 1px inset; BORDER-TOP: #307ce8 1px inset
}

');
}
//----------- 构建 子菜单 对象 -------------//
function SubMenu(objName,objID)
{
this.obj = objName;
this.id = objID;
this.addMenu = addMenu;
this.addLink = addLink;
this.addHR = addHR;
this.length = 0;
}
//-------------- 生成 菜单 makeMenu 方法 -----------//
function makeMenu(subID,oldID,word,icon,url,target,thetitle)
{
var thelink = '';

if(icon&&icon!="")
{
icon = '';
}
else
{
icon = '';
}

if(!thetitle||thetitle=="")
{
thetitle = '';
}

if(url&&url!="")
{
thelink += '';
}

var Oobj = document.getElementById(oldID);
/*--------------------------------------------- 菜单html样式

菜单一 4  

--------------------------------------------------*/

Oobj.insertRow();

with(Oobj.rows(Oobj.rows.length-1))
{
id = "tr"+subID;
className = "menutrout";

title = thetitle;
}

eventObj = "tr"+subID;

eval(eventObj+'.attachEvent("onmouseover",MtrOver('+eventObj+'))');
eval(eventObj+'.attachEvent("onclick",MtrClick('+eventObj+'))');

var trObj = eval(eventObj);
for(i=0;i'+word+'';
cells(1).calssName = "indentWord"

cells(2).className = "menutd1";
cells(2).innerHTML = "4";

cells(3).className = "menutd2";
cells(3).innerText = " ";

}

document.body.insertAdjacentHTML('beforeEnd','

');

}
//---------------- 生成连接 makeLink 方法 ------------//
function makeLink(subID,oldID,word,icon,url,target,thetitle)
{

var thelink = '';

if(icon&&icon!="")
{
icon = '';
}
else
{
icon = '';
}

if(!thetitle||thetitle=="")
{
thetitle = '';
}

if(url&&url!="")
{
thelink += '';
}

var Oobj = document.getElementById(oldID);

/*--------------------------------------------- 连接html样式

连接一  

--------------------------------------------------*/

Oobj.insertRow();

with(Oobj.rows(Oobj.rows.length-1))
{
id = "tr"+subID;
className = "menutrout";
title = thetitle;
}

eventObj = "tr"+subID;

eval(eventObj+'.attachEvent("onmouseover",LtrOver('+eventObj+'))');
eval(eventObj+'.attachEvent("onmouseout",LtrOut('+eventObj+'))');
eval(eventObj+'.attachEvent("onclick",MtrClick('+eventObj+'))');

var trObj = eval(eventObj);
for(i=0;i'+word+'';
cells(2).className = "linktd1";
cells(2).innerText = " ";

cells(3).className = "menutd2";
cells(3).innerText = " ";

}
}
//-------------- 菜单对象 addMenu 方法 ------------//
function addMenu(word,icon,url,target,title)
{
var subID = this.id + "_" + this.length;
var subObj = this.obj+"["+this.length+"]";

var oldID = this.id;

eval(subObj+"= new SubMenu('"+subObj+"','"+subID+"')");

makeMenu(subID,oldID,word,icon,url,target,title);
this.length++;

}
//------------- 菜单对象 addLink 方法 -------------//
function addLink(word,icon,url,target,title)
{
var subID = this.id + "_" + this.length;
var oldID = this.id;

makeLink(subID,oldID,word,icon,url,target,title);
this.length++;
}
//------------ 菜单对象 addHR 方法 -----------------//
function addHR()
{
var oldID = this.id;
var Oobj = document.getElementById(oldID);

Oobj.insertRow();

/*------------------------------------------


--------------------------------------------*/

Oobj.rows(Oobj.rows.length-1).insertCell();
with(Oobj.rows(Oobj.rows.length-1))
{
cells(0).colSpan= 4;
cells(0).insertAdjacentHTML('beforeEnd','


');
}

}
//--------- MtrOver(obj)-------------------//
function MtrOver(obj)
{
return sub_over;

function sub_over()
{

var sonid = obj.id.substring(2,obj.id.length);

var topobj = obj.parentElement.parentElement;

NowSubMenu = topobj.id;

if(obj.className=="menutrout")
{
mouseWave();
}

HideMenu(1);

SubMenuList[returnIndex(NowSubMenu)] = NowSubMenu;
ShowTheMenu(sonid,MPreturn(sonid))

SubMenuList[returnIndex(obj.id)] = sonid;

if(topobj.oldTR)
{
eval(topobj.oldTR+'.className = "menutrout"');
}
obj.className = "menutrin";
topobj.oldTR = obj.id;

}
}
//--------- LtrOver(obj)-------------------//
function LtrOver(obj)
{
return sub_over;

function sub_over()
{
var topobj = obj.parentElement.parentElement;
NowSubMenu = topobj.id;

HideMenu(1);

SubMenuList[returnIndex(NowSubMenu)] = NowSubMenu;

if(topobj.oldTR)
{
eval(topobj.oldTR+'.className = "menutrout"');
}
obj.className = "menutrin";
topobj.oldTR = obj.id;
}
}
//--------- LtrOut(obj)-------------------//
function LtrOut(obj)
{
return sub_out;

function sub_out()
{
var topobj = obj.parentElement.parentElement;

obj.className = "menutrout";

topobj.oldTR = false;
}
}
//----------MtrClick(obj)-----------------//
function MtrClick(obj)
{
return sub_click;

function sub_click()
{
if(obj.cells(1).all.tags("A").length>0)
{
obj.cells(1).all.tags("A")(0).click();
}
}
}
//---------- returnIndex(str)--------------//
function returnIndex(str)
{
return (str.split("_").length-3)
}
//---------ShowTheMenu(obj,num)-----------------//
function ShowTheMenu(obj,num)
{
var topobj = eval(obj.substring(0,obj.length-2));

var trobj = eval("tr"+obj);

var obj = eval(obj);

if(num==0)
{
with(obj.style)
{
pixelLeft = topobj.style.pixelLeft +topobj.offsetWidth;
pixelTop = topobj.style.pixelTop + trobj.offsetTop;
}
}
if(num==1)
{
with(obj.style)
{
pixelLeft = topobj.style.pixelLeft + topobj.offsetWidth;
pixelTop = topobj.style.pixelTop + trobj.offsetTop + trobj.offsetHeight - obj.offsetHeight;
}
}
if(num==2)
{
with(obj.style)
{
pixelLeft = topobj.style.pixelLeft - obj.offsetWidth;
pixelTop = topobj.style.pixelTop + trobj.offsetTop;
}
}
if(num==3)
{
with(obj.style)
{
pixelLeft = topobj.style.pixelLeft - obj.offsetWidth;
pixelTop = topobj.style.pixelTop + trobj.offsetTop + trobj.offsetHeight - obj.offsetHeight;
}
}

obj.style.visibility = "visible";

if(obj.alphaing)
{
clearInterval(obj.alphaing);
}

obj.alphaing = setInterval("menu_alpha_up("+obj.id+","+alphaStep+")",menuSpeed);
}
//----------HideMenu(num)-------------------//
/*----------------------
var SubMenuList = new Array();
var NowSubMenu = "";
---------------------*/
function HideMenu(num)
{
var thenowMenu = "";

var obj = null;

if(num==1)
{
thenowMenu = NowSubMenu
}

for(i=SubMenuList.length-1;i>=0;i--)
{
if(SubMenuList[i]&&SubMenuList[i]!=thenowMenu)
{

obj = eval(SubMenuList[i]);

if(obj.alphaing)
{
clearInterval(obj.alphaing);
}
obj.alphaing = setInterval("menu_alpha_down("+obj.id+","+alphaStep+")",menuSpeed);

obj.style.visibility = "hidden";

eval("tr"+SubMenuList[i]).className = "menutrout";

SubMenuList[i] = null;
}
else
{
if(SubMenuList[i]==thenowMenu)
{
return;
}
}
}

NowSubMenu = "";
}
//-----------MainMenuPosition return()------------//
function MMPreturn()
{
var obj = eval(JustMenuID);

var x = event.clientX;
var y = event.clientY;

var judgerX = x + obj.offsetWidth;
var judgerY = y + obj.offsetHeight;
var px = 0;
var py = 0;

if(judgerX>document.body.clientWidth)
{
px = 2;
}
if(judgerY>document.body.clientHeight)
{
py = 1;
}

return (px+py);
}
//-----------MenuPosition return(obj)--------------//
function MPreturn(obj)
{
var topobj = eval(obj.substring(0,obj.length-2));

var trobj = eval("tr"+obj);

var x = topobj.style.pixelLeft + topobj.offsetWidth;
var y = topobj.style.pixelTop + trobj.offsetTop;
obj = eval(obj);

var judgerY = obj.offsetHeight + y;
var judgerX = obj.offsetWidth + x;

var py = 0;
var px = 0;

if(judgerY>=document.body.clientHeight)
{
py = 1;
}

if(judgerX>= document.body.clientWidth)
{
px = 2;
}

return (px+py);
}
//-----------mouseWave()-------------//
function mouseWave()
{
if(mouseCanSound)
{
theBS.src= "sound/sound.wav";
}
}
//----------- menu_alpha_down -------//
function menu_alpha_down(obj,num)
{
var obj = eval(obj);

if(obj.filters.Alpha.Opacity > 0 )
{
obj.filters.Alpha.Opacity += -num;
}
else
{
clearInterval(obj.alphaing);
obj.filters.Alpha.Opacity = 0;
obj.alphaing = false;
obj.style.visibility = "hidden";
}
}
//------------ menu_alpha_up --------//
function menu_alpha_up(obj,num)
{
var obj = eval(obj);

if(obj.filters.Alpha.Opacity

var one = new MouseMenu("one");

one.addMenu("菜单一",1,"javascript:alert('I am menu');","_self","I am title");
one[0].addLink("连接一",2,"javascript:alert('I am link')")
one[0].addHR()
one[0].addLink("连接二","","javascript:alert('I am link')")
one[0].addMenu("菜单三");
one[0][one[0].length-1].addLink("连接一",1,"javascript:;")
one[0].addLink("连接三","","javascript:alert('I am link')")
one[0].addLink("连接四","","javascript:alert('I am link')")
one.addLink("连接二","","javascript:alert('I am link')")
one.addMenu("菜单二",3);
one[2].addLink("连接一","","javascript:alert('I am link')")
one.addHR();
one.addLink("连接三,多长都可以",4,"javascript:alert('I am link')")

有声音的哦…… 可以关闭!设置 mouseCanSound = false;
就可以了

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

(0)

相关推荐

  • js捕获鼠标右键菜单中的粘帖事件实现代码

    比如点击菜单中的"粘帖"后事件如何捕获  问题解决了! 用的jQuery中的paste事件,这里需要注意,如想获得粘帖文本要使用setTimeout控制下时间. 复制代码 代码如下: $("#input").bind('paste', function(e) { var el = $(this); setTimeout(function() { var text = $(el).val(); alert(text); }, 100); }); 貌似是在1.4版后加上

  • 用js实现网页上模仿桌面右键菜单

    网页上模仿桌面右键菜单 右击鼠标看看 -_-! \n"; // rbpm = right button pop menu HTMLstr += " \n"; HTMLstr += "\n"; HTMLstr += " \n"; HTMLstr += " \n"; HTMLstr += ""; HTMLstr += "\n"; HTMLstr += "\n";

  • 深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制

    我记得在刚开始接触动态HTML及JavaScript时就接触过关于鼠标右键屏蔽的脚本代码,当时这些代码很多会用在防止浏览者未经允许的复制网页上的文字或者其他内容,后来的实际应用证明这种做法是不符合用户体验的,而且破解的方法也有很多,比如我曾经写过一篇文章讲解如何解除网页禁止复制的办法. 由此可见,限制右键及复制是不明智的做法,但是今天我仍然要谈谈关于禁止网页复制.右键菜单的事儿,因为随着网页APP技术的发展,网页应用和桌面应用之间的界限越来越模糊,有一些桌面程序实际上是由网页配合JavaScri

  • js 右键菜单,支持不同对象不同菜单(兼容IE、Firefox)

    里面的代码修改也比较简单. ddd .DreamMenu { position:absolute; visibility:hidden; z-index:100; overflow:hidden; width:150px; background-color:buttonface; border:dimgray 1px solid !important; border:buttonhighlight menu menu buttonhighlight 2px outset; padding:1px

  • js右键菜单效果代码

    将以下代码添加到<head>与</head>之间  ---------------------------------- <script language='javascript'>  /*******以下内容可以修改***************/  var mname=new Array(  "首  页",  "修  改",  "下  载",  "删  除",  "新  建&q

  • 利用JS重写Cognos右键菜单的实现代码

    复制代码 代码如下: <!--菜单样式--> <STYLE type=text/css> .cMenu { BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; FONT-WEIGHT: normal; FONT-SIZE: 14px; VISIBILITY: hidden; BORDER-LEFT: #000000 1px solid; WIDTH: 150px; CURSOR: default; COLO

  • js禁止页面复制功能禁用页面右键菜单示例代码

    <body oncontextmenu="return false">禁用网页右键菜单,但是仍然可以使用快捷键复制. js代码禁用复制功能: 复制代码 代码如下: <script type="text/javascript"> document.body.onselectstart=document.body.oncontextmenu=function(){ return false;} </script> 注意这段代码必须放在

  • JavaScript 对任意元素,自定义右键菜单的实现方法

    一.一些概念: 1.鼠标事件有一个botton属性:返回一个整数,用于表示点击的是哪个鼠标按键. BUG:在IE和标准DOM的鼠标事件中,唯一一个button属性值相同的是"单击右键"事件,都返回2. 2.事件onmousedown:表示鼠标按键按下的动作. 事件oncontextmenu:点击鼠标触发的另一个事件. 3.中断默认事件处理函数的方法:IE中设置returnValue=false;  标准DOM中调用preventDefault()方法. 4.事件对象:①在IE中,事件对

  • JavaScript模拟鼠标右键菜单效果

    本文实例为大家分享了JavaScript模拟鼠标右键菜单的具体代码,供大家参考,具体代码如下 效果图: 具体代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>右键菜单</title> <style type="text/css"> *{ margin: 0; padd

  • ExtJs grid行 右键菜单的两种方法

    在这下边: 方法一 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridContextMenu.aspx.cs" Inherits="gridContextMenu" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &

随机推荐