javascript - 为我的网络应用制作自定义右键单击上下文菜单

标签 javascript jquery jquery-plugins contextmenu right-click

我有一些网站,例如 google-docs 和 map-quest,当您右键单击时,它们具有自定义下拉菜单。他们以某种方式覆盖了浏览器的下拉菜单行为,我现在可以确定他们是如何做到的。我找到了一个 jQuery plugin这样做,但我仍然对一些事情感到好奇:

  • 这是如何运作的?浏览器的下拉菜单是真的被覆盖了,还是只是模拟出来的效果?如果是,怎么做?
  • 插件抽象掉了什么?幕后发生了什么?
  • 这是实现此效果的唯一方法吗?

custom context menu image

See several custom-context menus in action

最佳答案

我知道这个问题很老了,但我只是想出了同样的问题并自己解决了,所以我会回答以防有人像我一样通过谷歌找到这个问题。我的解决方案基于@Andrew 的解决方案,但之后基本上修改了所有内容。

编辑:看到这最近很受欢迎,我决定也更新样式,使它看起来更像 2014 而不是 windows 95。我修复了 @Quantico 和 @Trengot 发现的错误所以现在这是一个更可靠的答案。

编辑 2:我使用 StackSnippets 进行设置,因为它们是一项非常酷的新功能。我离开 good jsfiddle 此处供引用思想(单击第 4 个面板以查看它们的工作情况)。

新堆栈片段:

// JAVASCRIPT (jQuery)

// Trigger action when the contexmenu is about to be shown
$(document).bind("contextmenu", function (event) {
    
    // Avoid the real one
    event.preventDefault();
    
    // Show contextmenu
    $(".custom-menu").finish().toggle(100).
    
    // In the right position (the mouse)
    css({
        top: event.pageY + "px",
        left: event.pageX + "px"
    });
});


// If the document is clicked somewhere
$(document).bind("mousedown", function (e) {
    
    // If the clicked element is not the menu
    if (!$(e.target).parents(".custom-menu").length > 0) {
        
        // Hide it
        $(".custom-menu").hide(100);
    }
});


// If the menu element is clicked
$(".custom-menu li").click(function(){
    
    // This is the triggered action name
    switch($(this).attr("data-action")) {
        
        // A case for each action. Your actions here
        case "first": alert("first"); break;
        case "second": alert("second"); break;
        case "third": alert("third"); break;
    }
  
    // Hide it AFTER the action was triggered
    $(".custom-menu").hide(100);
  });
/* CSS3 */

/* The whole thing */
.custom-menu {
    display: none;
    z-index: 1000;
    position: absolute;
    overflow: hidden;
    border: 1px solid #CCC;
    white-space: nowrap;
    font-family: sans-serif;
    background: #FFF;
    color: #333;
    border-radius: 5px;
    padding: 0;
}

/* Each of the items in the list */
.custom-menu li {
    padding: 8px 12px;
    cursor: pointer;
    list-style-type: none;
    transition: all .3s ease;
    user-select: none;
}

.custom-menu li:hover {
    background-color: #DEF;
}
<!-- HTML -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js"></script>

<ul class='custom-menu'>
  <li data-action="first">First thing</li>
  <li data-action="second">Second thing</li>
  <li data-action="third">Third thing</li>
</ul>

<!-- Not needed, only for making it clickable on StackOverflow -->
Right click me

注意:您可能会看到一些小错误(下拉菜单远离光标等),请确保它在 jsfiddle 中有效,因为它与您的网页比 StackSnippets 可能更相似。

关于javascript - 为我的网络应用制作自定义右键单击上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4495626/

相关文章:

javascript - jQuery 自动完成功能不起作用

javascript - PhoneGap 在应用程序浏览器中按后退键时未关闭

javascript - 有没有办法在忽略 HTML 属性的 ORDER 的情况下比较 jQuery 对象?

javascript - Bootstrap 移动导航下拉菜单在打开时跳转

javascript - 如何验证用户在选择表单中选择的内容

javascript - 禁用除单击之外的所有 hammer.js( Angular 锤)事件

javascript - Jquery - 遍历复选框和多个元素

javascript - 有没有等同于 jQuery 的 $.fn.extend 的纯 Javascript?

php - 如何为动态生成的字段创建规则

javascript - jQuery leaveNotice 插件和 Internet Explorer