jquery 简短 右键菜单 兼容ie6 ie7 ie8 firefox chrome

本文介绍了一种使用JavaScript禁用网页上的右键点击,并实现自定义上下文菜单的方法。通过监听鼠标事件并阻止默认行为来实现右键功能的替代方案,同时提供了如何显示自定义菜单的具体实现。

 

 

$( function (){
document.oncontextmenu
= function (){ return   false ;} // 屏蔽右键
document.onmousemove = mouseMove; // 记录鼠标位置
});
var  mx = 0 ,my = 0 ;
function  mouseMove(ev){Ev = ev || window.event; var  mousePos = mouseCoords(Ev);mx = mousePos.x;my = mousePos.y;} 
function  mouseCoords(ev){
    
if (ev.pageX || ev.pageY){ return {x:ev.pageX,y:ev.pageY};}
    
return {x:ev.clientX,y:ev.clientY + $(document).scrollTop()};
}

$.fn.extend({RightMenu: 
function (id,options){options  =  $.extend({menuList:[]},options); var  menuCount = options.menuList.length;
    
if  ( ! $( " # " + id)[ 0 ]){
        
var  divMenuList = " <div id=\ "" +id+ " \ "  class=\ " div_RightMenu\ " ><div><ul class='ico'> " ;
        
for ( var  i = 0 ;i < menuCount;i ++ ){
            divMenuList
+= " <li class=\ " RMli_ " +options.menuList[i].menuclass+ " \ "  onclick=\ "" +options.menuList[i].clickEvent+ " \ " > " + options.menuList[i].menuName + " </li> " ;
        }
        divMenuList 
+=   " </ul></div><div> " ;
        $(
" body " ).append(divMenuList).find( " # " + id).hide().find( " li " )
        .bind(
" mouseover " , function (){$( this ).addClass( " RM_mouseover " );})
        .bind(
" mouseout " , function (){$( this ).removeClass( " RM_mouseover " );});
        $(document).click(
function (){$( " # " + id).hide();});
    }
    
return   this .each( function (){
        
this .oncontextmenu = function (){
        /*这段 判断鼠标移到页面的最右侧或者最下侧 防止滚动条变化 {*/
        var mw=$(document).width(),mh=$(document).height(),
            w=$('#'+id).width(), h=$('#'+id).height();
         if(mh<h+my){my=my-h;}//超 高
         if(mw<w+mx){mx=mx-w;}//超 宽
      /*} 当然也可以直接跳过*/

            $( " # " + id).hide().css({top:my,left:mx}).show();
        }
    });
    }
});

 

 

 

/Files/Task/rm.rar

转载于:https://www.cnblogs.com/Task/archive/2009/12/30/1635851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值