jQuery 将鼠标悬停在两个单独的元素上
我有两个单独的元素设置,它们出现在 DOM 的不同部分 - 我面临的问题是它们是绝对定位的,并且我无法将它们包装在容器 div 中。
我在这里设置了一个 JSfiddle - http://jsfiddle.net/sA5C7/1/
我是什么尝试做的是:将元素移入和移出一起 - 这样用户就可以在任一元素之间移动鼠标,并且只有当它们同时移开时,它才会再次隐藏吗?
我该如何设置?因为目前,一旦我离开单个元素 - 它就会触发该元素的“离开事件”等。
I have two separate elements setup which appear on different parts of the DOM - the problem I am facing is that they are absolutely positioned and I can't wrap them in a container div.
I have setup a JSfiddle here - http://jsfiddle.net/sA5C7/1/
What I am trying to do is: move the elements in and out together - so that a user can move their mouse between either element and ONLY once they move off BOTH would it hide again ?
How can I set this up? Because at the moment, once I move off a single element - it fires the "leave event" for that element etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用您设置的两个布尔变量,每个变量对应每个元素。当您输入元素时,它变为 true;如果您离开,则变为 false。
并且只有当两者都为 false 时才会离开 =>隐藏元素。
You could use two boolean variables that you set, each for every element. It gets true when you enter the element and false if you leave.
And only when both are false on leaving => hide the elements.
接受的解决方案效果很好。稍微简化一下并举个例子。
https://jsfiddle.net/ngb1q3kp/2/
如果您想要一个更抽象的示例。这可以轻松处理两个以上的元素: https://jsfiddle.net/q6o2v8fz/
The accepted solution works great. To simplify a bit and give an example.
https://jsfiddle.net/ngb1q3kp/2/
If you want a more abstracted example. This can easily handle more than two elements: https://jsfiddle.net/q6o2v8fz/