简体   繁体   English

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

[英]check for ('div')mouseenter on ('a')mouseleave

my problem is following: I got a trigger(a) and a popup(div). 我的问题是:我有一个触发器(a)和一个弹出窗口(div)。 The div doesn't lie nested inside the anchor. div不嵌套在锚内。

  • When I hover over a, I want the div to show up. 当我将鼠标悬停在a上时,我希望div显示出来。
  • When I go from a to the div, I want it to stay visible. 当我从一个div到div时,我希望它保持可见。
  • When I leave the div, I want it to close. 当我离开div时,我希望它关闭。
  • When I hover over a and leave without entering the div, I want the div to close. 当我将鼠标悬停在a并离开而没有进入div时,我希望div关闭。

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. 2.检查a上的mouseleave时,检查div上是否有mouseenter。 If it is, I want to abort the mouseleave. 如果是,我想中止mouseleave。 If not, I want to close the div. 如果没有,我想关闭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: 这是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)
            };
    });

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). 进入触发器时,识别面板(图层,弹出窗口,等等),使用.data()保存彼此的引用,并让事件处理程序检查相关目标是触发器(从面板视图)还是面板(从触发视图)。 I threw something together. 我扔了一些东西。 Have a look at the console log to see how this works… http://jsfiddle.net/rodneyrehm/X5uRD/ 看看控制台日志,看看它是如何工作的...... http://jsfiddle.net/rodneyrehm/X5uRD/

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, ie "if global variable is true, don't hide the popup on mouseleave, otherwise hide popup" or something like this. 我建议您添加一个mouseentermouseleave回调给你的<div>元素,并让它们设置一个全局变量,告诉你的其他回调如何处理它们的事件,即“如果全局变量为真,不要隐藏在mouseleave上弹出,否则隐藏弹出窗口“或类似的东西。

The other approach would be to check whether the mouse is inside the popup when the mouseleave callback tries to hide the popup. 另一种方法是在mouseleave回调试图隐藏弹出窗口时检查鼠标是否在弹出窗口内。 That might be much more work than it is worth though. 但这可能比它的价值更多。

I believe the problem with your implementation is that the mouseenter on the div will fire shortly after the mouseleave from the a . 我相信,你的执行问题是mouseenterdiv将在不久后火mouseleavea

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. 请注意,您必须确保从.popup_toggle事件和.popup_div事件访问同一个计时器。 You may want to consider using Ben Alman's doTimeout plugin to help with this. 你可能想考虑使用Ben Alman的doTimeout插件来帮助解决这个问题。 It (usually) results in much clearer code than manually working with setTimeout / clearTimeout . 它(通常)导致比手动使用setTimeout / clearTimeout更清晰的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM