仿微软的下拉式菜单(Javascript)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<script language=javascript>
var mmenus = new Array();
var misShow = new Boolean();
misShow=false;
var misdown = new Boolean();
misdown=false;
var msrcel;
var mfromel;
var mtoel;
var musestatus=false;
var mthestatus="";
var mpopTimer = 0;
mmenucolor='#FFFFFF';mfontcolor='#000000';mmenuoutcolor='#009AFF';
mmenuincolor='#009AFF';mmenuoutbordercolor='#009AFF';mmenuinbordercolor='#009AFF';
mmidoutcolor='#0099FF';mmidincolor='#0099FF';mmenuovercolor='#FF0000';mitemedge='0';
msubedge='1';
mmenuunitwidth=60;
mmenuitemwidth=160;
mmenuheight=22;
mmenuwidth='500';
mmenuadjust=10;
mmenuadjustV=0;
mfonts='font-family: Arial; font-size: 9pt; color: #000000; ';
mcursor='default';
function stoperror()
{
return true;
}
window.onerror=stoperror;
function mpopOut() {
mpopTimer = setTimeout('mallhide()', 500);
}
function getReal(el, type, value) {
temp = el;
while ((temp != null) && (temp.tagName != "BODY"))
{
if (eval("temp." + type) == value) {
el = temp;
return el;
}
temp = temp.parentElement;
}
return el;
}
function mMenuRegister(menu)
{
mmenus[mmenus.length] = menu
return (mmenus.length - 1)
}
function mMenuItem(caption,command,target,isline,statustxt,img,sizex,sizey,pos){
this.caption=caption;
this.command=command;
this.target=target;
this.isline=isline;
this.statustxt=statustxt;
this.img=img;
this.sizex=sizex;
this.sizey=sizey;
this.pos=pos;
}
function mMenu(caption,command,target,img,sizex,sizey,pos){
this.items = new Array();
this.caption=caption;
this.command=command;
this.target=target;
this.img=img;
this.sizex=sizex;
this.sizey=sizey;
this.pos=pos;
this.id=mMenuRegister(this);
}
function mMenuAddItem(item)
{
this.items[this.items.length] = item
item.parent = this.id;
this.children=true;
}
mMenu.prototype.addItem = mMenuAddItem;
function mtoout(src){
src.style.borderLeftColor=mmenuoutbordercolor;
src.style.borderRightColor=mmenuinbordercolor;
src.style.borderTopColor=mmenuoutbordercolor;
src.style.borderBottomColor=mmenuinbordercolor;
src.style.backgroundColor=mmenuoutcolor;
src.style.color=mmenuovercolor;
}
function mtoin(src){
src.style.borderLeftColor=mmenuinbordercolor;
src.style.borderRightColor=mmenuoutbordercolor;
src.style.borderTopColor=mmenuinbordercolor;
src.style.borderBottomColor=mmenuoutbordercolor;
src.style.backgroundColor=mmenuincolor;
src.style.color=mmenuovercolor;
}
function mnochange(src){
src.style.borderLeftColor=mmenucolor;
src.style.borderRightColor=mmenucolor;
src.style.borderTopColor=mmenucolor;
src.style.borderBottomColor=mmenucolor;
src.style.backgroundColor='';
src.style.color=mfontcolor;
}
function mallhide(){
for(var nummenu=0;nummenu<mmenus.length;nummenu++)
{
var themenu=document.all['mMenu'+nummenu]
var themenudiv=document.all['mmenudiv'+nummenu]
mnochange(themenu);
mmenuhide(themenudiv);
}
}
function mmenuhide(menuid){
menuid.style.filter='Alpha(Opacity=100)';
menuid.style.visibility='hidden';
misShow=false;
}
function mmenushow(menuid,pid){
menuid.style.filter='Alpha(Opacity=80)';
menuid.style.left=mposflag.offsetLeft+pid.offsetLeft+mmenuadjust;
menuid.style.top=mposflag.offsetTop+mmenutable.offsetHeight+mmenuadjustV+1;
if(mmenuitemwidth+parseInt(menuid.style.left)>document.body.clientWidth)
menuid.style.left=document.body.clientWidth-mmenuitemwidth;
menuid.style.visibility='visible';
misShow=true;
}
function mmenu_over(menuid,x){
mtoel = getReal(window.event.toElement, "className", "coolButton");
mfromel = getReal(window.event.fromElement, "className", "coolButton");
if (mtoel == mfromel)
return;
msrcel = window.event.srcElement;
if(x<0){
misShow = false;
mallhide();
mtoout(msrcel);
}else{
mallhide();
mtoin(msrcel);
mmenushow(menuid,eval("mMenu"+x));
}
clearTimeout(mpopTimer);
}
function mmenu_out(menuid){
mtoel = getReal(window.event.toElement, "className", "coolButton");
mfromel = getReal(window.event.fromElement, "className", "coolButton");
if (mtoel == mfromel)
return;
msrcel = window.event.srcElement;
if (misShow){
mtoin(msrcel);
}else{
mnochange(msrcel);
}
mpopOut()
}
function mmenu_down(menuid,x){
msrcel = getReal(window.event.srcElement, "className", "coolButton");
if(misShow){
mmenuhide(menuid);
mtoout(msrcel);
}
else{
mtoin(msrcel);
mmenushow(menuid,eval("mMenu"+x));
misdown=true;
}
}
function mmenu_up(){
misdown=false;
}
function mmenuitem_over(menuid){
msrcel = getReal(window.event.srcElement, "className", "coolButton");
if(misdown){
mtoin(msrcel);
}
else{
mtoout(msrcel);
}
if(mthestatus!=""){
musestatus=true;
window.status=mthestatus;
}
clearTimeout(mpopTimer);
}
function mmenuitem_out(menuid){
msrcel = getReal(window.event.srcElement, "className", "coolButton");
mnochange(msrcel);
if(musestatus)window.status="";
mpopOut()
}
function mmenuitem_down(menuid){
msrcel = getReal(window.event.srcElement, "className", "coolButton");
mtoin(msrcel)
misdown=true;
}
function mmenuitem_up(menuid){
msrcel = getReal(window.event.srcElement, "className", "coolButton");
mtoout(msrcel)
misdown=false;
}
function mexec2(x){
var cmd;
if(mmenus[x].target=="blank"){
cmd = "window.open('"+mmenus[x].command+"')";
}else{
cmd = mmenus[x].target+".location='"+mmenus[x].command+"'";
}
eval(cmd);
}
function mexec(x,i){
var cmd;
if(mmenus[x].items[i].target=="blank"){
cmd = "window.open('"+mmenus[x].items[i].command+"')";
}else{
cmd = mmenus[x].items[i].target+".location='"+mmenus[x].items[i].command+"'";
}
eval(cmd);
}
function mbody_click(){
if (misShow)
{
msrcel = getReal(window.event.srcElement, "className", "coolButton");
for(var x=0;x<=mmenus.length;x++)
{
if(msrcel.id=="mMenu"+x)
return;
}
mallhide();
}
}
document.onclick=mbody_click;
var mMenuBodyRef;
function mwritetodocument()
{
var mwb=1;
for(var i in document.all)
{
if (document.all[i].tagName == 'BODY')
{
mMenuBodyRef = document.all[i]
var stringx='<div id="mposflag" style="position:absolute;"></div><table border=1 cellpand=0 cellspacing=0 width=1004 bgcolor='+mmenucolor+' style="filter:Alpha(Opacity=80);cursor:'+mcursor+';'+mfonts+
' border-left: '+mwb+'px solid '+mmenuoutbordercolor+';'+
' border-right: '+mwb+'px solid '+mmenuinbordercolor+'; '+
'border-top: '+mwb+'px solid '+mmenuoutbordercolor+'; '+
'border-bottom: '+mwb+'px solid '+mmenuinbordercolor+'; padding:0px"><tr><td>'
stringx+='<table id=mmenutable border=0 cellpadding=0 cellspacing=0 width='+mmenuwidth+' height='+mmenuheight+' bgcolor='+mmenucolor+
' onselectstart="event.returnValue=false"'+
' style="filter:Alpha(Opacity=80);cursor:'+mcursor+';'+mfonts
//' border-left: '+mwb+'px solid '+mmenuoutbordercolor+';'+
//' border-right: '+mwb+'px solid '+mmenuinbordercolor+'; '+
//'border-top: '+mwb+'px solid '+mmenuoutbordercolor+'; '+
//'border-bottom: '+mwb+'px solid '+mmenuinbordercolor+'; '
stringx+='padding:0px"><tr>'
for(var x=0;x<mmenus.length;x++)
{
var thismenu=mmenus[x];
var imgsize;
if(thismenu.sizex=="0"&&thismenu.sizey=="0")
{
imgsize="";
}
else
{
imgsize=" width="+thismenu.sizex+" height="+thismenu.sizey;
}
var ifspace;
if(thismenu.caption=="")
{
ifspace="";
}else{
ifspace=" ";
}
stringx += "<td class=coolButton id=mMenu"+x+" style='border: "+mitemedge+"px solid "+mmenucolor+
"' width="+mmenuunitwidth+"px onmouseover=mmenu_over(mmenudiv"+x+
","+x+") onmouseout=mmenu_out(mmenudiv"+x+
") onmousedown=mmenu_down(mmenudiv"+x+","+x+")";
if(thismenu.command!=""){
stringx += " onmouseup=mexec2("+x+");mmenu_up();";
}else{
stringx += " onmouseup=mmenu_up()";
}
if(thismenu.pos=="0"){
stringx += " align=center><img align=absmiddle src="+thismenu.img+imgsize+">"+ifspace+thismenu.caption+"</td>";
}else if(thismenu.pos=="1"){
stringx += " align=center>"+thismenu.caption+ifspace+"<img align=absmiddle src="+thismenu.img+imgsize+"></td>";
}else if(thismenu.pos=="2"){
stringx += " align=center background="+thismenu.img+">"+thismenu.caption+"</td>";
}else{
stringx += " align=center>"+thismenu.caption+"</td>";
}
stringx += "";
}
stringx+="<td width=*> </td></tr></table>";
for(var x=0;x<mmenus.length;x++)
{
thismenu=mmenus[x];
if(x<0){
stringx+='<div id=mmenudiv'+x+' style="visiable:none"></div>';
}else{
stringx+='<div id=mmenudiv'+x+
' style="filter:Alpha(Opacity=80);cursor:'+mcursor+';position:absolute;'+
'width:'+mmenuitemwidth+'px; z-index:'+(x+100);
if(mmenuinbordercolor!=mmenuoutbordercolor&&msubedge=="0")
{
stringx+=';border-left: 1px solid '+mmidoutcolor+
';border-top: 1px solid '+mmidoutcolor;}
stringx+=';border-right: 1px solid '+mmenuinbordercolor+
';border-bottom: 1px solid '+mmenuinbordercolor+';visibility:hidden" onselectstart="event.returnValue=false">\n'+
'<table width="100%" border="0" height="100%" align="center" cellpadding="0" cellspacing="2" '+
'style="'+mfonts+' border-left: 1px solid '+mmenuoutbordercolor;
if(mmenuinbordercolor!=mmenuoutbordercolor&&msubedge=="0")
{
stringx+=';border-right: 1px solid '+mmidincolor+';border-bottom: 1px solid '+mmidincolor;
}
stringx+=';border-top: 1px solid '+mmenuoutbordercolor+';padding: 4px" bgcolor='+mmenucolor+'>\n'
for(var i=0;i<thismenu.items.length;i++)
{
var thismenuitem=thismenu.items[i];
var imgsize;
if(thismenuitem.sizex=="0"&&thismenuitem.sizey=="0")
{
imgsize=""
}else{
imgsize=" width="+thismenuitem.sizex+" height="+thismenuitem.sizey
}
var ifspace;
if(thismenu.caption==""){
ifspace="";
}else{
ifspace=" ";
}
if(!thismenuitem.isline)
{
stringx += "<tr><td class=coolButton style='border: "+mitemedge+"px solid "+mmenucolor+"' width=100% height=15px onmouseover=\"mthestatus='"+thismenuitem.statustxt+"';mmenuitem_over(mmenudiv"+x+");\" onmouseout=mmenuitem_out(mmenudiv"+x+") onmousedown=mmenuitem_down(mmenudiv"+x+") onmouseup=";
stringx += "mexec("+x+","+i+");mmenuitem_up(mmenudiv"+x+") ";
if(thismenuitem.pos=="0")
{
stringx += "><img align=absmiddle src="+thismenuitem.img+imgsize+">"+ifspace+thismenuitem.caption+"</td></tr>";
}else if(thismenuitem.pos=="1"){
stringx += ">"+thismenuitem.caption+ifspace+"<img align=absmiddle src="+thismenuitem.img+imgsize+"></td></tr>";
}else if(thismenuitem.pos=="2"){
stringx += "background="+thismenuitem.img+">"+thismenuitem.caption+"</td></tr>";
}else{
stringx += ">"+thismenuitem.caption+"</td></tr>";
}
}else{
stringx+='<tr><td height="1" background="hr.gif"><img border="0" src="none.gif" width="1" height="1"></td></tr>\n';
}
}
stringx+='</table></td></tr></table>\n</div>'
}
}
document.write("<div align='left'>"+stringx+"</div>");
break
}
}
}
function document_load(){
mwritetodocument();
}
mpmenu1=new mMenu('File','','self','','','','');
mpmenu1.addItem(new mMenuItem('New File','javascript:alert("New File")','self',false,'New File','','','',''));
mpmenu1.addItem(new mMenuItem('Open','javascript:alert("Open")','self',false,'Open','','','',''));
mpmenu1.addItem(new mMenuItem('Save','javascript:alert("Save")','self',false,'Save','','','',''));
mpmenu1.addItem(new mMenuItem(null,null,null,true));
mpmenu1.addItem(new mMenuItem('Exit','javascript:alert("Exit")','self',false,'Exit','','','',''));
mpmenu2=new mMenu('Edit','','self','','','','');
mpmenu2.addItem(new mMenuItem('Undo','javascript:alert("Undo")','self',false,'Undo','','','',''));
mpmenu2.addItem(new mMenuItem('ReUndo','javascript:alert("ReUndo")','self',false,'ReUndo','','','',''));
mpmenu2.addItem(new mMenuItem(null,null,null,true));
mpmenu2.addItem(new mMenuItem('Copy','javascript:alert("Copy")','self',false,'Copy','','','',''));
mpmenu3=new mMenu('History','','self','','','','');
mpmenu3.addItem(new mMenuItem('My WebSite','http://www.tuqiu.com','blank',false,'My WebSite','','','',''));
mpmenu3.addItem(new mMenuItem('Yahoo','http://www.yahoo.com','blank',false,'Yahoo','','','',''));
mpmenu4=new mMenu('Help','','self','','','','');
mpmenu4.addItem(new mMenuItem('Content','javascript:alert("Content")','self',false,'Content','','','',''));
mpmenu4.addItem(new mMenuItem('Email','mailto:webmaster@tastelife.net','self',false,'Email','','','',''));
mpmenu4.addItem(new mMenuItem(null,null,null,true));
mpmenu4.addItem(new mMenuItem('About','javascript:alert("About")','self',false,'About','','','',''));
</SCRIPT>
</head>
<body leftmargin=0>
<SCRIPT>document_load()</SCRIPT>
</body>