改变 YUI 菜单的鼠标行为

发布于 2024-07-12 11:30:21 字数 699 浏览 1 评论 0原文

我正在使用 Yui 构建一个“弹出”菜单,其使用鼠标的工作方式与平常有所不同。 这不是一个 ContextMenu,因为我希望它响应左键单击,而 ContextMenu 似乎倾向于响应右键单击。

按照示例,如果我这样做,菜单就会出现,并且一切都接近我想要的方式:

YAHOO.util.Event.addListener(myClickTarget, 'click', myThingGotClicked);

在 myThingGotClicked 函数中,我手动设置菜单的位置并 show() 它。

我的问题是我想将菜单可见性“绑定”到鼠标按钮的状态。 也就是说,在 mouseDown 上,我希望菜单出现,在 mouseUp 上,我希望菜单消失(选择活动项目,如果有的话)。 因此,监听“click”事件并没有做正确的事情,因为“click”仅在 mouseUp 之后发送。

“明显”的解决方案是这样做:

YAHOO.util.Event.addListener(myClickTarget, 'mousedown', myThingGotClicked);

但这不起作用。 在调试器中单步执行,您可以看到它实际上在按下鼠标时调出菜单,但随后某些东西立即隐藏了菜单。 全速行驶时,看起来什么也没有发生。

有什么想法吗?

I'm using Yui to build a "popup" menu that works a bit differently with the mouse than usual. This is not a ContextMenu, because I want it to respond to left clicks, and the ContextMenu seems bent on responding to right clicks.

Following the examples, if I do this, the menu comes up and everything is close to how I want it:

YAHOO.util.Event.addListener(myClickTarget, 'click', myThingGotClicked);

In my myThingGotClicked function, I manually set the menu's position and show() it.

My problem is that I want to "bind" the menu visibility to the state of the mouse button. That is, on a mouseDown, I want the menu to come up, and on a mouseUp, I want the menu to disappear (selecting the active item, if any). So, listening to the 'click' event doesn't do the right thing, because a "click" is only sent after mouseUp.

The "obvious" solution is to do this:

YAHOO.util.Event.addListener(myClickTarget, 'mousedown', myThingGotClicked);

But this doesn't work. Stepping through in a debugger, you can see that it does actually bring up the menu on a mousedown, but then something immediately hides the menu. At full speed, it looks like nothing happens at all.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-07-19 11:30:21

问题在于 MenuManager 类在文档级别侦听 mousedown 事件并隐藏所有可见的 Menu 实例。 因此,由于您正在构建一种独特的 Menu 实现,因此您需要在处理程序内停止 mousedown 事件的传播,以便 MenuManager 不会处理该事件。 这是一些伪代码:

var myThingGotClicked = function (event) {

    YAHOO.util.Event.stopPropagation(event);

    // Do other stuff

};

YAHOO.util.Event.on(myClickTarget, 'mousedown', myThingGotClicked);
  • Todd

The problem is that the MenuManager class listens for the mousedown event at the document level and hides all visible Menu instances. So, since you are building a unique sort of Menu implementation, you'll need to stop the propagation of the mousedown event inside your handler so that the MenuManager doesn't handle the event. Here is some pseudo code for you:

var myThingGotClicked = function (event) {

    YAHOO.util.Event.stopPropagation(event);

    // Do other stuff

};

YAHOO.util.Event.on(myClickTarget, 'mousedown', myThingGotClicked);
  • Todd
荆棘i 2024-07-19 11:30:21

这有点接近,因为菜单确实弹出了,但是如果您尝试在菜单中进行选择,那么下面页面的文本选择就会变得有点疯狂。 我认为,我还需要添加一个 mouseup 处理程序,因为释放鼠标时菜单不会下降。

我真正想要的是菜单在每个版本的 Mac OS 上都像菜单一样工作(直到最近 OS X 添加了“单击以使菜单‘粘性’到默认行为)。

That's a bit closer, as the menu does pop up, but if you try to make a selection in the menu, the text selection of the page underneath goes sort of nuts. I also need to add a mouseup handler, I think, as the menu doesn't go down on mouse release.

What I really want here are menus that work like menus on every version of the Mac OS (until more recently when OS X added the "click to make the menu 'sticky' to the default behavior).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文