一个简单的仿xp的js下拉菜单,本来想用在我的网站的后台管理,但后来发现不实用,还是采用了左右框架的树型菜单,但这个程序既然写出来了,贴出来,希望对其它人有用,有兴趣的朋友可以对快捷键功能进行扩展,目前留有接口,但未实现。
menu.htm(菜单主文件)
<html>
<head>
<title>Noteless Valley(lijun.org)程序示例</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="xp_menu.css" rel="stylesheet" type="text/css">
<script language="javascript" src="xp_menu.js"></script>
<script language="javascript" for="document" event="onselectstart">
<!--
return document_onselectstart()
//-->
</script>
<script language="javascript" for="document" event="onclick">
<!--
return document_onclick()
//-->
</script>
</head>
<body scroll="no" language="javascript" onresize="return window_onresize()" onload="return window_onload()">
<script language="javascript">
<!--
//定义一个主菜单对象
var mnuMain = new menu("mnuMain");
mnuMain.bar[0] = new menu_bar("mnuMainNL","","无名谷","","Noteless Valley","",0,"mnuNL");
mnuMain.bar[1] = new menu_bar("mnuMainSiteAdmin","","网站管理","","网站管理","",0,"mnuSiteAdmin");
mnuMain.bar[2] = new menu_bar("mnuMainSystem","","系统管理","","系统管理","",0,"mnuSystem");
mnuMain.bar[3] = new menu_bar("mnuMainShow","","演示菜单","","Menu Show","",0,"mnuShow");
popMainMenu(mnuMain,0,0,"100%",27); //激活主菜单,通过参数确定主菜单的位置
var mnuNL = new menu("mnuNL");
mnuNL.bar[0] = new menu_bar("mnuHomePage","","首页","","HomePage","",1,"http://lijun.org");
mnuNL.bar[1] = new menu_bar("mnuDesign","","网页设计","","Design","",1,"http://lijun.org/article.asp?topic=1");
mnuNL.bar[2] = new menu_bar("mnuProgram","","网络编程","","Program","",1,"http://lijun.org/article.asp?topic=2");
mnuNL.bar[3] = new menu_bar("mnuOthers","","其它文章","","Others","",1,"http://lijun.org/article.asp?topic=3");
popSubMenu(mnuNL);
var mnuSiteAdmin = new menu("mnuSiteAdmin")
mnuSiteAdmin.bar[0] = new menu_bar("mnuAddressUpdate","","访问地区更新","","访问地区更新","",1,"AddressUpdate.asp");
popSubMenu(mnuSiteAdmin);
var mnuSystem = new menu("mnuSystem");
mnuSystem.bar[0] = new menu_bar("mnuSysUser","","用户管理","","用户管理","",1,"userAdmin.asp");
mnuSystem.bar[1] = new menu_bar("mnuSysAuthority","","权限管理","","权限管理","",1,"authority.asp");
mnuSystem.bar[2] = new menu_bar("\-","","","","","","","");
mnuSystem.bar[3] = new menu_bar("mnuSysPwdChange","","口令修改","","口令修改","",1,"chgpwd.asp");
mnuSystem.bar[4] = new menu_bar("\-","","","","","","","");
mnuSystem.bar[5] = new menu_bar("mnuSysHelp","","帮助","","帮助","",1,"help.asp");
mnuSystem.bar[6] = new menu_bar("mnuSysAbout","","关于本系统","","关于本系统","",1,"about.asp");
mnuSystem.bar[7] = new menu_bar("\-","","","","","","","");
mnuSystem.bar[8] = new menu_bar("mnuSysExit","","退出系统","","退出本系统","",1,"quit.asp");
popSubMenu(mnuSystem);
var mnuShow = new menu("mnuShow");
mnuShow.bar[0] = new menu_bar("mnuShow1","","演示菜单一","","Menu Show 1","",1,"menushow1.asp");
mnuShow.bar[1] = new menu_bar("mnuShow2","","演示菜单二","","Menu Show 2","",0,"mnuShow_2");
mnuShow.bar[2] = new menu_bar("mnuShow3","","演示菜单三","","Menu Show 3","",1,"menushow3.asp");
mnuShow.bar[3] = new menu_bar("\-","","","","","","","");
mnuShow.bar[4] = new menu_bar("mnuShow4","","演示菜单四","","Menu Show 4","",1,"menushow4.asp");
mnuShow.bar[5] = new menu_bar("mnuShow5","","演示菜单五","","Menu Show 5","",0,"mnuShow_2");
popSubMenu(mnuShow);
var mnuShow_2 = new menu("mnuShow_2");
mnuShow_2.bar[0] = new menu_bar("mnuShow21","","演示菜单二一","","Menu Show 2.1","",1,"menushow21.asp");
mnuShow_2.bar[1] = new menu_bar("mnuShow22","","演示菜单二二","","Menu Show 2.2","",0,"mnuShow_22");
mnuShow_2.bar[2] = new menu_bar("mnuShow23","","演示菜单二三","","Menu Show 2.3","",1,"menushow23.asp");
mnuShow_2.bar[3] = new menu_bar("mnuShow24","","演示菜单二四","","Menu Show 2.4","",0,"mnuShow_22");
mnuShow_2.bar[4] = new menu_bar("mnuShow25","","演示菜单二五","","Menu Show 2.5","",1,"menushow25.asp");
popSubMenu(mnuShow_2);
var mnuShow_22 = new menu("mnuShow_22");
mnuShow_22.bar[0] = new menu_bar("mnuShow221","","演示菜单二二一","","Menu Show 2.2.1","",1,"menushow221.asp");
mnuShow_22.bar[1] = new menu_bar("mnuShow222","","演示菜单二二二","","Menu Show 2.2.2","",1,"menushow222.asp");
mnuShow_22.bar[2] = new menu_bar("mnuShow223","","演示菜单二二三","","Menu Show 2.2.3","",1,"menushow223.asp");
popSubMenu(mnuShow_22);
//-->
</script>
<table border=0 cellpadding=0 cellspacing=0><tr><td height=27>此部分已经被顶部菜单条覆盖</td></tr></table>
<p id=lblOutput>信息提示区域</p>
</body>
</html>
xp_menu.css(样式表)
body {
FONT-SIZE: 13px;
MARGIN: 0px;
SCROLLBAR-SHADOW-COLOR: silver;
SCROLLBAR-3DLIGHT-COLOR: silver;
LINE-HEIGHT: 20px;
FONT-FAMILY: Verdana, 宋体;
SCROLLBAR-DARKSHADOW-COLOR: silver;
BACKGROUND-COLOR: #ff99cc;
border: none;
}
DIV.MainMenuBG
{
border-right: teal 1px solid;
font-size: 14px;
border-left: #ffffff 1px solid;
cursor: default;
color: #000000;
border-bottom: teal 1px solid;
font-family: Verdana, 宋体;
position: absolute;
background-color: #d6d3ce;
}
DIV.MainMenu
{
border-right: #d6d3ce 1px solid;
padding-right: 8px;
border-top: #d6d3ce 1px solid;
padding-left: 8px;
padding-bottom: 1px;
border-left: #d6d3ce 1px solid;
cursor: default;
padding-top: 1px;
border-bottom: #d6d3ce 1px solid;
position: absolute;
background-color: #d6d3ce;
}
DIV.MainMenuOver
{
border-right: #08246b 1px solid;
padding-right: 8px;
border-top: #08246b 1px solid;
padding-left: 8px;
padding-bottom: 1px;
border-left: #08246b 1px solid;
cursor: default;
padding-top: 1px;
border-bottom: #08246b 1px solid;
position: absolute;
background-color: #b5bed6;
}
DIV.MainMenuClick
{
border-right: #636563 1px solid;
padding-right: 8px;
border-top: #636563 1px solid;
padding-left: 8px;
padding-bottom: 1px;
border-left: #636563 1px solid;
cursor: default;
padding-top: 1px;
border-bottom: #636563 1px solid;
position: absolute;
background-color: #dedbd6;
}
DIV.SubMenuBG
{
border-right: #636563 1px solid;
border-top: #636563 1px solid;
font-size: 14px;
visibility: hidden;
border-left: #636563 1px solid;
color: #000000;
border-bottom: #636563 1px solid;
font-family: Verdana, 宋体;
position: absolute;
background-color: #fffbf7;
}
DIV.SubMenu
{
cursor: default;
position: absolute;
}
DIV.SubMenuOver
{
cursor: default;
position: absolute;
}
TD.ico
{
border-top: #dedbd6 1px solid;
border-left: #dedbd6 1px solid;
border-bottom: #dedbd6 1px solid;
background-color: #dedbd6;
}
TD.icoOver
{
border-top: #08246b 1px solid;
border-left: #08246b 1px solid;
border-bottom: #08246b 1px solid;
background-color: #b5bed6;
border-right-width: 1px;
border-right-color: #08246b;
}
TD.text
{
padding-right: 2px;
border-top: #fffbf7 1px solid;
padding-left: 5px;
border-bottom: #fffbf7 1px solid;
background-color: #fffbf7;
}
TD.textOver
{
padding-right: 2px;
border-top: #08246b 1px solid;
padding-left: 5px;
border-bottom: #08246b 1px solid;
background-color: #b5bed6;
}
TD.skey
{
padding-right: 2px;
border-top: #fffbf7 1px solid;
padding-left: 2px;
border-bottom: #fffbf7 1px solid;
background-color: #fffbf7;
}
TD.skeyOver
{
padding-right: 2px;
border-top: #08246b 1px solid;
padding-left: 2px;
border-bottom: #08246b 1px solid;
background-color: #b5bed6;
}
TD.sub
{
border-right: #fffbf7 1px solid;
border-top: #fffbf7 1px solid;
border-bottom: #fffbf7 1px solid;
background-color: #fffbf7;
}
TD.subOver
{
border-right: #08246b 1px solid;
border-top: #08246b 1px solid;
border-bottom: #08246b 1px solid;
background-color: #b5bed6;
}
DIV.SubMenu TD
{
font-size: 14px;
padding-top: 2px;
}
xp_menu.js(.js包含文件)
<!--
// menu source code
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 以下为建立菜单的部分
//_______________________________________
//菜单条的结构
function menu_bar(name,ico,text,key,tip,skey,type,cmd) {
this.name = name; //名称
this.ico = ico; //图标
this.text = text; //文字
this.key = key; //热键
this.tip = tip; //提示
this.skey = skey; //快捷键
this.type = type; //命令类型,0为弹出下级菜单,1为执行一个命令(跳转到另一个文件)
this.cmd = cmd; //下级菜单的名称或命令字符串,根据type的值而定
}
//菜单的结构
function menu(name) {
this.name = name; //名称
this.bar = new Array(); //所包含的菜单条数组
}
//输出一已定义的主菜单,横排的
function popMainMenu(menu,left,top,width,height) {
var strHTML, posLeft, posTop, i;
strHTML = "<div id='" + menu.name + "' style='left:" + left + ";top:" + top + ";width:" + width + ";height:" + height + "' class='MainMenuBG'>"
document.write (strHTML);
posLeft = 2;
posTop = 1;
for (i=0;i<menu.bar.length;i++) {
strHTML = "<div id='" + menu.bar[i].name + "' nowrap style='left:" + posLeft + ";top:" + posTop + ";' class='MainMenu' " +
"onmouseover='return menu_onmouseover(this,\"" + menu.bar[i].cmd + "\")' onmouseout='return menu_onmouseout(this)' " +
"onclick='return menu_onclick(this,\"" + menu.bar[i].cmd + "\")' title='" + menu.bar[i].tip + "'>" + menu.bar[i].text;
if (menu.bar[i].key!="") {
strHTML += "(<u>" + menu.bar[i].key + "</u>)";
}
strHTML += "</div>";
document.write (strHTML);
posLeft += document.all.item(menu.bar[i].name).offsetWidth + 1;
}
strHTML = "</div>";
document.write (strHTML);
}
//输出一个已定义的子菜单,竖排的
function popSubMenu(menu) {
var strHTML, posLeft, posTop, i;
strHTML = "<div id='" + menu.name + "' style='left:20;top:30;width:244;height:300;' class='SubMenuBG'>"
document.write (strHTML);
posLeft = 1;
posTop = 1;
for (i=0;i<menu.bar.length;i++) {
if (menu.bar[i].name!="\-") { //如果名称不是“\-”,表示是一个普通菜单条
strHTML = "<div id='" + menu.bar[i].name + "' style='left:" + posLeft + ";top:" + posTop + ";' class='SubMenu' " +
"title='" + menu.bar[i].tip + "' " +
"onmouseover='return bar_onmouseover(this," + menu.bar[i].type + ",\"" + menu.bar[i].cmd + "\")' " +
"onmouseout='return bar_onmouseout(this," + menu.bar[i].type + ",\"" + menu.bar[i].cmd + "\")' " +
"onclick='return bar_onclick(this," + menu.bar[i].type + ",\"" + menu.bar[i].cmd + "\")'>" +
" <table border=0 cellspacing=0 cellpadding=0 width=240>" +
" <tr>" +
" <td nowrap align=center width=20 id='" + menu.bar[i].name + "_ico' class='ico'>";
if (menu.bar[i].ico!="") {
strHTML += "<img border=0 width=16 height=16 src='" + menu.bar[i].ico + "'>";
}else {
strHTML += " ";
}
strHTML += "</td>" +
" <td nowrap id='" + menu.bar[i].name + "_text' class='text'>" + menu.bar[i].text;
if (menu.bar[i].key!="") { strHTML = strHTML + "(<u>" + menu.bar[i].key + "</u>)"; }
strHTML += "</td>" +
"<td nowrap align=right id='" + menu.bar[i].name + "_skey' class='skey'>";
if (menu.bar[i].skey!="") {
strHTML += menu.bar[i].skey;
}else {
strHTML += " ";
}
strHTML += " </td>" +
" <td nowrap align=center width=20 id='" + menu.bar[i].name + "_sub' class='sub'>";
if (menu.bar[i].type==0) {
strHTML += "→";
}else {
strHTML += " ";
}
strHTML += "</td>" +
" </tr>" +
" </table>" +
"</div>";
document.write (strHTML);
posTop += document.all.item(menu.bar[i].name).offsetHeight;
}else { //如果名称是“\-”,表示是一条分隔线
strHTML = "<div style='left:" + posLeft + ";top:" + posTop + ";' class='SubMenu'>" +
" <table border=0 cellpadding=0 cellspacing=0 width=240>" +
" <tr>" +
" <td nowrap style='background-color: #dedbd6;' width=21></td>" +
" <td nowrap style='padding-left:5px;padding-top:1px;padding-bottom:1px;'>" +
" <table border=0 cellpadding=0 cellspacing=0 width=100%>" +
" <tr><td height=1 bgcolor='#636563'></td></tr>" +
" </table>" +
" </td>" +
" </tr>" +
" </table>" +
"</div>";
document.write (strHTML);
posTop += 3;
}
}
strHTML = "</div>"
document.write (strHTML);
document.all.item(menu.name).style.height = posTop+3; //重设子菜单背景框
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 以下为操纵菜单的部分
//_________________________________________________
function showMenu(menu,submenu) { //显示一级子菜单
document.all.item(submenu).style.left = menu.parentElement.offsetLeft + menu.offsetLeft + 1; //设置子菜单位置
document.all.item(submenu).style.top = menu.parentElement.offsetTop + menu.offsetTop + menu.offsetHeight -1; //设置子菜单的位置
document.all.item(submenu).style.visibility = "visible"; //显示子菜单
mnuPrev[mnuPrev.length] = new menuinfo(submenu,1); //将新打开的子菜单的ID加入当前显示菜单ID数组
}
function showSubMenu(menu,submenu) { //显示二级及以下子菜单
document.all.item(submenu).style.left = menu.parentElement.offsetLeft + menu.offsetLeft + menu.offsetWidth + 2; //设置子菜单位置
document.all.item(submenu).style.top = menu.parentElement.offsetTop + menu.offsetTop + 1; //设置子菜单的位置
if (document.all.item(submenu).offsetLeft + document.all.item(submenu).offsetWidth > doc_width) {
document.all.item(submenu).style.left = menu.parentElement.offsetLeft - document.all.item(submenu).offsetWidth + 1;
}
if (document.all.item(submenu).offsetTop + document.all.item(submenu).offsetHeight > doc_height) {
document.all.item(submenu).style.top = document.all.item(submenu).offsetTop + menu.offsetHeight - document.all.item(submenu).offsetHeight + 2;
}
document.all.item(submenu).style.visibility = "visible"; //显示子菜单
mnuPrev[mnuPrev.length] = new menuinfo(submenu,1); //将新打开的子菜单的ID加入当前显示菜单ID数组
}
function showstatus() { //测试阶段用的函数,显示当前菜单ID数组内的内容
lblMsg.innerText = "";
for(var i=0;i<mnuPrev.length;i++) {
lblMsg.innerText += mnuPrev[i].id + " > ";
}
}
function menuinfo(id,type) {
this.id = id; //0表示菜单项,1表示菜单框
this.type = type;
}
function menu_onmouseover(obj,cmd) {
if(!mClick) {
obj.className = "MainMenuOver";
mnuPrev[mnuPrev.length] = new menuinfo(obj.id,0); //将当前的菜单ID压入当前菜单ID数组
//showstatus(); //调试阶段使用
}else {
if (mnuPrev[0].id == obj.id) { return false; } //如果是在本身上移动,不作为
document.all.item(mnuPrev[0].id).className = "MainMenu";
for (var i=1;i<mnuPrev.length;i++) {
if (mnuPrev[i].type==0) { //如果是菜单项,设置成默认样式
document.all.item(mnuPrev[i].id + "_ico").className = "ico";
document.all.item(mnuPrev[i].id + "_text").className = "text";
document.all.item(mnuPrev[i].id + "_skey").className = "skey";
document.all.item(mnuPrev[i].id + "_sub").className = "sub";
}else { //如果是菜单框,隐藏它
document.all.item(mnuPrev[i].id).style.visibility = "hidden";
}
}
mnuPrev.length = 0;
obj.className = "MainMenuClick";
mnuPrev[mnuPrev.length] = new menuinfo(obj.id,0);
showMenu(obj,cmd);
//showstatus(); //调试阶段使用
}
}
function menu_onmouseout(obj) {
if(!mClick) {
obj.className = "MainMenu";
mnuPrev.length --; //清除最后一个压入当前菜单ID数组的菜单ID
//showstatus(); //调试阶段使用
}
}
function menu_onclick(obj,cmd) {
if(!mClick) { //如果菜单当前状态是非点击状态,证明当前是从非点击状态到点击状态
obj.className = "MainMenuClick"; //设置当前菜单为鼠标单击之后的状态
showMenu(obj,cmd); //显示子菜单
//showstatus(); //调试阶段使用
mClick = true; //设置菜单当前是点击状态
}else {
obj.className = "MainMenuOver"; //设置当前菜单为鼠标移动上面的状态,非点击状态
document.all.item(mnuPrev[mnuPrev.length-1].id).style.visibility = "hidden";
mnuPrev.length --;
//showstatus(); //调试阶段使用
mClick = false; //主菜单当前是非点击状态
}
bMenuClick = true; //在一个菜单上单击后设置其状态为true,用来屏蔽document_click事件
}
function bar_onmouseover(obj,type,cmd) {
for (var i=1;i<mnuPrev.length;i++) {
if(mnuPrev[i].id==obj.parentElement.id) {
for (var j=i+1;j<mnuPrev.length;j++) { //处理由前一个菜单项打开的子菜单,可能有多层
if (mnuPrev[j].type==0) { //如果是菜单项,设置成默认样式
document.all.item(mnuPrev[j].id + "_ico").className = "ico";
document.all.item(mnuPrev[j].id + "_text").className = "text";
document.all.item(mnuPrev[j].id + "_skey").className = "skey";
document.all.item(mnuPrev[j].id + "_sub").className = "sub";
}else { //如果是菜单框,隐藏它
document.all.item(mnuPrev[j].id).style.visibility = "hidden";
}
}
mnuPrev.length = i + 1;
break;
}
}
mnuPrev[mnuPrev.length] = new menuinfo(obj.id,0);
//showstatus(); //调试阶段使用
document.all.item(obj.id + "_ico").className = "icoOver";
document.all.item(obj.id + "_text").className = "textOver";
document.all.item(obj.id + "_skey").className = "skeyOver";
document.all.item(obj.id + "_sub").className = "subOver";
if (type==0) { showSubMenu(obj,cmd); } //如果有下一级菜单就展开
//showstatus(); //调试阶段使用
}
function bar_onmouseout(obj,type,cmd) {
if (type!=0) {
document.all.item(obj.id + "_ico").className = "ico";
document.all.item(obj.id + "_text").className = "text";
document.all.item(obj.id + "_skey").className = "skey";
document.all.item(obj.id + "_sub").className = "sub";
if (mnuPrev.length>0) {
mnuPrev.length --;
}
//showstatus(); //调试阶段使用
}
}
function bar_onclick(obj,type,cmd) {
if (type==1) {
//window.navigate(cmd); //跳转到相应的文件
lblOutput.innerText = cmd; //演示期间,将文件名显示出来
}else {
bMenuClick = true; //在一个菜单上单击后设置其状态为true,用来屏蔽document_click事件
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 以下为与主文件有关的控制部分,具体使用时需要关注的也仅仅这部分而已
//_____________________________________________________________
var doc_width; //当前窗口的宽度,随窗口大小改变而改变
var doc_height; //当前窗口的高度,随窗口大小改变而改变
var mClick = false; //一级菜单是否被点击,初始一级菜单是未被点击状态
var bMenuClick = false; //单击是否是在一个菜单上,初始单击不是在一个菜单上
var mnuPrev = new Array(); //当前显示的菜单ID数组,初始为空
function document_onselectstart() {
return false; //屏蔽内容选择功能
}
function document_onclick() {
if(!bMenuClick) {
if (mnuPrev.length>0) {
for (var i=1;i<mnuPrev.length;i++) {
if (mnuPrev[i].type==0) { //如果是菜单项,设置成默认样式
document.all.item(mnuPrev[i].id + "_ico").className = "ico";
document.all.item(mnuPrev[i].id + "_text").className = "text";
document.all.item(mnuPrev[i].id + "_skey").className = "skey";
document.all.item(mnuPrev[i].id + "_sub").className = "sub";
}else { //如果是菜单框,隐藏它
document.all.item(mnuPrev[i].id).style.visibility = "hidden";
}
}
document.all.item(mnuPrev[0].id).className = "MainMenu"; //将主菜单的状态恢复普通状态
}
mnuPrev.length = 0; //清空当前显示菜单
mClick = false; //清除菜单点击状态
}else {
bMenuClick = false;
}
}
function window_onresize() {
//当窗口尺寸变化时改变相应变量的值
doc_height = window.document.body.offsetHeight;
doc_width = window.document.body.offsetWidth;
}
function window_onload() {
//初始化窗口尺寸
doc_height = window.document.body.offsetHeight;
doc_width = window.document.body.offsetWidth;
}
//-->