右键菜单

<html>
<head>
  <style type="text/css">
 body{
  font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
  margin-left:0px;
 }
 #contextMenu{ /* The menu container */
  border:1px solid #202867; /* Border around the entire menu */
  background-color:#FFF; /* White background color of the menu */
  margin:0px;
  padding:0px;
  width:175px; /* Width of context menu */
  font-family:arial;
  font-size:12px;
  background-image:url('images/gradient.gif');
  background-repeat:repeat-y;
  
  /* Never change these two values */
  display:none;
  position:absolute;

 }
 #contextMenu a{ /* Links in the context menu */
  color: #000;
  text-decoration:none;
  line-height:25px;
  vertical-align:middle; 
  height:28px;
  
  /* Don't change these 3 values */
  display:block; 
  width:100%;
  clear:both;
  
 }
 #contextMenu li{ /* Each menu item */
  list-style-type:none;
  padding:1px;
  margin:1px;
  cursor:pointer; 
  clear:both;
 }
 #contextMenu li div{ /* Dynamically created divs */
  cursor:pointer; 
 }
 #contextMenu .contextMenuHighlighted{ /* Highlighted context menu item */
  border:1px solid #000;
  padding:0px;
  background-color:#E2EBED;
 
 }
 #contextMenu img{
  border:0px;
 }
 #contextMenu .imageBox{ /* Dynamically created divs for images in the menu */

  float:left;
  padding-left:2px;
  padding-top:3px;
  vertical-align:middle;
  
  width: 30px; /* IE 5.x */
  width/* */:/**/28px; /* Other browsers */
  width: /**/28px;
 }
 #contextMenu .itemTxt{
  float:left;  
  width: 120px; /* IE 5.x */
  width/* */:/**/140px; /* Other browsers */
  width: /**/140px;  
 }
 </style>
 <script type="text/javascript">
 
 /************************************************************************************************************
 (C) www.dhtmlgoodies.com, October 2005
 
 This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website. 
 
 Terms of use:
 You are free to use this script as long as the copyright message is kept intact. However, you may not
 redistribute, sell or repost it without our permission.
 
 Thank you!
 
 www.dhtmlgoodies.com
 Alf Magne Kalleland
 
 ************************************************************************************************************/

 
 var contextMenuObj;
 var MSIE = navigator.userAgent.indexOf('MSIE')?true:false;
 var navigatorVersion = navigator.appVersion.replace(/.*?MSIE (/d/./d).*/g,'$1')/1; 
 var activeContextMenuItem = false;
 function highlightContextMenuItem()
 {
  this.className='contextMenuHighlighted';
 }
 
 function deHighlightContextMenuItem()
 {
  this.className='';
 }
 
 function showContextMenu(e)
  {
   contextMenuSource = this;
   if(activeContextMenuItem)activeContextMenuItem.className='';
   if(document.all)e = event;
   var xPos = e.clientX;
   if(xPos + contextMenuObj.offsetWidth > (document.documentElement.offsetWidth-20)){
    xPos = xPos + (document.documentElement.offsetWidth - (xPos + contextMenuObj.offsetWidth)) - 20;
   }
  
   var yPos = e.clientY;
   if(window.document.body.scrollTop > 0)
     {
       yPos = (window.screen.Height) ? e.clientY + window.document.body.scrollTop -20 : e.clientY -20;
     }
     else if (window.pageYOffset)
     {
       yPos = (window.pageYOffset > 0) ? e.clientY + window.pageYOffset -20 : e.clientY -20;
     }
     else
     { yPos = e.clientY -20; }
   /* * */
   contextMenuObj.style.left = xPos + 'px';
   contextMenuObj.style.top = yPos + 'px';
   contextMenuObj.style.display='block';
   return false;
 }

 function hideContextMenu(e)
 {
  if(document.all) e = event;
  if(e.button==0 && !MSIE){
   
  }else{
   contextMenuObj.style.display='none';
  }
 }
 
 function initContextMenu()
 {
  contextMenuObj = document.getElementById('contextMenu');
  contextMenuObj.style.display = 'block';
  var menuItems = contextMenuObj.getElementsByTagName('LI');
  for(var no=0;no<menuItems.length;no++){
   menuItems[no].onmouseover = highlightContextMenuItem;
   menuItems[no].onmouseout = deHighlightContextMenuItem;
   
   var aTag = menuItems[no].getElementsByTagName('A')[0];
   
   var img = menuItems[no].getElementsByTagName('IMG')[0];
   if(img){
    var div = document.createElement('DIV');
    div.className = 'imageBox';
    div.appendChild(img);
    
    if(MSIE && navigatorVersion<6){
     aTag.style.paddingLeft = '0px';
    }
    
    var divTxt = document.createElement('DIV'); 
    divTxt.className='itemTxt';
    divTxt.innerHTML = aTag.innerHTML;
    
    aTag.innerHTML = '';
    aTag.appendChild(div);
    aTag.appendChild(divTxt);
    if(MSIE && navigatorVersion<6){
     div.style.position = 'absolute';
     div.style.left = '2px';
     divTxt.style.paddingLeft = '15px';
    }
    
    if(!document.all){
     var clearDiv = document.createElement('DIV');
     clearDiv.style.clear = 'both';
     aTag.appendChild(clearDiv);  
    }
   }else{
    if(MSIE && navigatorVersion<6){
     aTag.style.paddingLeft = '15px';
     aTag.style.width = (aTag.offsetWidth - 30) + 'px';
    }else{
     aTag.style.paddingLeft = '30px';
     aTag.style.width = (aTag.offsetWidth - 60) + 'px';
    }
   }
  }
  contextMenuObj.style.display = 'none';  
  document.documentElement.oncontextmenu = showContextMenu;
  document.documentElement.onclick = hideContextMenu;
 }

   </script>
</head>
<body>

   <div align="center">点击鼠标右键看效果</center>
   <ul id="contextMenu">
 <li><a href="http://www.sharejs.com"><img src="images/button.gif">Home</a></li>
 <li><a href="#" οnclick="document.getElementById('htmlSource').style.display='block';return false">View HTML</a></li>
 <li><a href="#">Menu item 3</a></li>
 <li><a href="#">Menu item 4</a></li>
   </ul>
   <script type="text/javascript">
     initContextMenu();
   </script>

</body>
</html>

 

这个上下文关联右键菜单是基于<ul>无序列表创建的,所以在网页里定制右键菜单只需要通过纯HTML代码即可,大大简化了用户操作.范例如下:
<ul id="contextMenu">
  <li><a href="http://www.sharejs.com">
<img src="images/button.gif">Home</a></li>
  <li><a href="#" οnclick="jsFunction();return false">
View HTML</a></li>
  <li><a href="#">Menu item 3</a></li>
  <li><a href="#">Menu item 4</a></li>
</ul>

注意,UL的ID必须为 "contextMenu"

你可以通过"initContextMenu"函数来初始化菜单.如:
<script type="text/javascript">
initContextMenu();
</script>

将上面这段代码放在 <UL><LI> 后面

要修改菜单的样式,可以直接修改CSS

将上面这段代码放在 <UL><LI> 后面

要修改菜单的样式,可以直接修改CSS

本文来源:http://www.sharejs.com/showdetails-459.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值