function RightMenu() {
|
this.items = {txt: '删除', cla: 'menu-del'};
|
}
|
|
/**
|
* @param {Object} menuObj 菜单列表
|
* @param {int} disX 鼠标在屏幕x轴方向的点击的位置
|
* @param {int} disY 鼠标在屏幕y轴方向的点击的位置
|
*/
|
RightMenu.prototype.showMenu = function(menuObj, disX, disY) {
|
if(menuObj != undefined) {
|
this.items = menuObj;
|
}
|
//创建前清除右键内容
|
this.hideMenu();
|
|
var scrollY = $(document).scrollTop(); // 获取页面滚动的高度
|
//创建标签
|
var __div=$('<div id="rightMenu" style="background-color:#fff;"></div>');
|
var __a=new Array();
|
|
//根据this.items的值生成a标签并添加到div中
|
for(var i=0; i<this.items.length;i++)
|
{
|
__a[i]=$('<a href="javascript:;" class="'+this.items[i].cla+'">'+this.items[i].txt+'</a>');
|
__div.append(__a[i]);
|
}
|
|
//将div添加到body中
|
$('body').append(__div);
|
|
// 设置div的样式
|
$('#rightMenu').css({
|
'position':'absolute',
|
'top': disY + scrollY+'px',
|
'left': disX+'px',
|
'border-top':'1px solid #349d36',
|
'border-left':'1px solid #349d36',
|
'border-right':'1px solid #349d36',
|
'z-index': '999999'
|
});
|
$('#rightMenu a').css({
|
'display':'block',
|
'padding':'6px 14px',
|
'text-decoration':'none',
|
'text-align':'center',
|
'color':'#000000',
|
'border-bottom':'1px solid #349d36'
|
});
|
}
|
|
RightMenu.prototype.hideMenu = function() {
|
// 清除右键内容
|
$('#rightMenu').remove();
|
}
|
|
var rightMenu = new RightMenu();
|
|
$(function() {
|
document.oncontextmenu=function(){
|
return false;
|
};
|
// 鼠标右键菜单移入移出事件
|
$('body').on('mouseover', '#rightMenu a', function(){
|
$(this).css({
|
'background-color': '#349d36',
|
'color': '#fff'
|
});
|
});
|
$('body').on('mouseout', '#rightMenu a', function(){
|
$(this).css({
|
'background-color': '#fff',
|
'color': '#000'
|
});
|
});
|
|
// 点击页面中的任意一个位置清除右键菜单
|
$('body').on('click', 'div', function() {
|
rightMenu.hideMenu();
|
});
|
});
|