jquery 可拖动和鼠标悬停
我目前有一些下拉菜单,可以在鼠标悬停时打开。我正在使用jquery ui 中的draggable 和droppable 实现一些拖放功能。拖动时似乎菜单的鼠标悬停事件不会触发,有没有办法让它们工作?
我已按如下方式实现(简化):
$('#some_id').draggable({ helper: 'clone', opacity: 0.35, zIndex: 20000, cursor: 'move' });
$('#some_menu').live('mouseenter click', function(){jThis.find('div').addClass('opened');});
I currently have some dropdown menus which open on mouse over. I'm implementing some drag-n-drop features using draggable and droppable from jquery ui. It seems that the mouseover events for the menus do not fire when dragging, is there a way to allow them to work?
I've implemented it as follows (simplified):
$('#some_id').draggable({ helper: 'clone', opacity: 0.35, zIndex: 20000, cursor: 'move' });
$('#some_menu').live('mouseenter click', function(){jThis.find('div').addClass('opened');});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚发现这是一个非常逻辑的问题。一旦你开始拖动元素,它就会粘在你的鼠标指针下面。因此,它会一直悬停在当前元素上!
一个(不太漂亮的)修复方法是设置cursorAt选项,使鼠标指针位于可拖动元素之外:
如果有一种方法可以将鼠标指针传递到正在拖动的元素下方,那就更好了,但是到目前为止我还没有找到解决方案。
希望这会有所帮助!
I just found out that this is a very logical problem. Once you start dragging the element, it sticks under your mouse pointer.. hence, it'll just hover over the current element all the time!!
A (not so pretty) fix is to set the cursorAt option so the mouse pointer is outside of the draggable element:
It would be much nicer if there is a way to somehow pass the mouse pointer underneath the element that is being dragged, but so far I haven't found a solution for that.
Hope this helps a bit!
正如Marcel Paans指出的那样,问题是助手粘在鼠标光标下方。
解决方案是将辅助元素上的 CSS 属性
pointer-events
设置为none
。这样做将使指针事件在助手下面的元素上触发。
您可以通过为 helper 选项提供回调来生成 helper 元素来轻松完成此操作:
As Marcel Paans indicated the problem is that the helper sticks under your mouse cursor.
The solution is to set the CSS property
pointer-events
tonone
on the helper element.Doing this will let the pointer events fire on the elements underneath the helper.
You can do this easy by supplying the helper option with a callback to generate the helper element:
这可以通过在定义 droppable 的地方使用“over:”和“out:”来实现。
不过,在您的场景中,您必须使菜单可放置,我猜这并不完全是您想要的(我假设您尝试放置到菜单中的某些内容,而不是整个下拉菜单) )。也许只是什么都不做或者恢复这些项目上的 drop: 功能..?
积分和更多信息:
http://forum.jquery。 com/topic/draggable-highlighting-custom-div-on-droppable-hover
http://jsfiddle.net/nickadeemus2002/wWbUF/1/
This can be achieved by using "over:" and "out:" where your droppable is defined.
In your scenario, though, you'd have to make your menus droppable, which I'm guessing is not exactly what you want (I'm assuming you're trying to drop to something that is IN the menu, not the whole dropdown). Perhaps just do nothing or revert for the drop: function on those items..?
Credits and more info:
http://forum.jquery.com/topic/draggable-highlighting-custom-div-on-droppable-hover
http://jsfiddle.net/nickadeemus2002/wWbUF/1/
您可以为可拖动对象编写一个拖动事件处理程序,以检查光标是否位于要处理鼠标悬停事件的元素上。您必须使用静态元素的 jQuery 方法 offset()、width() 和 height() 自行检测鼠标悬停,以及拖动事件的事件对象中的光标位置或 jQuery UI 自己的 ui.offset,以更适合您的目的为准。
另一种选择,不太优雅但可能更容易实现,是使静态元素成为可放置元素,在这种情况下,jQuery UI 允许您在可拖动元素悬停在其上时处理事件。如果您实际上不想允许的话,您可以阻止删除。
You can write a drag event handler for your draggable that checks whether the cursor is over the element for which you want to handle the mouseover event. You would have to detect the mouseover yourself using jQuery methods offset(), width() and height() for the static element and either cursor position from the event object for drag event or jQuery UI's own ui.offset, whichever fits your purpose better.
Another option, less elegant but probably easier to implement, would be to make your static element a droppable in which case jQuery UI lets you handle an event when a draggable hovers over it. You can then prevent the drop if you don't actually want to allow it.
根据 Yonatan 发布的内容:
你的 JS:
只需将其添加到你的 CSS 中:
会更干净一些。
Based on what Yonatan posted:
Your JS:
Just add this to your CSS:
is a little cleaner.