美文网首页
JS ContextMenu的简单实现

JS ContextMenu的简单实现

作者: FConfidence | 来源:发表于2017-09-17 19:29 被阅读15次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>contextmenu 鼠标右键</title>
        <style>
        #contextmenu {
            width: 180px;
            height: 240px;
            background-color: #f2f2f2;
            position: absolute;
            border: 1px solid #BFBFBF;
            box-shadow: 2px 2px 3px #aaaaaa;
        }
    
        .show {
            display: block;
        }
    
        .hidden {
            display: none;
        }
        </style>
    </head>
    
    <body>
        <script>
        (function() {
            var menu = document.createElement("div");
            menu.id = "contextmenu";
            menu.className = "hidden";
            document.body.appendChild(menu);
    
            addHandler(document, "contextmenu", function(ev) {
                preventDefault(ev);
                return false;
            });
    
            addHandler(document, "mouseup", openNewContextMenu);
            addHandler(document, "mousedown", function(ev) {
                menu.className = "hidden";
            });
    
            function openNewContextMenu(ev) {
                ev = ev || window.event;
                var btn = ev.button;
                if (btn == 2) {
                    // menu.style.left = ev.clientX + "px";
                    // menu.style.top = ev.clientY + "px";
                    var objPos = getNextContextMenuPostion(ev);
                    menu.style.left = objPos.left + 'px';
                    menu.style.top = objPos.top + 'px';
                    // console.log(getNextContextMenuPostion(ev))
                    menu.className = "show";
                }
            }
    
            function preventDefault(event) //阻止事件的默认行为
            {
                if (event.preventDefault) {
                    event.preventDefault(); //Chrome FireFox
                } else {
                    event.returnValue = false; //IE
                }
            }
    
            function addHandler(element, type, handler) {
                if (element.addEventListener) { //FireFox Chrome
                    element.addEventListener(type, handler, false);
                } else if (element.attachEvent) { //IE
                    element.attachEvent("on" + type, handler);
                } else {
                    //使用0级事件
                    //alert("请更新你的浏览器或者使用谷歌浏览器");
                    //element.setAttribute("on"+type, handler);
                    element['on' + type] = handler; //可以这样使用
                }
            }
    
            function getStyle(obj, attr) {
                if (obj.currentStyle) //针对IE浏览器
                {
                    return obj.currentStyle[attr];
                } else //针对Firefox
                {
                    return getComputedStyle(obj, false)[attr];
                }
                /**
                 * obj.style[attr]只能获取行间样式
                 * 复合样式: background
                 * 所以getStyle(obj,"background") 会报错
                 * 写成 getStyle(obj,"backgroundcolor")
                 */
            }
    
            function getNextContextMenuPostion(ev) {
                var x = ev.clientX,
                    y = ev.clientY,
                    html = document.documentElement,
                    vx = html.clientWidth,
                    vy = html.clientHeight,
                    // mw = menu.offsetWidth,
                    mw = parseInt(getStyle(menu, 'width')),
                    // mh = menu.offsetHeight;
                    mh = parseInt(getStyle(menu, 'height'));
                // console.log(x+mw, vx)
                return {
                    left: (x + mw) > vx ? (vx - mw - 10) : x,
                    top: (y + mh) > vy ? (vy - mh - 10) : y
                };
            }
    
        })();
        </script>
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:JS ContextMenu的简单实现

          本文链接:https://www.haomeiwen.com/subject/ufemsxtx.html