1
81041
2019-10-18 cc4059f9f5a2cd6766f4d888bd35d19f3827a053
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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();
    });
});