1<html>
2<head>
3<title>AgetimeMenu Demo</title>
4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
5<style>
6.agetime_bar{
7position:absolute;top:0px;left:0px;height:22px;width:100%;border:1px outset;background-color:RGB(212,208,200);z-index:98;
8}
9.agetime_barItem{
10width:60px;height:20px;border:1px solid RGB(212,208,200);text-align:left;padding-left:10px;
11background:RGB(212,208,200);color:#000000;font-size:9pt;
12}
13.agetime_barItemDown{
14width:60px;height:20px;border:1px inset RGB(212,208,200);text-align:left;padding-left:10px;
15background:#F0F0F0;color:#000000;font-size:9pt;
16}
17.agetime_barItemHover{
18width:60px;height:20px;border:1 outset;text-align:left;padding-left:10px;
19background:#F0F0F0;color:#000000;font-size:9pt;
20}
21.agetime_pad{
22cursor:default;font-size:9pt;width:100%;
23}
24.agetime_padItem{
25width:100%;height:18px;border:1px solid RGB(212,208,200);text-align:left;padding-left:10px;
26background:RGB(212,208,200);color:#000000;font-size:9pt;
27}
28.agetime_padItemFalse{
29padding-left:10px;font-size:9pt; color:#808080;
30}
31.agetime_padItemFalseHover{
32padding-left:10px;font-size:9pt; color:#808080;background-color:#333366;
33}
34.agetime_padItemHover{
35width:100%;height:18px;text-align:left;padding-left:10px;
36background-color:#333366;color:#FFFFFF;font-size:9pt;
37}
38.agetime_padItemDown{
39width:100%;height:18px;text-align:left;padding-left:10px;border:1px inset;
40background-color:#9999CC;color:#FFFFFF;font-size:9pt;
41}
42.agetime_hr{
43border:1px inset;
44}
45.agetime_board{
46background-color:RGB(212,208,200);border:2px outset;
47}
48</style>
49</head>
50<body>
51<script language="javascript">
52var menu = agetimeMenu("agetime",
53[
54[
55["文件",null,null,true,"打开文件"], //显示文字,方法,命令,状态,状栏显示文字
56["打开",null,null,false,"打开文件"],
57["--"],
58["你好","js","alert('Hello')",true,"一声问候"],
59["新窗口","ABC","about:blank",true,"弹出ABC窗口"],
60["空白",null,"about:blank",true,"在当前窗口显示空白页"]
61],
62[
63["编辑",null,null,false,"打开文件"],
64["撤消",null,null,true,"打开文件"],
65["重做",null,null,true,"打开文件"]
66],
67[
68["文件","js","alert('无子菜单')",true,"打开文件"]
69]
70]
71);
72//方法为"js"时,命令则为javascript语句,为非"js"值时,命令则是一个URL,而打开这个URL的目标位置则是方法所指定的窗口;
73//["你好","js","alert('Hello'),true,"一声问候"];
74//显示文字为"--"是按钮是一个分隔符;
75
76function agetimeMenu(id,array){
77var menu=this;
78menu.pad=null; //装载各个子菜单
79menu.barItems=[]; //菜单条的各位按钮
80menu.pads=[]; //每个子菜单为一个table存放于menu.pad上;
81menu.selectedIndex=-1; //菜单条选中按钮的索引值
82menu.board=null; //子菜单面板
83
84//建立菜单条
85this.crtMenuBar=function(){
86var len=array.length;
87menu.bar = document.body.appendChild(document.createElement('div'));
88menu.bar.className=id+"_bar";
89for(var i=0;i<len;i++){
90menu.barItems[i]=menu.addMenuBarItem(array[i][0],i);
91menu.addMenuPad(array[i],i);
92}
93}
94
95//子菜单
96this.addMenuPad=function(ary,index){
97var len=ary.length;
98var pad=menu.crtElement("table",menu.pad);
99pad.cellSpacing=1; pad.cellPadding=0;
100pad.className=id+"_pad";
101pad.style.display="none";
102for(var i=1;i<len;i++){
103var Row=pad.insertRow(i-1);
104menu.addMenuPadItem(ary[i],Row);
105}
106menu.pads[index]=pad;
107}
108
109//各子菜单按钮
110this.addMenuPadItem=function(ary,Row){
111var Cell=Row.insertCell(0);
112if(ary[0]!="--"){
113Cell.innerText=ary[0];
114if(ary[3]){ //有效状态;
115Cell.className=id+"_padItem";
116Cell.onmouseover=function(){
117Cell.className=id+"_padItemHover";
118window.status=ary[4];
119}
120Cell.onmouseout=function(){
121Cell.className=id+"_padItem";
122window.status="";
123}
124Cell.onmousedown=function(){ Cell.className=id+"_padItemDown"; }
125Cell.onmouseup=function(){
126Cell.className=id+"_padItemHover";
127menu.hideMenu();
128menu.execute(ary);
129}
130}
131else{ //按钮无效;
132Cell.className=id+"_padItemFalse";
133Cell.onmouseover=function(){
134Cell.className=id+"_padItemFalseHover";
135window.status=ary[4];
136}
137Cell.onmouseout=function(){
138Cell.className=id+"_padItemFalse";
139window.status="";
140}
141}
142}
143else{
144var hr=menu.crtElement("hr",Cell);
145hr.className=id+"_hr";
146}
147Cell.onclick=function(){
148event.cancelBubble=true;
149}
150}
151
152//菜单条的按钮
153this.addMenuBarItem=function(ary,index){
154var item=menu.crtElement("button",menu.bar);
155item.value=ary[0];
156item.disabled=!ary[3];
157item.className=id+"_barItem";
158item.onmouseover=function(){
159if(menu.selectedIndex==-1){
160item.className=id+"_barItemHover";
161}
162else{
163menu.barItems[selectedIndex].className=id+"_barItem";
164item.className=id+"_barItemDown";
165menu.showMenu(index);
166}
167window.status=ary[4];
168}
169item.onmouseout=function(){
170if(menu.selectedIndex==-1) item.className=id+"_barItem";
171window.status="";
172}
173item.onclick=function(){
174event.cancelBubble=true;
175if(menu.selectedIndex==-1){
176item.className=id+"_barItemDown";
177menu.showMenu(index);
178}
179else{
180menu.hideMenu();
181item.className=id+"_barItemHover";
182}
183menu.execute(ary);
184item.blur();
185}
186return item;
187}
188
189//显示子菜单
190this.showMenu=function(index){
191if(menu.selectedIndex!=-1) menu.pads[selectedIndex].style.display="none";
192menu.board.style.pixelLeft=menu.barItems[index].offsetLeft+2;
193//menu.board.style.pixelHeight="";
194if(menu.pads[index].rows.length>0) menu.board.style.display="";
195else menu.board.style.display="none";
196menu.pads[index].style.display="";
197menu.selectedIndex=index;
198}
199//隐藏子菜单
200this.hideMenu=function(){
201if(menu.selectedIndex==-1) return;
202menu.barItems[menu.selectedIndex].className=id+"_barItem";
203menu.pads[selectedIndex].style.display="none";
204menu.selectedIndex=-1;
205menu.board.style.display="none";
206}
207
208//执行菜单命令;
209this.execute=function(ary){
210if(ary[2]==null) return;
211if(ary[1]=="js") { eval(ary[2]); menu.hideMenu(); }
212else if(ary[1]==null || ary[1].toLowerCase=="_self") location.href=ary[2];
213else{ var x=window.open(ary[2],ary[1]); x.focus(); }
214}
215
216//建立子菜单的显示面板
217this.crtMenuBoard=function(){
218document.write(
219"<div id='"+id+"_board' style='position:absolute;width:160px;height:10px;left:0px;top:20px;background-color:#666666;z-index:99;display:none;'>"+
220"<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;'>"+
221"<iframe id='"+id+"_frame' name='"+id+"_frame' width='100%' height='100%' frameborder='0' scrolling='no'></iframe>"+
222"</div>"+
223"<div id='"+id+"_pad' style='position:absolute;width:100%;height:100%;left:0px;top:0px;'></div>"+
224"</div>"
225);
226menu.board=document.getElementById(id+"_board");
227menu.pad=document.getElementById(id+"_pad");
228menu.pad.className=id+"_board";
229menu.pad.onselectstart=function(){ return false;}
230}
231
232//增加对像的一个子元素
233this.crtElement=function(el,p){
234return p.appendChild(document.createElement(el));
235}
236
237//安装菜单;
238this.setup=function(){
239menu.crtMenuBoard();
240menu.crtMenuBar();
241document.attachEvent("onclick",menu.hideMenu);
242}
243
244menu.setup();
245}
246</script>
247</body>
248</html>
一个简单的javascript菜单
comments powered by Disqus