javascript - check for ('div')mouseenter on ('a')mouseleave - Stack Overflow

my problem is following:I got a trigger(a) and a popup(div). The div doesn't lie nested inside th

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)
            };
    });

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)
            };
    });
Share Improve this question edited Oct 11, 2011 at 11:03 axtavt 243k41 gold badges516 silver badges486 bronze badges asked Oct 11, 2011 at 10:57 chabuyachabuya 1431 gold badge3 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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/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, 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745226578a4617487.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信