检查 ('div')mouseenter 和 ('a')mouseleave
我的问题如下: 我有一个触发器(a)和一个弹出窗口(div)。 div 并不嵌套在锚点内。
- 当我将鼠标悬停在 a 上时,我希望显示 div。
- 当我从 a 转到 div 时,我希望它保持可见。
- 当我离开 div 时,我希望它关闭。
- 当我将鼠标悬停在 a 上并离开而不进入 div 时,我希望 div 关闭。
我已经弄清楚了大部分内容,但现在我正在为没有要求而苦苦挣扎。 2. 当检查 a 上的 mouseleave 时,我检查 div 上是否有 mouseenter 。如果是的话,我想中止 mouseleave。如果没有,我想关闭div。
我做错了什么?这是正确的方法吗?
这是标记:
<a href="#" class="popup_toggle" style='display:block;width:50px;height:50px;border:1px solid red;position:relative;'>Toggle</a>
<div class="popup_div" style='position:absolute;top:50px;left:0px;border:1px solid blue;display:none;'>Popup</div>
这是 jQuery:
$('.popup_toggle').mouseenter(function() {
var element = $(this).next('.popup_div');
$.data(this, 'timer', setTimeout(function() {
element.show(100);
}, 500));
});
$('.popup_toggle').mouseleave(function() {
clearTimeout($.data(this, 'timer'));
if($('.popup_div').mouseenter==true)
{
return false;
}
else
{
$('.popup_div').hide(100)
};
});
my problem is following:
I got a trigger(a) and a popup(div). The div doesn't lie nested inside the anchor.
- When I hover over a, I want the div to show up.
- When I go from a to the div, I want it to stay visible.
- When I leave the div, I want it to close.
- When I hover over a and leave without entering the div, I want the div to close.
I got most of that figured out, but now I'm struggeling with requierement no. 2.
When checking for mouseleave on a, I check if there is a mouseenter on the div. If it is, I want to abort the mouseleave. If not, I want to close the div.
What am I doing wrong? Is this even the right way to do this?
Here's the markup:
<a href="#" class="popup_toggle" style='display:block;width:50px;height:50px;border:1px solid red;position:relative;'>Toggle</a>
<div class="popup_div" style='position:absolute;top:50px;left:0px;border:1px solid blue;display:none;'>Popup</div>
Here's the jQuery:
$('.popup_toggle').mouseenter(function() {
var element = $(this).next('.popup_div');
$.data(this, 'timer', setTimeout(function() {
element.show(100);
}, 500));
});
$('.popup_toggle').mouseleave(function() {
clearTimeout($.data(this, 'timer'));
if($('.popup_div').mouseenter==true)
{
return false;
}
else
{
$('.popup_div').hide(100)
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要做的事情相当简单。输入触发器时,识别面板(图层、弹出窗口等),使用 .data() 保存彼此的引用,并让事件处理程序检查相关目标是否是触发器(从面板视图)或面板(从触发器视图)。我把一些东西放在一起。查看控制台日志以了解其工作原理... http://jsfiddle.net/rodneyrehm/X5uRD/< /a>
What you're trying to do is fairly simple. When entering the trigger, identify the panel (layer, popup, whatever), save reference to each other using .data() and have the event handlers check if the related targets are either the trigger (from the panel view) or the panel (from the trigger view). I threw something together. Have a look at the console log to see how this works… http://jsfiddle.net/rodneyrehm/X5uRD/
这很可能行不通……不。我建议您向
元素添加一个
mouseenter
和mouseleave
回调,并让它们设置一个全局变量来告诉您的其他回调如何处理它们的事件,即“如果全局变量为真,不要隐藏 mouseleave 上的弹出窗口,否则隐藏弹出窗口”或类似的内容。另一种方法是当 mouseleave 回调尝试隐藏弹出窗口时检查鼠标是否在弹出窗口内。但这可能比其价值要多得多的工作。
That will most likely not work...no. I would suggest that you add a
mouseenter
andmouseleave
callback to you<div>
element as well and have them set a global variable that tells your other callbacks how to handle their events, i.e. "if global variable is true, don't hide the popup on mouseleave, otherwise hide popup" or something like this.The other approach would be to check whether the mouse is inside the popup when the
mouseleave
callback tries to hide the popup. That might be much more work than it is worth though.我相信您的实现问题在于
div
上的mouseenter
会在a
的mouseleave
之后不久触发>。这将为您提供如下信息:
请注意,您必须确保从
.popup_toggle
事件和.popup_div
事件访问相同的计时器。您可能需要考虑使用 Ben Alman 的doTimeout
插件帮忙解决这个问题。它(通常)会产生比手动使用setTimeout
/clearTimeout
更清晰的代码。I believe the problem with your implementation is that the
mouseenter
on thediv
will fire shortly after themouseleave
from thea
.This would give you something like:
Note that you'll have to make sure that you access the same timer from both the
.popup_toggle
event and the.popup_div
event. You may want to consider using Ben Alman'sdoTimeout
plugin to help with this. It (usually) results in much clearer code than manually working withsetTimeout
/clearTimeout
.