为我的网络应用程序制作自定义右键单击上下文菜单
我有一些网站,例如 google-docs 和 map-quest,当您右键单击时,它们具有自定义下拉菜单。他们以某种方式覆盖了浏览器下拉菜单的行为,我现在确切地知道他们是如何做到的。我找到了一个 jQuery 插件 可以做到这一点,但我仍然对几件事:
- 这是如何工作的?浏览器的下拉菜单实际上被覆盖了,还是只是模拟的效果?如果是这样,怎么办?
- 该插件抽象了什么?幕后发生了什么?
- 这是达到这种效果的唯一方法吗?
I've a few websites like google-docs and map-quest that have custom drop down menus when you right-click. Somehow they override the browser's behavior of drop-down menu, and I'm now sure exactly how they do it. I found a jQuery plugin that does this, but I'm still curious about a few things:
- How does this work? Is the browser's drop-down menu actually being overridden, or is the effect just simulated? If so, how?
- What does the plugin abstract away? What's going on behind the scenes?
- Is this the only way of achieving this effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我知道这个问题很老了,但我刚刚提出了同样的问题并自己解决了它,所以我回答以防有人像我一样通过谷歌找到这个问题。我的解决方案基于@Andrew 的解决方案,但后来基本上修改了所有内容。
编辑:看到它最近有多受欢迎,我决定也更新样式,使其看起来更像 2014 年,而不是像 Windows 95。我修复了 @Quantico 和 @Trengot 发现的错误,所以现在它是更可靠的答案。
编辑 2:我使用 StackSnippets 设置它,因为它们是一个非常酷的新功能。我将 good jsfiddle 留在这里供参考(单击第四个面板)看看他们工作)。
新堆栈片段:
注意:您可能会看到一些小错误(下拉列表远离光标等),请确保它在 中工作jsfiddle,因为它比 StackSnippets 更类似于您的网页。
I know this question is very old, but just came up with the same problem and solved it myself, so I'm answering in case anyone finds this through google as I did. I based my solution on @Andrew's one, but basically modified everything afterwards.
EDIT: seeing how popular this has been lately, I decided to update also the styles to make it look more like 2014 and less like windows 95. I fixed the bugs @Quantico and @Trengot spotted so now it's a more solid answer.
EDIT 2: I set it up with StackSnippets as they're a really cool new feature. I leave the good jsfiddle here for reference thought (click on the 4th panel to see them work).
New Stack Snippet:
Note: you might see some small bugs (dropdown far from the cursor, etc), please make sure that it works in the jsfiddle, as that's more similar to your webpage than StackSnippets might be.
正如阿德里安所说,插件将以同样的方式工作。您需要三个基本部分:
1:
'contextmenu'
事件的事件处理程序:在这里,您可以将事件处理程序绑定到您想要显示菜单的任何选择器。我选择了整个文档。
2:
'click'
事件的事件处理程序(关闭自定义菜单):3:控制菜单位置的 CSS:
CSS 的重要之处在于包含
z- index
和position:absolute
将所有这些包装在一个灵活的 jQuery 插件中并不会太难。
您可以在这里看到一个简单的演示:http://jsfiddle.net/andrewwhitaker/fELma/
As Adrian said, the plugins are going to work the same way. There are three basic parts you're going to need:
1: Event handler for
'contextmenu'
event:Here, you could bind the event handler to any selector that you want to show a menu for. I've chosen the entire document.
2: Event handler for
'click'
event (to close the custom menu):3: CSS to control the position of the menu:
The important thing with the CSS is to include the
z-index
andposition: absolute
It wouldn't be too tough to wrap all of this in a slick jQuery plugin.
You can see a simple demo here: http://jsfiddle.net/andrewwhitaker/fELma/
下面是 javascript 中右键单击上下文菜单的示例:
右键单击上下文菜单
使用原始 javasScript 代码实现上下文菜单功能。您可以检查一下吗,希望这对您有帮助。
实时代码:
here is an example for right click context menu in javascript:
Right Click Context Menu
Used raw javasScript Code for context menu functionality. Can you please check this, hope this will help you.
Live Code:
浏览器的上下文菜单被覆盖。无法在任何主要浏览器中增强本机上下文菜单。
由于插件正在创建自己的菜单,因此唯一真正被抽象的部分是浏览器的上下文菜单事件。该插件根据您的配置创建一个 html 菜单,然后将该内容放置在您单击的位置。
是的,这是创建自定义上下文菜单的唯一方法。显然,不同的插件所做的事情略有不同,但它们都会覆盖浏览器的事件并将自己的基于 html 的菜单放置在正确的位置。
The browser's context menu is being overridden. There is no way to augment the native context menu in any major browser.
Since the plugin is creating its own menu, the only part thats really being abstracted is the browser's context menu event. The plugin creates an html menu based on your configuration, then places that content at the location of your click.
Yes, this is the only way to go about creating a custom context menu. Obviously, different plugins do things slightly different, but they will all override the browser's event and place their own html-based menu in the correct place.
您可以观看本教程:http://www.youtube.com/watch?v=iDyEfKWCzhg
确保上下文菜单首先是隐藏的并且具有绝对位置。这将
确保不会有多个上下文菜单和无用的上下文菜单创建。该页面的链接位于 YouTube 视频的描述中。
You can watch this tutorial: http://www.youtube.com/watch?v=iDyEfKWCzhg
Make sure the context menu is hidden at first and has a position of absolute. This will
ensure that there won't be multiple context menu and useless creation of context menu. The link to the page is placed in the description of the YouTube video.
我知道这也很旧了。我最近需要创建一个上下文菜单,将其注入到其他具有基于单击的元素的不同属性的网站中。
这相当粗糙,并且可能有更好的方法来实现这一目标。
它使用 jQuery 上下文菜单库 位于此处
我很喜欢创建它,尽管你们可能会有一些用处。
这是小提琴。我希望它能够帮助那里的人。
I know that this is rather old also. I recently had a need to create a context menu that I inject into other sites that have different properties based n the element clicked.
It's rather rough, and there are probable better ways to achieve this.
It uses the jQuery Context menu Library Located Here
I enjoyed creating it and though that you guys might have some use out of it.
Here is the fiddle. I hope that it can hopefully help someone out there.
简单
注意:
不要使用 display:none 而是使用 opacity 来隐藏和显示
额外的CSS
Simple One
Note:
dont use display:none instead use opacity to hide and show
extra css
我使用 Bootstrap 有一个很好且简单的实现,如下所示。
I have a nice and easy implementation using Bootstrap as follows.