XIAO牛
----在雨中长大
各位做Web开发的朋友对于固定的网页菜单大多比较熟悉,网上资料也比较多,可是我发现对于右键弹出式菜单的介绍却少的可怜。在这里我介绍一个应用在刚做的项目中的弹出菜单。它是使用htc实现的。
 
  1  <html xmlns:hGui>
  2  <!--Content of sMenu.htc -->
  3      <head>
  4          <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  5          <public:component tagName="sMenu">
  6              <public:attach event="oncontentready" onevent="OnContentReady()" />
  7              <public:attach event="ondocumentready" onevent="init()" />
  8              <public:attach event="ondeactivate" onevent="element_OnDeActivate()" />
  9              <public:method name="show" />
 10              <public:method name="buildMenu" />
 11              <public:event name="onmenumousedown" id="idMenuMouseDown" />
 12              <public:event name="onmenuclick" id="idMenuClick" />
 13              <public:event name="onbeforepopup" id="idMenuBeforePopup" />
 14              <public:event name="onhidden" id="idHidden" />
 15              <PUBLIC:DEFAULTS viewLinkContent />
 16          </public:component>
 17           <style type="text/css">
 18                  <!--
 19  body
 20   {
 21  }
 22  .Panel
 23   {
 24      BORDER-RIGHT: #666666 1px solid;
 25      PADDING-RIGHT: 0px;
 26      BORDER-TOP: #666666 1px solid;
 27      PADDING-LEFT: 0px;
 28      FONT-SIZE: 9pt;
 29      PADDING-BOTTOM: 0px;
 30      MARGIN: 0px;
 31      BORDER-LEFT: #666666 1px solid;
 32      COLOR: menutext;
 33      PADDING-TOP: 0px;
 34      BORDER-BOTTOM: #666666 1px solid;
 35      FONT-FAMILY: SimSun;
 36      POSITION: absolute;
 37      BACKGROUND-COLOR: #f9f8f7;
 38      filter:progid:DXImageTransform.Microsoft.Shadow(color='dimGray', Direction=135, Strength=2);
 39  }
 40  span.img
 41   {
 42      filter:progid:DXImageTransform.Microsoft.Alpha( Opacity=100, FinishOpacity=50, Style=2, StartX=0,  FinishX=100, StartY=0, FinishY=100);
 43  }
 44  .clsHSpacer
 45   {
 46      BORDER-RIGHT: medium none;
 47      PADDING-RIGHT: 0px;
 48      BORDER-TOP: buttonshadow 1px solid;
 49      PADDING-LEFT: 0px;
 50      FONT-SIZE: 1px;
 51      PADDING-BOTTOM: 2px;
 52      MARGIN: 2px 0px 3px;
 53      OVERFLOW: hidden;
 54      BORDER-LEFT: medium none;
 55      WIDTH: 100%;
 56      PADDING-TOP: 2px;
 57      BORDER-BOTTOM: buttonhighlight 1px solid;
 58      POSITION: relative;
 59      HEIGHT: 2px;
 60      BACKGROUND-COLOR: red
 61  }
 62  --></style>
 63           <script language="jscript">
 64          <!--
 65          var sMI,sMenu;
 66          var lightTr;
 67          var showSubMenu=null;
 68          
 69          function OnContentReady()
 70           {
 71              element.style.visibility="hidden";
 72              element.style.position="absolute";
 73              ctrBody.style.visibility="visible";
 74          }
 75  
 76          function createImgInTD(strSrc, td)
 77           {
 78              var span = document.createElement("span");
 79  
 80              with (span.style)
 81               {
 82                  width = "20px";
 83                  height = "16px";
 84                  backgroundImage = "url(" + strSrc + ")";
 85                  backgroundPosition = "center center";
 86                  backgroundRepeat = "no-repeat";
 87                  verticalAlign = "baseline";
 88              }
 89              
 90              td.appendChild(span);
 91          }
 92  
 93          function createSeperatorTD(td, tr)
 94           {
 95              td.style.height="5px";
 96  
 97              td = tr.insertCell();
 98              td.style.height="5px";
 99              td.colSpan=3;
100              td.innerHTML = "<div class='clsHSpacer'></div>";
101          }
102  
103          function buildMenu(arrayItems)
104           {
105              while(tableMenu.rows.length > 1)
106               {
107                  tableMenu.deleteRow(1);
108              }
109  
110              var i;
111  
112              sMI = arrayItems;
113              sMenu = new Array(sMI.length);
114  
115              for(i = 0; i < sMI.length; i++)
116               {
117                  sMenu[i] = sMI[i].split(",");
118              }
119  
120              var Tr, Td;
121  
122              for(i = 0; i < sMenu.length; i++)
123               {
124                  if (sMenu[i][0].length > 0)
125                   {
126                      Tr = tableMenu.insertRow();
127                      Td = Tr.insertCell();
128                      
129                      sMenu[i][0= rTrim(lTrim(sMenu[i][0]));
130                      
131                      Td.innerText = "";
132                      Td.style.width = "20px";
133                      Td.vAlign = "middle";
134      
135                      if (sMenu[i].length > 4)
136                          if (sMenu[i][4].length > 0)
137                              createImgInTD(sMenu[i][4], Td);
138  
139                      Td.style.backgroundColor = "#dbd8d1";
140  
141                      if(sMenu[i][0== "-")
142                          createSeperatorTD(Td, Tr);
143                      else
144                       {
145                          Td.style.height = 20;
146  
147                          Td = Tr.insertCell();
148                          Td.vAlign = "middle";
149                          
150                          var span = document.createElement("span");
151                          span.innerHTML = sMenu[i][0];
152                          span.style.marginLeft = "4px";
153                          Td.appendChild(span);
154                          Td.span = span;
155                          Td.noWrap = true;
156      
157                          if (Td.offsetWidth < 100)
158                              Td.style.width = 100;
159                          
160                          Td=Tr.insertCell();
161                          Td.style.width=15;
162      
163                          if(sMenu[i][1]) 
164                           {
165                              if(element.document.all(sMenu[i][1]))
166                               {
167                                  Tr.subMenu=element.document.all(sMenu[i][1]);
168                              }
169      
170                              Td.innerText = "8";
171                              Td.style.font = "8pt Marlett";
172                          }
173      
174                          Tr.menuText = sMenu[i][0];
175                          Tr.menuData = lTrim(rTrim(sMenu[i][5]));
176                          Tr.onmouseenter = mi_OnMouseEnter;
177                          Tr.onmouseleave = mi_OnMouseLeave;
178                          Tr.onclick = mi_OnClick;
179                          Tr.onmousedown = mi_OnMouseDown;
180                      }
181                  }    //End For
182              }
183          }
184  
185          function init()
186           {
187              var strInner = lTrim(rTrim(element.innerHTML));
188  
189              if (strInner.length > 0)
190               {
191                  var arrayItems = strInner.split(";");
192                  
193                  buildMenu(arrayItems);
194              }
195          }
196  
197  
198          function element_OnDeActivate()
199           {
200              if (showSubMenu == null)
201                  hide();
202          }
203              
204          function mi_OnMouseEnter()
205           {
206              var Tr;
207              Tr=event.srcElement;
208  
209              event.srcElement.style.backgroundColor="#b6bdd2";
210              lightTr=event.srcElement;
211              tableMenu.setActive();
212              if(Tr.subMenu)
213               {
214                  showSubMenu = Tr.subMenu;
215                  showSubMenu.attachEvent("onhidden",onSubMenuHidden);
216                  showSubMenu.show(absLeft(element) + Tr.offsetLeft + Tr.offsetWidth,
217                                  absTop(element)+Tr.offsetTop);
218              }
219          }
220          function mi_OnMouseLeave()
221           {    
222              event.srcElement.style.backgroundColor="#f9f8f7";
223              event.srcElement.style.color="menutext";
224              lightTr=null;
225          }
226          function mi_OnClick()
227           {
228              if (lightTr.disabled == false)
229               {
230                  hide();
231                  var oEvent;
232                  oEvent=createEventObject();
233                  oEvent.returnValue=true;
234                  oEvent.menuText = lightTr.cells[1].innerText;
235                  oEvent.menuData = lightTr.menuData;
236                  idMenuClick.fire(oEvent);
237              }
238          }
239  
240          function mi_OnMouseDown()
241           {
242              var oEvent;
243  
244              oEvent = createEventObject();
245              oEvent.returnValue = true;
246              oEvent.menuText = lightTr.cells[1].innerText;
247              oEvent.menuData = lightTr.menuData;
248  
249              idMenuMouseDown.fire(oEvent);
250          }
251  
252          function fireBeforePopup(oTR)
253           {
254              var oEvent;
255  
256              oEvent = createEventObject();
257              oEvent.returnValue = true;
258  
259              if (oTR.cells.length > 1)
260                  oEvent.menuText = oTR.cells[1].innerText;
261              else
262                  oEvent.menuText = "";
263  
264              oEvent.menuData = oTR.menuData;
265              oEvent.disableItem = false;
266  
267              idMenuBeforePopup.fire(oEvent);
268  
269              var oResult = new Object;
270  
271              oResult.disableItem = oEvent.disableItem;
272              oResult.returnValue = oEvent.returnValue;
273  
274              return oResult;
275          }
276  
277          function browseMenuItem()
278           {
279              var oResult = null;
280  
281              for (var i = 0; i < tableMenu.rows.length; i++)
282               {
283                  var oRow = tableMenu.rows(i);
284  
285                  oResult = fireBeforePopup(oRow);
286  
287                  if (oResult.returnValue == false)
288                      return false;
289  
290                  oRow.disabled = oResult.disableItem;
291  
292                  if (typeof(oRow.menuData) != "undefined")
293                   {
294                      for (var j = 0; j < oRow.cells.length; j++)
295                          oRow.cells(j).disabled = oRow.disabled;
296                  }
297              }
298  
299              return true;
300          }
301  
302          function show(x, y)
303           {
304              if (browseMenuItem())
305               {
306                  oTransContainer.filters[0].Apply();
307                  with(oTransContainer)
308                   {
309                      style.left = 0;
310                      style.top = 0;
311                      style.width = divPanel.offsetWidth;
312                      style.height = divPanel.offsetHeight;
313                  }
314                  
315                  with(divPanel)
316                   {
317                      style.left = 0;
318                      style.top = 0;
319                      style.width = tableMenu.offsetWidth;
320                      style.height = tableMenu.offsetHeight;
321                  }
322  
323                  with(frmPanel)
324                   {
325                      style.left = 0;
326                      style.top = 0;
327                      style.width = divPanel.offsetWidth;
328                      style.height = divPanel.offsetHeight;
329                  }
330  
331                  with(element)
332                   {
333                      style.pixelLeft = x;
334                      style.pixelTop = y;
335                      style.width = divPanel.offsetWidth;
336                      style.height = divPanel.offsetHeight;
337                  }
338  
339                  element.style.visibility = "visible";
340                  oTransContainer.filters[0].Play();
341                  tableMenu.setActive();
342              }
343          }
344  
345          function showSubMenu(x, y, father)
346           {
347              show(x, y);
348          }
349  
350          function hide()
351           {
352              element.style.visibility = "hidden";
353              var oEvent;
354              oEvent = createEventObject();
355              idHidden.fire(oEvent);
356          }
357          
358          function onSubMenuHidden()
359           {
360              showSubMenu.detachEvent("onhidden", onSubMenuHidden)
361  
362              if (element != element.document.activeElement)
363                  hide();
364  
365              showSubMenu = null;
366          }
367  
368          function absLeft(ele)
369           {
370              var e, left = 0;
371              e = ele;
372              while(e.tagName != "BODY")
373               {
374                  left += e.offsetLeft;
375                  e = e.offsetParent;
376              }
377              return left;
378          }
379  
380          function absTop(ele)
381           {
382              var e, top = 0;
383              e=ele;
384              while(e.tagName != "BODY")
385               {
386                  top += e.offsetTop;
387                  e = e.offsetParent;
388              }
389              return top;
390          }
391          
392          function lTrim(s)
393           {
394              var i;
395              for(i = 0; i < s.length; i++)
396               {
397                  if(s.charCodeAt(i) != 32)
398                      return(s.substr(i));
399              }
400              return s;
401          }    
402  
403          function rTrim(str)
404           {
405              var nLen = str.length;
406              for (var i = str.length - 1; i >= 0; i--)
407                  if (str.charCodeAt(i) != 32)
408                      break;
409                  else
410                      nLen--;
411          
412              return str.substr(0, nLen);    
413          }
414  //--></script>
415      </head>
416      <body id="ctrBody" style="font-size:12;cursor:default;visibility:hidden;" onselectstart="return false;">
417          <DIV ID="oTransContainer" STYLE="z-index:4998;position:absolute; filter:progid:DXImageTransform.Microsoft.Fade(duration=0.4,overlap=1.0) ">
418              <iframe id="frmPanel" frameborder="0" scrolling="no" style="position:absolute;z-index:4999">
419              </iframe>
420              <div id="divPanel" class="Panel" style="position:absolute;z-index:5000">
421                  <table id="tableMenu" border="0" cellspacing="0" cellpadding="0" style="font-size:12">
422                      <tr id="titleRow">
423                          <td colspan="3" height="0" id="titleBar" onmousemove="Title_mMove()" onmousedown="Title_mDown()" onmouseup="Title_mUp()"></td>
424                      </tr>
425                  </table>
426              </div>
427          </DIV>
428      </body>
429      <script>
430      var xDown,yDown;
431      var xOrg,yOrg;
432      var bMoving=false;
433      var movingEle=element;
434      
435      function Title_mDown()
436       {
437          movingEle=element;
438          titleBar.setCapture();
439          
440          bMoving=true;
441          
442          xDown=event.x;yDown=event.y;    
443          
444          xOrg=movingEle.style.pixelLeft;
445          yOrg=movingEle.style.pixelTop;
446      }
447  
448      function Title_mUp()
449       {
450          if (bMoving)
451           {
452              bMoving = false;
453              movingEle = null;
454              titleBar.releaseCapture();
455          }
456      }
457      function Title_mMove()
458       {
459          if (bMoving)
460           {
461              movingEle.style.position = "absolute";
462              var nLeft,nTop;
463              
464              nLeft = movingEle.style.pixelLeft+event.x-xDown;
465              nTop = movingEle.style.pixelTop+event.y-yDown;
466              
467              if (nLeft > 0)
468                  movingEle.style.pixelLeft = nLeft;
469              else
470                  movingEle.style.pixelLeft = 0;
471              if (nTop > 0)
472                  movingEle.style.pixelTop = nTop;
473              else
474                  movingEle.style.pixelTop = 0;
475              
476          }
477      }
478      
479      </script>
480  </html>
481 

使用该htc的范例:
 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
 2  
 3  <html>
 4    <head>
 5      <title>PopupMenuTest</title>
 6      <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
 7      <meta name="CODE_LANGUAGE" Content="C#">
 8      <meta name=vs_defaultClientScript content="JavaScript">
 9      <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
10    </head>
11    <body MS_POSITIONING="GridLayout" oncontextmenu="return DisplayPopupMenu();">
12           <script language="javascript">
13          function DisplayPopupMenu()
14           {
15              menuTree.show(event.x, event.y);
16              return false;
17          }
18          
19          function menuTreeBeforePopup()
20           {
21          }
22          
23          function menuTreeClick(objTree)
24           {
25              
26              switch(event.menuData)
27               {
28                  case "refresh":    alert('refresh clicked.');
29                                  break;
30                  case "property":alert('property clicked.');
31                                  break;
32              }
33          }
34          </script>
35      <form id="Form1" method="post" runat="server">
36          <?XML:NAMESPACE PREFIX="hGui" /><?import namespace="hGui" implementation="/webcontrols/sMenu.htc" />
37          <hGui:smenu id="menuTree" onmenuclick="menuTreeClick()" onbeforepopup="menuTreeBeforePopup();" style="Z-INDEX: 100">
38              刷新,,,,./images/refresh.gif,refresh;
39              -,0,,,,;
40              属性 ,,,,./images/property.gif,property
41          </hGui:smenu>
42       </form>
43    </body>
44  </html>
45 

怎么样,发现了没?这个菜单可是模仿Office样式的哦。弹出菜单就介绍到这了,怎么应用就看你们的了。 如果这样你们还不知道怎样应用的话,<<---模仿一下这个看看吧,说不定会有灵感。
posted on 2006-04-12 15:29  XIAO牛  阅读(285)  评论(0)    收藏  举报