检查 ('div')mouseenter 和 ('a')mouseleave

发布于 2024-12-09 08:43:26 字数 1210 浏览 1 评论 0原文

我的问题如下: 我有一个触发器(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 技术交流群。

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

发布评论

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

评论(3

枕头说它不想醒 2024-12-16 08:43:26

你想要做的事情相当简单。输入触发器时,识别面板(图层、弹出窗口等),使用 .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/

花开雨落又逢春i 2024-12-16 08:43:26

这很可能行不通……不。我建议您向

元素添加一个 mouseentermouseleave 回调,并让它们设置一个全局变量来告诉您的其他回调如何处理它们的事件,即“如果全局变量为真,不要隐藏 mouseleave 上的弹出窗口,否则隐藏弹出窗口”或类似的内容。

另一种方法是当 mouseleave 回调尝试隐藏弹出窗口时检查鼠标是否在弹出窗口内。但这可能比其价值要多得多的工作。

That will most likely not work...no. I would suggest that you add a mouseenter and mouseleave 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.

凉薄对峙 2024-12-16 08:43:26

我相信您的实现问题在于 div 上的 mouseenter 会在 amouseleave 之后不久触发>。

这将为您提供如下信息:

$('.popup_toggle').mouseenter(function() {
    // Clear any pending "hide" timer
    // Set a show timer
});

$('.popup_toggle').mouseleave(function() {
    // Clear any pending "show" timer
    // Set a hide timer
});

$('.popup_div').mouseenter(function() {
    // Clear any pending "hide" timer
});

请注意,您必须确保从 .popup_toggle 事件和 .popup_div 事件访问相同的计时器。您可能需要考虑使用 Ben Alman 的 doTimeout 插件帮忙解决这个问题。它(通常)会产生比手动使用 setTimeout/clearTimeout 更清晰的代码。

I believe the problem with your implementation is that the mouseenter on the div will fire shortly after the mouseleave from the a.

This would give you something like:

$('.popup_toggle').mouseenter(function() {
    // Clear any pending "hide" timer
    // Set a show timer
});

$('.popup_toggle').mouseleave(function() {
    // Clear any pending "show" timer
    // Set a hide timer
});

$('.popup_div').mouseenter(function() {
    // Clear any pending "hide" timer
});

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's doTimeout plugin to help with this. It (usually) results in much clearer code than manually working with setTimeout/clearTimeout.

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